Posts Tagged ‘CSS’

How to Hide/Show Html Elements

An HTML website contains mainly text, images, and link controls. A common way of organizing all the controls is by placing the elements into a <div> control. With a <div> control, you can manipulate everything from the font to placement in an HTML page using CSS (cascading style sheets). Note that although the CSS code for manipulation is beyond the scope of this tutorial, we will be using CSS to state whether or not to display a div box.

We begin with an example of a <div> control. At this point, the code does nothing special.

<body>
  <div id="myDivBox">
    <h2>My Title</h2>
    <p>This is some sample text</p>
  </div>
</body>

Next, we add CSS that controls whether or not to display the content of the <div> control. The CSS attribute is the keyword ‘display’. The ‘display’ keyword accepts the values ‘block‘, ‘none‘, ‘list-item‘, and ‘inline‘. Our example will utilize ‘none’ that hides the <div> control and ‘block‘ that will display it (as a block as opposed to inline). Our <div> control will intially begin hidden, so let’s add a style attribute to the <div> control as shown below.

<body>
  <div id="myDivBox" style="display:none;">
    <h2>My Title</h2>
    <p>This is some sample text</p>
  </div>
</body>

Next, we will add javascript method to control the CSS attributes of our <div> control. As with all javascript, the script belongs in the head. The method is called toggleDiv and accepts no inputs. What the script is doing is checking if the <div> control is in a ‘block‘ or ‘none‘ state. Whichever state is found, we set the opposite.

<head>
  <script type="text/javascript">
    function toggleDiv()
    {
      if (document.getElementById("myDivBox").style.display == "block")
      {
        document.getElementById("myDivBox").style.display = "none";
      }
      else
      {
        document.getElementById("myDivBox").style.display = "block";
      }
    }
  </script>
</head>

Finally, we will add an input button that will toggle whether or not to display the <div> control. The input button will call the javascript method via an onClick action.

<body>
  <input type="button" value="Toggle" onclick="toggleDiv()">
  <div id="myDivBox" style="display:none;">
    <h2>My Title</h2>
    <p>This is some sample text</p>
  </div>
</body>

Here is a working example:

A complete HTML example is listed below. To run this, simply copy the code into a file with an html extension.

<html>
  <head>
    <script type="text/javascript">
      function toggleDiv()
      {
        if (document.getElementById("myDivBox").style.display == "block")
        {
          document.getElementById("myDivBox").style.display = "none";
        }
        else
        {
          document.getElementById("myDivBox").style.display = "block";
        }
      }
    </script>
  </head>
  <body>
    <input type="button" value="Toggle" onclick="toggleDiv()">
    <div id="myDivBox" style="display:none;">
      <h2>My Title</h2>
      <p>This is some sample text</p>
    </div>
  </body>
</html>

Post Fancy Sample Code on Website

You may have noticed the fancing code snippets that accompany many of the tutorials on this website! I would love to say I developed this myself, but the truth is that this is a free resource free for anybody to download and use. It is called SyntaxHighlighter and can be found at http://code.google.com/p/syntaxhighlighter/.

What is Included?

So what does the Syntax Highlighter come with? Below is a list my favorite features of this software:

  1. It’s completely free!
  2. Associate keywords for supported languages are highlighted.
  3. Each line of code is numbered and colored for easier reading!
  4. Allows one click to code only view.
  5. Allows one click copy to clipboard of code.
  6. Allows one click copy to printer.

What Comes With the Download

The file can be downloaded from their official website at http://code.google.com/p/syntaxhighlighter/ as a rar compressed file. Inside the compressed file, you will find various folders and files. The core files that are required include:

  1. clipboard.swf
  2. shCore.js
  3. SyntaxHighlighter.css

Still, there are many more JavaScript files that have been included in the download. Not all of these files are required. Instead, you choose additional optional JavaScript files for the programming or scripting languages you want the Syntax Highlighter to associate.

Installation After Uploading Files

After you have uploaded the required and accompanying optional files, you will need to include the javascript and css files to the header of the pages with which you plan on using the Syntax Highlighter. See the example below:

<link type="text/css" rel="stylesheet" href="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Styles/SyntaxHighlighter.css"></link>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shCore.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushCSharp.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushJava.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushPhp.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushXml.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushJScript.js"></script>
<script language="javascript">
  window.onload = function () {
    dp.SyntaxHighlighter.ClipboardSwf = 'http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/clipboard.swf';
    dp.SyntaxHighlighter.HighlightAll('code');
  }
</script>

In the above, there Line 1 includes the CSS file. Next, Line 2 includes the one core JavaScript file. Lines 4 through 8 include optional JavaScript files. Lines 9 through 14 include setup for using Syntax Highlighter. The portion requires including the clipboard.swf file.

Usage

I assume you already have formatted code contained within the pre tags, as found in a previous tutorial called 3 Steps to Posting Sample Code on your Website. As of now, all you have is a formatted tag, without the fancy upgrades. All you need to add for fancy code formatting is to add name=”code” class=”html”. Exchange html with the type of file you have included. Click to see sample supported files: http://code.google.com/p/syntaxhighlighter/wiki/Languages. An example:

<pre name="code" class="html">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Victor's Programming Aid&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;p&gt;This is a test&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>

Conclusion

With that, you now have fancy code formatting on your website. Comment on some examples of where you have used the Syntax Highlighter in action!

Web Developer Plugin for Firefox

The Web Developer designed for Firefox, Flock and Seamonkey is a free extension that adds a menu and a toolbar. It will run on any platform that these browsers support including Windows, Mac OS X and Linux. Just to list a few features:

  • Disable Javascript
  • Disable Referrer
  • Disable Cookies
  • Add Cookies
  • View Cookie Information
  • Disable Styles
  • Edit Styles (on the fly)
  • Display Form Details
  • Show Passwords
  • Make Form Fields Editable
  • Disable Images
  • Edit HTML (on the fly)
  • Resize Window
  • Various Validation
  • View Generated Source
  • Many other features…

Most of the features are useful for testing various browser compatibilities. Not all users will enable cookies, referrers, images, or javascript on their browser. The Web Developer plugin allows an easy way to test how your site will handle various situations. Most useful if Edit CSS and Edit HTML will bring up windows that allow changes and saves that will update on the fly. This plugin displays as a FireFox toolbar and (like most Firefox plugins) has no problems during the uninstall process.

Overall, Web Developer is a great tool for developing sites in HTML and CSS. It is also a useful tool when developing AJAX enabled sites, as you can view the generated source. To try Web Developer, visit the developer’s site directly at http://chrispederick.com/work/web-developer/ or from Firefox Extensions at https://addons.mozilla.org/en-US/firefox/addon/60.