<?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; CSS</title>
	<atom:link href="http://www.thebrightlines.com/tag/css/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>CSS debugging</title>
		<link>http://www.thebrightlines.com/2009/10/22/css-debugging/</link>
		<comments>http://www.thebrightlines.com/2009/10/22/css-debugging/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 20:21:16 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=8</guid>
		<description><![CDATA[Some text in your website has the wrong color or a box has more padding than it ought to have. Styles may pop up in the wrong places. No big deal, that happens all the time. But some times it can get hairy. Especially with unstructured CSS. So where to start debugging?]]></description>
			<content:encoded><![CDATA[<h2>Trace the styles</h2>
<p>What a relief when the CSS debuggers emerged. Before <a href="http://getfirebug.com/">Firebug</a> or IE&#8217;s <a href="http://en.wikipedia.org/wiki/Internet_Explorer_Developer_Toolbar">developer toolbar</a> we had to just <em>guess</em> what the problem was. Now we can see exactly which selector is overruling the other. If you&#8217;re still developing without one of those tools you should install one right away. And if you have Firefox you really need to take a look at what Firefox plugins are available for developers.</p>
<p><img title="Firebug in action" src="/article-data/images/Firebug-in-action.png" alt="" /></p>
<p>Firebug in action.</p>
<p>So what do you do if one CSS selector overrules yours? Well, the easiest solution is to just make your style more important. The easiest way to do that is to do something like this:</p>
<pre class="css">.myClass {
    width: 100% !important;
}</pre>
<p>That&#8217;s really simple, right?  But as always there is a negative side to it. The <em>!important</em> keyword is overruling anything else. That sounds handy but it also makes your CSS harder to control. If you start to use !important at the root of your HTML-tree (like &lt;body&gt;), you&#8217;ll most likely have to use !important all the way down to the leaves.</p>
<p>I prefer to structure my CSS in such a way that my CSS-file starts with global selectors like p and h1. After that I get more and more specific. In pratice this means that  I style the HTML structure from the root to the leaves.</p>
<p>A more practical way to make sure your styles are used above anything else is the use of id&#8217;s. Although I try to reserve id&#8217;s for Javascript many developers give common boxes like headers and footers an id to use in their CSS like this:</p>
<pre class="css">.logo {
    width: 50px;
}

#header .logo {
    width: 100px;
    padding: 0;
}</pre>
<p>I think that id&#8217;s are not needed in your CSS, but it&#8217;s easy and safe to use.</p>
<h2>Be explicit</h2>
<p>Besides structuring the CSS I like to be specific about where I want to use my styles. If I think I&#8217;m only going to use some in the footer I won&#8217;t write</p>
<pre class="css">.leftText {
    color: #ccc;
}</pre>
<p>but</p>
<pre class="css">.footer .leftText {
    color: #ccc;
}</pre>
<p>By specifying the path in the HTML I make sure that that style is only used in the footer. And if I want I can also use that class name to style something in the header as long as I specify the path. So that way I don&#8217;t have to worry much about keeping the class names unique as long as the paths are. This enables me to use generic class names like &#8220;.text&#8221;, &#8220;.alert&#8221; or &#8220;.column&#8221; multiple times.</p>
<p>I use this technique of specifying paths rather to prevent debugging than as a debugging tool. But if you have two conflicting styles, specifying the paths could be the solution to your problem.</p>
<h2>Validate your CSS</h2>
<p>If your style just doesn&#8217;t pop up and there is no other overruling style you propably have another problem on your hand. If you care about validation you could validate your CSS. When running a validation check, you might find a type or a missing curly bracket, but I must say: I don&#8217;t get such errors on a weekly basis.</p>
<p>On top of that, creating valid CSS and HTML doesn&#8217;t make much sense if your team members are not validation-aware. You may end up spending too much of your time fixing invalid code.</p>
<h2>Validate your HTML</h2>
<p>Sometimes your website looks broken because your HTML is broken. It&#8217;s easy to leave a tag unclosed without noticing it. Another way to break the HTML is to add a class attribute to a tag that already has a class attribute. In that case the browser doesn&#8217;t pick up the second attribute. Adding a second attribute is easily done when you&#8217;re working with somebody else&#8217;s code or if you have elements with lots of attributes.</p>
<h2>Disable your Javascript</h2>
<p>I very rarely have to blame Javascript, but sometimes Javascript is doing more than you know or want, like adding styles to the stylesheet or on an element itself. The latter case is easily recognized if you check an element with Firebug or the devtoolbar. Some libraries like some lightboxes jam elements full with attributes.</p>
<p>Javascript by the way is a major reason to use a debugging tool. That&#8217;s because you cannot see what Javascript does by requesting the source code from the browser. The source code that a browser supplies to you is the raw html that is delivered by the web server, <em>not</em> the HTML that is altered by Javascript.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2009/10/22/css-debugging/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
