<?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>The Bright Lines &#187; first-child</title>
	<atom:link href="http://www.thebrightlines.com/tag/first-child/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thebrightlines.com</link>
	<description>HTML, CSS, Javascript and more</description>
	<lastBuildDate>Thu, 29 Jul 2010 22:44:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hack: how to enable :first-child in IE6</title>
		<link>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/</link>
		<comments>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 00:43:14 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[first-child]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=433</guid>
		<description><![CDATA[How to implement the CSS selector :first-child in IE6 with a little help from Javascript.]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE:</strong> The pure Javascript routine is updated. It is now as fast as jQuery.</p>
<p>One of the things I most dearly miss in IE6 is the lack of support for the CSS selector <code>:first-child</code>. Some shortcomings in browsers can be patched with Javascript. And <code>:first-child</code> is one of them for sure. If you use a Javascript library like jQuery or Prototype it&#8217;s just one line of Javascript:</p>
<pre name="code" class="xhtml:nogutter">&lt;!--[if lte IE 6]&gt;
    &lt;script type="text/javascript"&gt;
        jQuery("*:first-child").addClass("firstChild")
    &lt;/script&gt;
&lt;![endif]--&gt;</pre>
<p>If you don&#8217;t like Javascript libraries you can use this pure js-code:</p>
<pre name="code" class="xhtml:nogutter">
var tags = document.getElementsByTagName("*"); var nTags = tags.length;
for (var i=0;i<nTags;i++) {
	if (tags[i].firstChild) {
		var el=tags[i].firstChild; while ((el.nodeType!=1)&#038;&#038;(el.nextSibling)) el=el.nextSibling;
		if (el.nodeType==1) el.className += " firstChild";
	}
}
</pre>
<p>Now every first child has the class <code>firstChild</code> that can be used in CSS selectors. You'll get something like this:</p>
<pre name="code" class="css:nogutter">
ul li:first-child,
ul li.firstChild {
    font-weight: bold;
}
</pre>
<h2>The speed</h2>
<p>Throwing around classes through the whole HTML tree is not without a price. Certainly not if you keep in mind that Javascript in IE6 is much slower than in Firefox or Safari. So I tested what would happen if the Javascript would run in the homepage of amazon.com, a page with almost a 1000 tags. Here are the results:</p>
<ul>
<li><strong>Adding firstChild with jQuery:</strong>
<ul>
<li>Amazon.com homepage (1000 tags): +- <strong>215</strong>milliseconds</li>
<li>Five times the Amazon.com homepage (5000 tags): +- <strong>742</strong> milliseconds</li>
</ul>
</li>
<li>Adding firstChild with plain Javascript:
<ul>
<li>Amazon.com homepage (1000 tags): +- <strong>129</strong> milliseconds</li>
<li>Five times the Amazon.com homepage (5000 tags): +- <strong>798</strong> milliseconds</li>
</ul>
</li>
</ul>
<p>As you can see there is not much performance difference. Especially when you keep in mind that every time I ran my test page I got different results. With a page of 5000 tags it could run as fast as 481 milliseconds or as slow as 931.</p>
<h2>Code crafting</h2>
<p>The first test I did with the pure Javascript code gave different results because of a inefficient routine. It ran about 1/3 slower in the 1000-tag page and 4 times slower in a 5000-tag page. After consulting a colleague I was able to find a more efficient routine that could match with jQuery.</p>
<h2>Conclusion</h2>
<p>I think a lag of about 200 milliseconds during a page load with a 1000 tags can be acceptable. Especially since a web page of 1000 tags is already a large website to my standards. Most websites I develop have about 300 tags a page at best. If you want to use this hack, I guess it's best to test the script in the largest pages of the website. The gain isn't that spectacular: just support for :first-child. But on the other side it could really make your life as a web developer just a bit easier.</p>
<p>You might be tempted to patch other shortcomings of IE6 with Javascript. Don't start coding, but consider the <a href="http://code.google.com/p/ie7-js/">IE7.js-script</a>. But I must say up front that I don't have good experiences with it. The script works good in small pages with small CSS files, but you'll experience serious performance issues with medium or large websites.</p>
<h2>Putting it in perspective</h2>
<p>Chrome 4, by the way needed just 37 milliseconds to run the Javascript in the 5000-tag page. That's 20 times faster then IE6.</p>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/' rel='bookmark' title='Permanent Link: Alternative for :nth-of-type() and :nth-child()'>Alternative for :nth-of-type() and :nth-child()</a></li>
<li><a href='http://www.thebrightlines.com/2009/10/28/for-the-sloppy-clickers/' rel='bookmark' title='Permanent Link: For the sloppy clickers'>For the sloppy clickers</a></li>
<li><a href='http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/' rel='bookmark' title='Permanent Link: Too advanced CSS selectors'>Too advanced CSS selectors</a></li>
<li><a href='http://www.thebrightlines.com/2009/11/08/how-to-overcome-the-limits-of-css-in-internet-explorer-6/' rel='bookmark' title='Permanent Link: How to overcome the limits of CSS in Internet Explorer 6'>How to overcome the limits of CSS in Internet Explorer 6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Differences between Photoshop web design and HTML. Part 1: line height</title>
		<link>http://www.thebrightlines.com/2009/12/26/differences-between-photoshop-web-design-and-html-part-1-line-height/</link>
		<comments>http://www.thebrightlines.com/2009/12/26/differences-between-photoshop-web-design-and-html-part-1-line-height/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 00:41:15 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Photoshop]]></category>
		<category><![CDATA[first-child]]></category>
		<category><![CDATA[line-height]]></category>
		<category><![CDATA[paragraph]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=293</guid>
		<description><![CDATA[Discusses the different effects when setting the line-height in Photoshop and in HTML. It's something to keep in mind when converting PSD to HTML/CSS.]]></description>
			<content:encoded><![CDATA[<p>Photoshop is *the* tool for designing websites. To illustrate its popularity: In my 10 years in web development I hardly encountered anything else. Attempts to design websites in line drawings always failed. In those few cases a design firm did sent their webdesign in Illustrator or PDF, it would have a fair chance of being converted to Photoshop by hand before any HTML was written.</p>
<p>But like anything Photoshop has its weak points. And since we humans tend to focus on those tiny grains of imperfections I decided to sum up the troubles you can encounter when converting a Photoshop document into to a real website.</p>
<h2>Line height between paragraphs</h2>
<p>One thing that definitely can be confusing is the line height of text. When increasing the line height in Photoshop by setting the &#8216;leading&#8217;, the extra space is added <em>above</em> the text. In HTML That extra space is distributed <em>above and below</em> the text. Below you can see the differences next to each other.</p>
<div class="wp-caption alignleft" style="width: 167px"><img title="A large line-height in Photoshop adds space above the text" src="/article-data/images/Photoshop-1.png" alt="A large line-height in Photoshop adds space above the text" width="157" height="103" /><p class="wp-caption-text">A large line-height in Photoshop adds space above the text</p></div>
<div class="wp-caption alignleft" style="width: 167px"><img title="A large line-height in HTML/CSS adds space above and below the text" src="/article-data/images/Photoshop-2.png" alt="A large line-height in HTML/CSS adds space above and below the text" width="157" height="103" /><p class="wp-caption-text">A large line-height in HTML/CSS adds space above and below the text</p></div>
<div class="wp-caption alignleft" style="width: 223px"><img title="Activate this window in the menu throught Window &gt; Character" src="/article-data/images/Photoshop-3.png" alt="Activate this window in the menu throught Window &gt; Character" width="213" height="214" /><p class="wp-caption-text">Activate this window in the menu throught Window &gt; Character</p></div>
<p>Many designers use line-height to create paragraphs. So if you want to know how much margin you have to set on paragraphs, you have to look at line-height of the first line in a paragraph. In short: <strong>Line-height of the first line &#8211; default line-height = the margin between paragraphs</strong>. That&#8217;s why I never look at the line height of the first line of text in a paragraph in Photoshop.</p>
<h2>Line height of the first line in a text box</h2>
<p>Another thing to be aware of is that the line-height has <em>absolutely no effect in the first line of a text box</em>. So it doesn&#8217;t matter if you set the line-height to 20px or 40px. No extra white space is added on top of the text. HTML does not follow Photoshop&#8217;s behaviour.</p>
<p>Because of that you have to be aware that different styles with different line heights in a website can push the whole text box up or down. That&#8217;s especially important when the first line of a text box should align with some box or image on the left or right as you can see in the examples below.</p>
<div class="wp-caption alignleft" style="width: 260px"><img title="The solid box and text box are aligned (line-height: 20px)." src="/article-data/images/Photoshop-4.png" alt="The solid box and the text box are aligned (line-height: 20px)." width="250" height="105" /><p class="wp-caption-text">The solid box and the text box are aligned (line-height: 20px).</p></div>
<div class="wp-caption alignleft" style="width: 260px"><img title="...but when a heading is added with 40px line-height it does not align anymore." src="/article-data/images/Photoshop-5.png" alt="...but when a heading is added with 40px line-height it does not align anymore." width="250" height="105" /><p class="wp-caption-text">...but when a heading is added with 40px line-height it does not align anymore.</p></div>
<p>You could fix the problem above with this CSS:</p>
<pre class="css:nogutter">.textBox h1:first-child {
    margin-top: -10px; /* only half of the extra line-height has to be compensated */
    font-size: 20px;
    line-height: 40px;
}</pre>
<p>The <code>first-child</code> selector ensures us that the negative margin is only used on something that&#8217;s on the very top of the text-box. As already said, the line-height is distributed above and below the text. So we only have to compensate for the extra space on the upper part of the line. So:</p>
<p><strong>40px</strong> (line-height) <strong>- 20px</strong> (font-size) <strong>= 10px</strong> (the value for our negative top margin).</p>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2010/01/07/do-you-create-your-design-in-photoshop-or-notepad/' rel='bookmark' title='Permanent Link: Do you create your design in Photoshop or Notepad?'>Do you create your design in Photoshop or Notepad?</a></li>
<li><a href='http://www.thebrightlines.com/2009/11/05/3-tips-for-making-css-sprites-in-photoshop/' rel='bookmark' title='Permanent Link: 3 Tips for making CSS sprites in Photoshop'>3 Tips for making CSS sprites in Photoshop</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2009/12/26/differences-between-photoshop-web-design-and-html-part-1-line-height/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
