<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cameron McKay &#187; html</title>
	<atom:link href="http://cdmckay.org/blog/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Tue, 11 Oct 2011 05:05:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>JSS 1.0.0-beta2 released!</title>
		<link>http://cdmckay.org/blog/2009/05/24/jss-100-beta-2-released/</link>
		<comments>http://cdmckay.org/blog/2009/05/24/jss-100-beta-2-released/#comments</comments>
		<pubDate>Mon, 25 May 2009 04:20:08 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[1.0.0-beta2]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=591</guid>
		<description><![CDATA[I&#8217;m proud to announce the release of JSS 1.0.0-beta2. Here are the changes: Fixed the way that {attributes} are handled when not using a selector. They should now properly grab the attribute from element that fired the event. Made it so each time a declaration block is applied, all the event handlers bound by JSS [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m proud to announce the release of <a href="http://javascript-stylesheets.googlecode.com/files/jquery.jss-1.0.0-beta2.js">JSS 1.0.0-beta2</a>.  Here are the changes:</p>
<ul>
<li>Fixed the way that {attributes} are handled when not using a selector.  They should now properly grab the attribute from element that fired the event.</li>
<li>Made it so each time a declaration block is applied, all the event handlers bound by JSS to the elements returned by that selector are unbound.</li>
<li>Fixed the semantics of the {@attr} syntax.  It now means that the eventTarget should always be used, regardless of whether or not a selector is defined.  In order to access the attributes of a selector use {@attr} now.</li>
<li>Added two new commands: set-data and remove-data.  These commands use the jQuery functions data() and removeData().  They are similar to set-attr and remove-attr except instead of storing the information in an attribute, they are stored in a table.  Data is accessed by using [data] syntax, similar to the {attr} syntax.</li>
<li>Added new event property: setup.  This event is fired when the JSS stylesheet is loaded (setup).  This is useful in certain cases where the user wants to setup some variables using set-data.</li>
<li>Made it so you can now set scroll-top and scroll-left via the set-attr command.</li>
<li>Added trigger command.  It can be used to trigger event properties on elements.</li>
<li>Fixed bug where you could put a string | in a binary command due to the<br />
simplistic implementation.  The new version requires any | to be escaped with \|.</li>
<li>Added fade-to command, based on the jQuery.fadeTo() command.</li>
<li>Added show and hide commands.</li>
<li>Fixed issue with data and attribute replacements not working correctly (i.e. the first event would replace them for all events).</li>
<li>Callbacks may now take string parameters.  For example, &#8220;fade-out (#target) normal !callback str1 str2&#8243;.</li>
</ul>
<p>I&#8217;ve also started writing documentation for the <a href="http://cdmckay.org/blog/jss/command-reference/">JSS commands</a>.  It is only partially completed but can still be helpful.  Check out the <a href="http://cdmckay.org/blog/jss">JSS homepage</a> for other documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/05/24/jss-100-beta-2-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The JSS Stylesheet, Part 1</title>
		<link>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/</link>
		<comments>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/#comments</comments>
		<pubDate>Tue, 05 May 2009 16:05:40 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[event properties]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[stylesheets]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=459</guid>
		<description><![CDATA[I&#8217;ve said previously that JSS is a lot like CSS. What is CSS? Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language. If that blurb were written about JSS, it&#8217;d sound a little like this: JavaScript [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve said previously that JSS is a lot like <a href="http://en.wikipedia.org/wiki/CSS">CSS</a>.  What is CSS?</p>
<blockquote><p>Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language.</p></blockquote>
<p>If that blurb were written about JSS, it&#8217;d sound a little like this:</p>
<blockquote><p>JavaScript Style Sheets (JSS) is a style sheet language used to describe the presentation (that is, the look and formatting) and functionality of a document written in HTML.</p></blockquote>
<p>In this article, we will look at the syntactic structure of a JSS stylesheet.  We will also talk briefly about including JSS stylesheets in your HTML documents.</p>
<p><span id="more-459"></span></p>
<p>At this point, some people may be wondering: <em>aren&#8217;t we mucking up CSS by combining functionality and presentation in the same place?</em>  This is a valid concern.  However, just because we can combine presentation and functionality in JSS, doesn&#8217;t necessarily mean we should.  A good JSS site will have two or more JSS stylesheets: one for JSS styles (if they are needed) and one for JSS event properties.  JSS event properties will be covered in a future article.</p>
<h3>Syntactic Structure</h3>
<p>Let&#8217;s get back to the CSS/JSS connection.  What do people do with CSS?  They write stylesheets.  More specifically, they write rules.  In JSS, we write rules too.  In fact, we write them in a very similar format to CSS, although with some important differences owing to the fact that JSS stylesheets are actually JavaScript programs.  Here&#8217;s the basic format of a CSS stylesheet:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">rule-selector-a
<span style="color: #00AA00;">&#123;</span>
  property-a<span style="color: #00AA00;">:</span> value-a<span style="color: #00AA00;">;</span>
  property-b<span style="color: #00AA00;">:</span> value-b<span style="color: #00AA00;">;</span>
  ...
<span style="color: #00AA00;">&#125;</span>
&nbsp;
rule-selector-b
<span style="color: #00AA00;">&#123;</span>
  property-a<span style="color: #00AA00;">:</span> value-a<span style="color: #00AA00;">;</span>
  property-b<span style="color: #00AA00;">:</span> value-b<span style="color: #00AA00;">;</span>
  ...
<span style="color: #00AA00;">&#125;</span>
&nbsp;
...</pre></div></div>

<p>In JSS, we use pretty much the same format, with the differences being the addition of boilerplate code (1), quoted selectors with colons (2), underscores instead of dashes in property names (3), quoted values with commas (4) and commas between rules (5).  The JSS format is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// (1)</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// (2)</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// (3) and (4)</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span>
  ...
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// (5)</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span>
  ...
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// (1)</span></pre></div></div>

<p>Let&#8217;s talk a bit about each of these difference:</p>
<ol>
<li><strong>Boilerplate code.</strong>  This is an unfortunate side effect of having to use JavaScript to extend CSS.  Fortunately, it&#8217;s the same for each JSS stylesheet, so it&#8217;s not too much of a hassle to remember.</li>
<li><strong>Quoted selectors with colons.</strong> Since the selector portion of a JSS rule is a string, it must be represented in JSS as a JavaScript string.  All strings in JavaScript must be either single-quoted (as in &#8216;Hello!&#8217;) or double-quoted (as in &#8220;w00t w00t&#8221;).  The colon following the selector is a necessity as internally, JSS selectors act as keys in a JavaScript object.  A JSS selector may be any valid jQuery selector.</li>
<li><strong>Underscores instead of dashes in property names.</strong>  This is not strictly true, although using underscores instead of dashes is the recommended way of representing JSS properties.  Consider, for example, the <em>font-family</em> property from CSS.  This can be represented in 3 ways in JSS:
<ol>
<li>font_family</li>
<li>&#8220;font_family&#8221;</li>
<li>&#8220;font-family&#8221;</li>
</ol>
</li>
<li><strong>Quoted values with commas.</strong> Unlike CSS, all values in JSS are quoted with either single or double quotes.  Furthermore, between declarations, there must be a comma.<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// This is correct.</span>
<span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This is incorrect.</span>
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// This declaration should not </span>
                         <span style="color: #006600; font-style: italic;">// have a comma.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>Commas between rules.</strong> Like declarations, each rule must have a comma between it:

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// Make sure the last rule has no comma after it.</span></pre></div></div>

</li>
</ol>
<h3>Loading JSS and JSS stylesheets</h3>
<p>Now that we understand the syntactic structure of a JSS stylesheet, it&#8217;s time to take look at how we include them in an HTML file.  Every page that makes use of JSS will have the following structure:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Title<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-1.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-2.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    ...
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-n.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Your website here. --&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Let&#8217;s quickly go through the components here.  The first <code>script</code> tag includes the jQuery library, which is required by JSS.  The second <code>script</code> tag includes the JSS jQuery plugin, which is the brains of JSS, and is required for any JSS stylesheet to work.  All the <code>script</code> tags after that are the JSS stylesheets that you wish to load.</p>
<p>As you can see, you can add an unlimited number of JSS stylesheets, and when the page loads, they&#8217;ll be applied in the same order they are included in the page.  Keep in mind, however, that if you&#8217;re including many <code>script</code> tags on a single webpage, it is advisable to use some sort of JavaScript loader.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing JSS</title>
		<link>http://cdmckay.org/blog/2009/04/30/introducing-jss/</link>
		<comments>http://cdmckay.org/blog/2009/04/30/introducing-jss/#comments</comments>
		<pubDate>Fri, 01 May 2009 04:43:34 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ie7]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[stylesheet]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=391</guid>
		<description><![CDATA[Ever wanted to make an image rollover, but you don&#8217;t know JavaScript? In this tutorial, we show you how JSS, a CSS-like language, makes this a breeze. Consider these two images: Imagine we wanted to make them &#8220;rollover&#8221;, i.e. the zebra should be shown when nothing is happening, and the shark should be showed when [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to make an image rollover, but you don&#8217;t know JavaScript?  In this tutorial, we show you how JSS, a CSS-like language, makes this a breeze.</p>
<p><span id="more-391"></span></p>
<p>Consider these two images:</p>
<p><img src="http://couchware.ca/users/cam/example/rollover/image_off.gif" alt="Zebra" /><br />
<img src="http://couchware.ca/users/cam/example/rollover/image_on.gif" alt="Shark" /></p>
<p>Imagine we wanted to make them &#8220;rollover&#8221;, i.e. the zebra should be shown when nothing is happening, and the shark should be showed when the mouse is &#8220;rolled over&#8221; (i.e. on top of) the zebra.  Like this:</p>
<p><img src="http://couchware.ca/users/cam/example/rollover/image_off.gif" src-out="http://couchware.ca/users/cam/example/rollover/image_off.gif" src-over="http://couchware.ca/users/cam/example/rollover/image_on.gif" /><script src="http://www.google.com/jsapi" type="text/javascript"></script> <script type="text/javascript"><!--
google.load("jquery", "1.3.2");
google.setOnLoadCallback(function()
{
  $("img[src-over]").hover(
    function() { $(this).attr('src', $(this).attr('src-over')); },
    function() { $(this).attr('src', $(this).attr('src-out')); }
  );
});
// --></script></p>
<p>Before we start writing some JSS, let&#8217;s write some HTML for a page just showing the rollover at rest (i.e. just show a plain old static image):</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>If we copied this code into an HTML file and ran it on our browser, we would see a static image of a zebra.  Not that exciting, right?  Now let&#8217;s see how we can use JSS to make this zebra &#8220;rollover&#8221; to the shark when the mouse is on it.  Here&#8217;s the JSS stylesheet:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s step through this syntax, because there&#8217;s a lot going on here, even though it&#8217;s a single line.  First, let&#8217;s annotate the JSS source so we can refer to it easier:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// 1. Boilerplate that starts any JSS stylesheet.</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// 2. Rule</span>
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// 3. CSS Selector</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// 4. JSS Declaration</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you might have noticed, a JSS stylesheet looks very similar to a CSS stylesheet.  Let&#8217;s walk through the anatomy of a JSS stylesheet.</p>
<p>At the top of any JSS stylesheet there is some boilerplate code (1).  This is necessary because a JSS stylesheet is actually a JavaScript program.  However, beyond the fact that this boilerplate needs to be in every JSS stylesheet, you don&#8217;t really need to know what it does.</p>
<p>A JSS stylesheet is made up of an unlimited number of rules (2).  This is the same as in a CSS stylesheet.  Inside a JSS rule, there are two components, a selector (3) and an unlimited number of declarations (inside a declaration block) (4).  This is where we start to see some of the advantages of JSS over using plain CSS.</p>
<p>JSS selectors are powered by <a href="http://jquery.com">jQuery</a>.  As such, JSS supports any CSS3-compliant selector.  In addition, JSS also allows you to use any of the jQuery CSS selector extensions as well.</p>
<p>For example, in CSS, this stylesheet should stripe all the tables on the page as alternating gray and white:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">table tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>odd<span style="color: #00AA00;">&#41;</span>
<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
table tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>even<span style="color: #00AA00;">&#41;</span>
<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>However, if you tried to use that CSS stylesheet in the two most popular browsers available (IE7 and Firefox 3) you would discover that it didn&#8217;t work.  On the other hand, if you use the following JSS stylesheet&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;table tr:nth-child(odd)&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span> background_color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#eee&quot;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;table tr:nth-child(even)&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span> background_color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#fff&quot;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;you would discover that it worked in all major browsers.  By using a JavaScript selector engine, JSS offers better cross-browser styling than regular CSS.</p>
<p><em>(Aside: Before all you web developers out there go crazy, I would like to note that I realize there are several drawbacks to using JavaScript to style your page.  Stay tuned to the end of this article for a discussion of them).</em></p>
<p>Now let&#8217;s return to our task of making a rollover.  How does the JSS stylesheet make it possible?  Let&#8217;s take a look at that JSS stylesheet again:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// 1. Boilerplate that starts any JSS stylesheet.</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// 2. Rule</span>
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// 3. CSS Selector</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// 4. JSS Declaration</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s look closer at the rule.  More specifically, take a look at the CSS selector.  For anyone familiar CSS, it reads &ldquo;select all <strong>img</strong> tags that have the attribute <strong>src-over</strong>&rdquo;.  Why would we want to do that?  Well, as it happens, we need some way to describe which image to use when the user mouses over our image.  In order to do that, we&#8217;re going to use something called a <strong>custom attribute</strong>.</p>
<p>Custom attributes are user-defined attributes on HTML tags.  For example, the <strong>img</strong> tag has an attribute called <strong>src</strong>.  That&#8217;s a proper attribute that is defined in a W3C recommendation.  What we&#8217;re going to do is add two new attributes, <strong>src-over</strong> and <strong>src-out</strong>, that are not part of any W3C standard.  We just made them up because they&#8217;re convenient for our purposes.</p>
<p>Now that we&#8217;ve decided that we&#8217;re adding these two attributes, we need to add them to our HTML file:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span></span>
<span style="color: #009900;">        src-out<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> src-over<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_on.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>With that done, let&#8217;s take a look at the last part of the JSS stylesheet: the declaration.  A JSS declaration has a property (hover) and a value (&#8220;set-attr src {src-over} | set-attr src {src-out}&#8221;).  This is just like a CSS declaration, like &#8220;font-family: Arial, sans-serif&#8221;.  In that case, &#8220;font-family&#8221; is the property and &#8220;Arial, sans-serif&#8221; is the value.</p>
<p>In the case of the rollover JSS stylesheet, the hover property must have a value with the following structure:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jss-expression | jss-expression&quot;</span></pre></div></div>

<p>The first JSS expression on the left is run when the mouse is over the image area, and the right JSS expression is run when the mouse leaves the image area.</p>
<p>Let&#8217;s look at what happens in the JSS rollover stylesheet when the mouse is brought over the image area:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">set<span style="color: #339933;">-</span>attr src <span style="color: #009900;">&#123;</span>src<span style="color: #339933;">-</span>over<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This JSS expression reads: &#8220;set the src attribute (of the selected element) to the contents of the src-over attribute (of the selected element)&#8221;.  This is because &#8220;set-attr&#8221; is a JSS <em>command</em>.  The &#8220;set-attr&#8221; command takes two arguments: the attribute to change, and the value to change it to.  That leaves one more question.  What&#8217;s the deal with the curly braces?</p>
<p>In JSS, curly braces are basically a &#8220;get-attr&#8221; command.  They fetch the value of an attribute.  In the context they are being used here, they fetch the value of the &#8220;src-over&#8221; attribute (which happens to be the name of the shark image &#8220;image_on.gif&#8221;).</p>
<p>With that information in hand, it can be deduced that the right hand side&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">set<span style="color: #339933;">-</span>attr src <span style="color: #009900;">&#123;</span>src<span style="color: #339933;">-</span>out<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8230;switches the src attribute back to src-out image URL when the mouse leaves the image area.</p>
<p>Thus, if we put it all together, and include the appropriate JavaScript files, we have the following.  In the &#8220;index.html&#8221; file we have:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'jquery.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'jquery.jss.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'styles.jss.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span></span>
<span style="color: #009900;">        src-off<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> src-over<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_on.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>And in the &#8220;styles.jss.js&#8221; file we have this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>These files together create a page that has a rollover that changes from zebra to shark when the mouse is hovered over it.  Better yet, this JSS stylesheet can be used with any web page to enable rollover images.  All the user has to do is set the &#8220;src-over&#8221; and &#8220;src-out&#8221; custom attributes.</p>
<h3>Notes on JSS Availability &amp; Performance</h3>
<p>JSS applies its stylesheets using JavaScript.  This has several implications to web designers using it.  First of all, in order for a site to take advantage of JSS, JavaScript must be enabled.  Fortunately, something like 95% of all web surfers have JavaScript enabled, so that&#8217;s not too big of deal.  Secondly, JSS is a lot slower than CSS.  For most websites, this difference will probably be imperceptible deal, but it&#8217;s still something to be aware of.</p>
<p>My recommendations would be this: if you can do it in CSS, and it works properly across all browsers, then use CSS.  There&#8217;s no reason not to.  However, if you need to use JSS features like event properties (like hover) or cross-browser selectors, then use JSS.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/04/30/introducing-jss/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating an HTML document with .NET</title>
		<link>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/</link>
		<comments>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 08:15:09 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[visual basic]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=43</guid>
		<description><![CDATA[Sometimes when you&#8217;re programming, you&#8217;ll want to generate an HTML document. This may be because you&#8217;re writing some sort of web app, or maybe you want your program to be able to output in a web-friendly format. The String Concatenation Approach Whatever your reason is for generating HTML, the best bet is your first prototype [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when you&#8217;re programming, you&#8217;ll want to generate an HTML document.  This may be because you&#8217;re writing some sort of web app, or maybe you want your program to be able to output in a web-friendly format.</p>
<p><span id="more-43"></span></p>
<h3>The String Concatenation Approach</h3>
<p>Whatever your reason is for generating HTML, the best bet is your first prototype looked something like this:</p>
<p><em>(Aside: All this code is in VB.NET&#8230; the original VB.NET (i.e. not 2005 or later).  It also assumes ASP.NET 1.1.  This is because the client I work for still uses this technology, so all my experience is with it.  The code should still be fine though, as all versions of .NET after 1.1 are more or less backwards compatible.)</em></p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #008000;">' Run some query on the database.</span>
<span style="color: #990099; font-weight: bold;">Dim</span> dataTable <span style="color: #330066;">As</span> DataTable <span style="color: #006600; font-weight: bold;">=</span> Query<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color: #008000;">' Dump it into an HTML document.</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;html&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;head&gt;&lt;title&gt;Database Table&lt;/title&gt;&lt;/head&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span>Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;body&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;table&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color: #008000;">' Print the rows.</span>
<span style="color: #990099; font-weight: bold;">For</span> <span style="color: #990099; font-weight: bold;">Each</span> row <span style="color: #330066;">As</span> DataRow <span style="color: #990099; font-weight: bold;">In</span> dataTable
  <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;tr&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #008000;">' Print the cells.</span>
  <span style="color: #990099; font-weight: bold;">For</span> <span style="color: #990099; font-weight: bold;">Each</span> item <span style="color: #330066;">As</span> Object <span style="color: #990099; font-weight: bold;">In</span> row
    <span style="color: #990099; font-weight: bold;">If</span> IsDBNull<span style="color: #006600; font-weight:bold;">&#40;</span>item<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">False</span> <span style="color: #990099; font-weight: bold;">Then</span>
      <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;td&gt;&quot;</span> + <span style="color: #990099; font-weight: bold;">CStr</span><span style="color: #006600; font-weight:bold;">&#40;</span>item<span style="color: #006600; font-weight:bold;">&#41;</span> + <span style="color: #cc0000;">&quot;&lt;/td&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">Else</span>
      <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">If</span>
  <span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">For</span>
  <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/tr&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">For</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/table&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/body&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/html&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Now don&#8217;t get me wrong, this code will do the trick.  But as you write it, you start to feel a bit dirty.  HTML is a language for representing hierarchies, yet you&#8217;re concatenating strings.  You start to feel like there&#8217;s a more elegant way to do it, a way that takes advantage of the structure of HTML.</p>
<p>And there is.</p>
<h3>The Programmatic Approach</h3>
<p>Ok, so we want to take an approach that has some knowledge of the HTML we&#8217;re writing.  Enter XML.  .NET uses XML for, among other things, it&#8217;s <a href="http://en.wikipedia.org/wiki/Javadoc">Javadoc</a>-like documentation generator and it&#8217;s ASP.NET config files.  With XML so prevalent in .NET, it&#8217;s not surprising to discover that it&#8217;s part of the standard library, in the namespace <code>System.Xml</code>.</p>
<p>So you&#8217;re thinking &#8220;Cameron, who cares about XML, I want to make an HTML document!&#8221;  Gear down, sugar plum, XML can still help!  As you may or may not know, HTML (at least the XHTML dialects) is a reformulation of XML.  That is, an XHTML document is an XML document.  As a result, we can use .NET&#8217;s built-in XML support to generate an HTML document.</p>
<p>Thus, if we want to write the same code in previous example in .NET XML, we would do the following.  First, we would need to make a new XML document.  That&#8217;s easy enough:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Xml</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create a new XML document.</span>
<span style="color: #0600FF;">Dim</span> xmlDoc <span style="color: #FF8000;">As</span> XmlDocument <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> XmlDocument</pre></div></div>

<p>Now we want to start writing some HTML.  So let&#8217;s create our <code>html</code> tag and add some headers.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">' Create the html tag.</span>
<span style="color: #0600FF;">Dim</span> xmlRoot <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;html&quot;</span><span style="color: #000000;">&#41;</span>
xmlDoc.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlRoot<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the head tag and append it under the html element.</span>
<span style="color: #0600FF;">Dim</span> xmlHead <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;head&quot;</span><span style="color: #000000;">&#41;</span>
xmlRoot.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlHead<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the title tag, set it's text to &quot;Database Table&quot;</span>
<span style="color: #008080; font-style: italic;">' and append it under the head element.</span>
<span style="color: #0600FF;">Dim</span> xmlTitle <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;title&quot;</span><span style="color: #000000;">&#41;</span>
xmlTitle.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">CreateTextNode</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Database Table&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
xmlHead.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlTitle<span style="color: #000000;">&#41;</span></pre></div></div>

<p>Wow, that&#8217;s a lot of code.  What&#8217;s going on there?  First, let&#8217;s look at the <code>XmlDocument</code> class.  This is important when we&#8217;re generating XML documents.  It&#8217;s what we use to create new elements.  However, even though it creates new elements, it doesn&#8217;t add them to the document.  Thus, we need to use the <code>AppendChild</code> method of the appropriate element to add it to the document.  As you can see above, we created a root element (<code>html</code>) and added that to the XML document (<code>xmlDoc</code>) first.  Then we created a <code>head</code> element, and added that to the root element, not the <code>xmlDoc</code>.  This was done in order to form a hierarchy, which is, in essence, what a XML (and by extension HTML) document is.</p>
<p>Here is what the resulting XML/XHTML document would look like so far:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Database Table<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So far so good, let&#8217;s finish it off.  Here&#8217;s the rest of the code we&#8217;d need to make our simple HTML document:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">' Create the body element and append it to the root.</span>
<span style="color: #0600FF;">Dim</span> xmlBody <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;body&quot;</span><span style="color: #000000;">&#41;</span>
xmlRoot.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlBody<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the table and append it.</span>
<span style="color: #0600FF;">Dim</span> xmlTable <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;table&quot;</span><span style="color: #000000;">&#41;</span>
xmlBody.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlTable<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the rows.</span>
<span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> row <span style="color: #FF8000;">As</span> DataRow In dataTable
  <span style="color: #0600FF;">Dim</span> xmlRow <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;tr&quot;</span><span style="color: #000000;">&#41;</span>
  xmlTable.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlRow<span style="color: #000000;">&#41;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">' Create the cells.</span>
  <span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> item <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span> In row
    <span style="color: #0600FF;">Dim</span> content <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
    <span style="color: #0600FF;">If</span> IsDBNull<span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span> <span style="color: #FF8000;">Then</span> content <span style="color: #008000;">=</span> <span style="color: #0600FF;">CStr</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Dim</span> xmlCell <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">createElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;td&quot;</span><span style="color: #000000;">&#41;</span>
    xmlCell.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">createTextNode</span><span style="color: #000000;">&#40;</span>content<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    xmlRow.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlCell<span style="color: #000000;">&#41;</span>
  <span style="color: #0600FF;">End</span> <span style="color: #FF8000;">For</span>
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">For</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Write the XML/HTML document.</span>
Response.<span style="color: #0600FF;">Write</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">OuterXml</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>And there you have it: a programmatic way to generate an HTML document.  So why bother?  Here&#8217;s a few reasons:</p>
<p><strong>Safety</strong>.  If you generate your HTML document by concatenating strings, there&#8217;s a chance you might forget to close a tag.  If you generate the document programmatically, you can ensure that all your tags will always be closed.</p>
<p><strong>Maintainability and readability</strong>.  I&#8217;ve had to deal with code where the HTML is generated by concatenation.  It&#8217;s confusing and frustrating.  If you generate your HTML documents programmatically, it&#8217;s a lot easier for a future programmer to follow what&#8217;s going on (especially if you, y&#8217;know, document it).</p>
<p><strong>Reusability and modifiability</strong>.  If you create your HTML document as a flat string, it makes it really hard to modify and reuse elsewhere in your code.  For example, if you wanted to add a tag in the middle of your HTML document using the string concatenation method, you will have to do a lot of painful string manipulations.  With the programmatic approach, it&#8217;s as simple as just calling <code>AppendChild</code> on the appropriate <code>XmlElement</code>.</p>
<p>Using an <code>XmlDocument</code> to represent an XML document just makes sense.  It&#8217;s less crude and more versatile.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Making tables print pretty in IE, part 2</title>
		<link>http://cdmckay.org/blog/2009/01/29/making-tables-print-pretty-in-ie-part-2/</link>
		<comments>http://cdmckay.org/blog/2009/01/29/making-tables-print-pretty-in-ie-part-2/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 23:00:52 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[ie6]]></category>
		<category><![CDATA[ie7]]></category>
		<category><![CDATA[printing]]></category>
		<category><![CDATA[tables]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=29</guid>
		<description><![CDATA[In my last post, I mentioned that I had come up with a way to prevent tables in Internet Explorer 6 and 7 from looking like this. In this post, I&#8217;ll show you how to do it. (Disclaimer: This method has been found to break if the a cell has a line break in it [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://cdmckay.org/blog/2009/01/28/making-tables-print-pretty-in-ie/">post</a>, I mentioned that I had come up with a way to prevent tables in Internet Explorer 6 and 7 from looking like <a href="http://www.xefteri.com/articles/content/26/images/table_footer_issue.gif" rel="lightbox[29]">this</a>.  In this post, I&#8217;ll show you how to do it.</p>
<p><strong>(Disclaimer: This method has been found to break if the a cell has a line break in it or is wrapped to a second line).</strong></p>
<p><span id="more-29"></span></p>
<p>First, let&#8217;s start with an example table:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example-table&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>First Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Last Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Client #<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Joe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Shmoe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>123451<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Joe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Shmoe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>123451<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Joe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Shmoe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>123451<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Joe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Shmoe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>123451<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Repeat these many times to make it go to a second page. --&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></div></div>

<p>Add a stylesheet like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#example-table</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-collapse</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">collapse</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#example-table</span> th<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#example-table</span> td
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>If you repeat those rows enough times you can get the graphical glitch mentioned above.  Now, to fix it, what we&#8217;ll do is add a <code>&lt;div&gt;</code> tag to every cell and then make it provide the side borders.</p>
<p>So first, we&#8217;ll modify the table as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example-table&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>First Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Last Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Client #<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cell&quot;</span>&gt;</span>Joe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cell&quot;</span>&gt;</span>Shmoe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;last-col&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cell&quot;</span>&gt;</span>123451<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>    
    <span style="color: #808080; font-style: italic;">&lt;!-- Repeat this many times to make it go to a second page. --&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></div></div>

<p>What we&#8217;ve done here is added a <code>&lt;div&gt;</code> to each cell and given these <code>&lt;div&gt;</code>&#8216;s a class called <code>cell</code>.  This is so we can do stuff to them in our CSS stylesheet.  We&#8217;ve also given the last cell in each row the class <code>last-col</code>.  This is so we can identify the last column of each row.  The reasoning for this will be explained shortly.</p>
<p>Now, as I&#8217;ve said, our goal here is to replace the <code>table</code>-provided vertical borders (the | ones) with the <code>div</code>-provided ones.  So we need to modify the stylesheet to reflect that:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#example-table</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-collapse</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">collapse</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#example-table</span> th<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#example-table</span> td
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>There&#8217;s also an issue of padding.  By default, tables are slightly padded, which is fine for most circumstances.  However, in this case, padding would be counterproductive, it would prevent the <code>div</code>&#8216;s border from matching up with the <code>table</code>&#8216;s border.  So that&#8217;ll have to be changed:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#example-table</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-collapse</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">collapse</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#example-table</span> th<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#example-table</span> td
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Time for some <code>div</code> styling.  Since the <code>div</code> is our new cell, we need to style it as such.  First thing, let&#8217;s make the <code>div</code> provide us with sides.  Now, we could do something like <code>border-left: 1px black solid; border-right: 1px black solid;</code>.  This would look fine, except that all the vertical borders in the middle of the table would 2 pixels wide (think about it).  So, instead, we&#8217;ll make all the <code>div</code> cells provide borders on the left side, except for the last column, which&#8217;ll provide borders on both sides (hence the purpose of the <code>last-col</code> class).</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.cell</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#example-table</span> <span style="color: #6666ff;">.last-col</span> div<span style="color: #6666ff;">.cell</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>We also need to add back the padding we stripped out from the table <code>td</code> tags:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.cell</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#example-table</span> <span style="color: #6666ff;">.last-col</span> div<span style="color: #6666ff;">.cell</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>And there you have it.  Now when you load up print preview, your tables should look pretty.  Here&#8217;s the final stylesheet in case you got lost:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* So the headers will display on every page. */</span>
thead <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">table-header-group</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Make table only provides vertical border. */</span>
<span style="color: #cc00cc;">#example-table</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-collapse</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">collapse</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 
 * Make header and normal cells only show top and bottom border,
 * and remove all padding so the div lines will match up with the table.
 */</span>
<span style="color: #cc00cc;">#example-table</span> th<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#example-table</span> td
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Make the div cell only provide the left border, and simulate table padding. */</span>
div<span style="color: #6666ff;">.cell</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Provide the right border in the last column. */</span>
<span style="color: #cc00cc;">#example-table</span> <span style="color: #6666ff;">.last-col</span> div<span style="color: #6666ff;">.cell</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #000000; font-weight: bold;">black</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/01/29/making-tables-print-pretty-in-ie-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making tables print pretty in IE, part 1</title>
		<link>http://cdmckay.org/blog/2009/01/28/making-tables-print-pretty-in-ie/</link>
		<comments>http://cdmckay.org/blog/2009/01/28/making-tables-print-pretty-in-ie/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 22:03:54 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[printing]]></category>
		<category><![CDATA[tables]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=17</guid>
		<description><![CDATA[So I&#8217;m currently doing a side job developing a web application to help pay for Wezzle and, y&#8217;know, food and shelter. Basically, I&#8217;m making a system that displays tables grabbed from an Oracle database using elaborate queries.  That&#8217;s the easy part.  These tables need to be printed out.  That&#8217;s the hard part. We&#8217;ll talk about [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m currently doing a side job developing a web application to help pay for Wezzle and, y&#8217;know, food and shelter.</p>
<p>Basically, I&#8217;m making a system that displays tables grabbed from an Oracle database using elaborate queries.  That&#8217;s the easy part.  These tables need to be printed out.  That&#8217;s the hard part.  We&#8217;ll talk about that here, specifically in relation to IE7 (the hacks here might not work for Firefox or Chrome).</p>
<p><strong>(Disclaimer: This method has been found to break if the a cell has a line break in it or is wrapped to a second line).</strong></p>
<p><span id="more-17"></span></p>
<p>CSS has a mechanism for handling printing.  When we link in a stylesheet with the <code>&lt;link&gt;</code> tag, we have the option to set an attribute for its media type.</p>
<p>For example, here&#8217;s an HTML snippet that would define two stylesheets:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;styles/style.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;print&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;styles/style.print.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>The main thing to notice here is that <code>media='screen'</code> is the stylesheet for the browser, and <code>media='print'</code> is the stylesheet for the printer.</p>
<p>Why is this useful? Well, typically there are many things we want to show in a browser window but we don&#8217;t want to print on a page.  If we&#8217;re going to print a table (and only a table) we&#8217;ll need to hide our navigation bar, buttons, and rotating skull <code>.gifs</code>. To do that, we need to go through our print stylesheet and add in <code>display='none'</code> for all the elements that we don&#8217;t want shown.</p>
<p>Once we&#8217;ve hidden all the non-table elements, it&#8217;s time to see how our page would look printed.  So we load up print preview and <a href="http://www.xefteri.com/articles/content/26/images/base_table.gif" rel="lightbox[17]">take a look</a>.  Ugh, the table headers show only on the first page, but not on the ensuing pages (example ), even though we&#8217;re using the <code>&lt;thead&gt;</code> tag.  Fortunately for us, this is easily corrected with the following CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">thead <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">table-header-group</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>With the heading problem fixed, we look again at the print preview.  Everything looks a lot nicer, except for  this really <a href="http://www.xefteri.com/articles/content/26/images/table_footer_issue.gif" rel="lightbox[17]">ugly looking graphical problem</a> with multi-page tables.  One solution, suggested by <a href="http://www.xefteri.com/articles/show.cfm?id=26">Xefteri</a>, is to do away with the vertical lines in your table.</p>
<p>I don&#8217;t think that&#8217;s good enough.  So I came up with a better (albeit slower) solution using <code>&lt;div&gt;</code> tags within each of the cells and using them to put the vertical lines into the table.  This will be explained in further detail in a future post.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/01/28/making-tables-print-pretty-in-ie/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

