Solving 5 Common CSS Headaches
In Articles, HTML / CSS by Jeffrey WayCSS is a relatively simple language to learn. Mastering it, on the other, can prove a little more difficult. Compensating for various browser inconsistencies alone can produce a migraine. In this article, we'll demystify five of the most head thumping issues that you'll encounter when building web applications.
Why Are My Styles Not Effective?
We've all done this. You think to yourself, "Wow, this text would look great if it were bright red." (really??) Unfortunately, when you add the necessary styling, your text remains black. How come?
There could be a couple reasons why your styles aren't taking effect. Needless to say, it can be a nasty problem - especially for beginning CSS developers. First, pay a visit to your CSS file and make sure that there aren't any typos. I've wasted many hours dismantling my documents only to find that I misspelled a word. But, if you haven't banged your head against the wall at one time or another, you aren't allowed to call yourself a web developer! Most likely, you're dealing with a specificity problem. Try adding "!important" next to the style that isn't taking effect. If it suddenly works, that means you definitely have a "weight" problem. As a matter of good practice, never leave "!important" anywhere in your stylesheet. Simply use it as a way of debugging. Consider the following example:
#wrap #myStyle
{
color: black;
}
Because there is greater specificity here, the color will remain black. By including the additional identifier, "#wrap", there is more weight on this selector. Consequently, the first style will be disregarded in favor of this one.
How can we solve this issue? You should first check to see if the second style can completely be erased. If it can't, you'll simply need to add more specificity to your original selector. We'll go over the concept of specificity" in much greater detail shortly. Let's try adding:
#wrap p#myStyle
{
color: red;
}
Now, the text will finally turn red. By adding the additional "p" selector, we've added one more point, thus overriding any other styling.
What's The Difference Between Absolute And Relative Positioning?
Maybe more than anything else, positioning can prove to be an unnecessarily confusing topic for beginning to intermediate CSS designers. The best way to learn is to first tackle absolute positioning. Let's assume that we have a blank html and CSS document. If we were to then place an image absolutely on the page - say 100px from the top, and 100px from the left of the window's edges - we could write the following style...
img
{
position: absolute;
top: 100px;
left: 100px;
}
Absolutely positioned elements are positioned in relation to their closest positioned parent elements. In this case, where there are no relatively positioned elements on the page, the image will be positioned in relation to the window.
Now, imagine that we wrapped a relatively positioned div around our image.
<body>
<div id="wrapper">
<img src="#" alt="Some Image" />
</div>
</body>
In order to set a positioning context, we must add "position: relative" to the styling of our parent div.
div#wrapper
{
position: relative;
background: gray; /*Just to see the borders.*/
height: 600px; /*Because the image is absolutely positioned, we need to force the height.*/
width: 770px;
margin: auto;
}
Now, when we absolutely position the image, it will be positioned "relative" to the "wrapper" division. Keep in mind that if we removed this property, the image would once again be placed in relation to the browser's window. The "position" property is key here.
Additional Resources
-

CSS-Tricks.com : Absolute Positioning Inside Relative Positioning
Chris Coyier goes over some practical examples that show exactly how and when to use positioning. Includes a demo and downloadable source code.
-

Digital-Web.com : Web Design 101: Positioning
"Let’s shed some light on the shadowy mysteries of CSS positioning. If your CSS skills are limited or even moderate, you will learn what you need to master positioning."
How Can I Compensate For Internet Explorer 6's Double-Margin Bug?
For those who are unfamiliar with the "Double-Margin Bug", if you float an element and then procede to add margins in the same direction as the float, Internet Explorer 6 will incorrectly double the value. In effect, a left margin of "100px" becomes "200px" in IE6. There are three different remedies that we'll review.
Change The Display To Inline. The simplest fix, discovered by Steve Clason, is to change the display property of your element.
#floatElement
{
display: inline;
float: left;
margin-left: 100px;
}
Use Conditional Comments. Luckily, changing the display will fix that nasty bug. However, what if, for some reason, you need a different method? Internet Explorer allows you to target different browser versions by using "conditional comments". Add the following into the head tag of your document:
<!--[if lt IE 6]> <link rel="stylesheet" type="text/css" href="ie6.css" /> <![endif]-->
In layman's terms, this code is saying, "If a visitor to your page is using Internet Explorer 6 or lower, import a stylesheet called "ie6.css". As a result, modern browsers will ignore this statement. IE 6 and below, on the other hand, will implement the file. Now, in our ie6.css file, we'll need to add some override styling.
#floatElement
{
float: left;
margin-left: 50px;
}
Since we know that IE 6 will double the margins on floated elements, if we reduce the value of the margin by 50%, it will fix our document. This method is particularly appropriate when you have many styles that are targeting IE6 directly. It's important to contain all of your "hacks" in a centralized location.
Implement The Underscore Hack. There are many ways to target older versions of Internet Explorer directly from our primary stylesheet. Generally, I prefer using conditional comments. However, if I only need to change a single property, I'll many times use the underscore hack. Consider this following:
#floatElement
{
float: left;
margin-left: 100px;
_margin-left: 50px;
}
Modern browsers will cycle through these properties. When they come to the underscore, they'll skip the style entirely. On the flip side, IE6 will ignore the underscore and implement the new margins. What we end up with is modern browsers adding "100px" to the left margin. IE 6, respectively, will add only "50px".
Additional Resources
-

DustinBrewer.com: CSS Fix For The Double Margin Float Bug In IE6
Dustin offers a quick explanation for overcoming this bug. Be sure to check out his related tutorials as well.
-

PositionIsEverything.com : Double Margins
Make sure that you check out this easy to read article if you are still somewhat confused.
Is There A Way To Measure How Specific My Selector Is?
Absolutely! Yet, very few people know the exact equation. Many of you probably go by the "keep adding more identifiers until it works" method. Practically speaking, this will work just fine. But, you should know how to calculate the weight of your selectors never-the-less.
First, let's associate each type of selector with a value.
- Elements - 1 points
- Classes - 10 points
- Identifiers - 100 points
- Inline Styling - 1000 points
Let's try to calculate the weight of the following style...
body #wrap div#sidebar ul li a.selected
{
random styling....
}
Referring to our calculator above, we'll dissect this selector. The body element receives one point. Next, we have an identifier (#wrap). This will add 100 points to the tally. Continuing on, we have "div#sidebar". How many points do you think this is worth? If you guessed 100 points, you'd be incorrect. You must factor in the "div" element into your weight. The correct answer is 101 points. The "ul", "li", and "a" elements earn one point a piece. Lastly, the "selected" class receives an additional 10 points. Adding everything up, we come to a sum of 215.
I recommend that you spend a few minutes and memorize this point system. It will save you a great deal of wasted time when you find yourself in a specificity dilemma!
Additional Resources
-

SmashingMagazine.com: CSS Specificity: Things You Should Know
For an in depth explanation of specificity, I highly recommend that you read this article from top to bottom.
-

StuffAndNonsense.com: CSS Specificity Wars
Learn the art of specificity in a fun "Star Wars" setting. This is a must read.
What Is The Best Way To Test My Site In Different Browsers?
A required step when you're working on a site is to review it in every modern browser: Firefox, Internet Explorer, Safari, and Opera. Step two is to test your site in older version of IE. I recommend that you download IE Tester, which will allow you to view your site in IE 5 - IE 8. The final step is to check all of the less common browsers. Visit BrowserShots.org to view snapshots of your site in every browser available.
Additional Resources
-

Mozilla.com: IE Tab for Firefox
Do you hate having to switch between IE and Firefox when testing your site? Why not download IE Tab?
Any confusing questions that I missed? I'm sure there are plenty. Leave a comment and I'll try to incorporate them into part two. I'm hoping to turn these "Question and Answer" style articles into an ongoing series.
Comments
Leave a CommentAdd a Comment
Trackbacks
Leave a Trackback- vBharat.com » Solving 5 Common CSS Headaches
- Best of July 2008 | Life as a Web Designer
- 25 Awesome tutorials for web designers « Narendra Dhami
- 25 Awesome tutorials for web designers « Guiwells’s Blog
- From free for free » Blog Archive » 25 Awesome tutorials for web designers
- Web geliştiriciler için harika paketler
- 25 Awesome tutorials for web designers | Boxed CSS
- 25 Awesome tutorials for web designers | Boxed CSS
- 9 Top CSS Essential Skills That Every Web designer Should Learn - aComment.net












brad
July 22nd, 2008
nice.
Ojay
July 22nd, 2008
IE tab… nice!
Bryce Reichmuth
July 22nd, 2008
This post was awesome. I love it when articles stress the underlying concepts of CSS because often time the basics of CSS are much more important than the advanced jargon that just makes things more complicated.
Gelay
July 22nd, 2008
I am first!!!
yamaniac
July 22nd, 2008
Good information!!!
keep em comin!!!
Jeremy Latham
July 22nd, 2008
The Firebug extension has pretty much eliminated “hours dismantling my documents” to find typos or specificity issues for me.
Benni
July 22nd, 2008
One of the easiest ways to debug CSS is to have Firebug installed.
Open Firebug, click on “Inspect” and choose the element you want to inspect. Then you can see all styles having an effect on this element and in which order they are.
pavs
July 22nd, 2008
This is really good stuff. I have recently introduced myself to CSS and all I can is that its a very interesting thing to learn. For a newbie, I would suggest using Firefox extension “firebug” to get yourself familiarize with CSS (well firebug does more than that, but it is much easier).
However, cross-browser compatibility is something I have always hated, it would have been to nice if everyone followed the standard so that web developers wouldn’t have to loose their sleep and pull their hair trying to position an object in the same place in all browsers.
Andrei Constantin
July 22nd, 2008
To be honest you dont have to master the css language when you have these tuts
erdem
July 22nd, 2008
That’s a nice tutorial, thanks & selam
Rakibul Islam
July 22nd, 2008
Thanks for sharing your knowledge.
Nysuatro
July 23rd, 2008
Learned some useful stuff.
Thanks for the info.
Gilbert
July 23rd, 2008
Nice tut. I wish I had read this when learning CSS. I learned the hard way but never mind.
Ben Griffiths
July 23rd, 2008
This is a great little article, thanks
Shane
July 23rd, 2008
Thanks very much for posting a link to IETester - it’s a new one to me. I was using ‘multiple IEs’, which does the job, but not as nicely.
I love CSS, but inconsistent browser implementations do complicate things. Hopefully, with the wider adoption of standards in IE8, some of these problems will be a thing of the past.
Also, as pavs suggested, the Firebug firefox extension is absolutely invaluable and has ’saved my bacon’ on countless occasions. It’s so feature rich and extensive and is an essential addition to any web designer/developer’s toolbox. Perhaps a feature will appear soon on NETTUTS?
Robin Papa
July 23rd, 2008
Just what I needed! The IE Margins stuff!
Thanks a lot!
Kim Dolleris
July 23rd, 2008
Ahh.. if only we had ONE browser, or at least ONE standard!
Thanks for pointing out and collecting the headaches - someone will surely find it very useful - i know i would.
Keep it up!
Quevin
July 23rd, 2008
I think it also helps to know what elements are naturally “block,” and which are “inline.” Like you can’t float a across all browsers w/o “display:block;” as a declaration. At the same time, you don’t need to declare “display:block” if it’s a . Also, it saves so many headaches to understand “quirks mode,” and how to avoid it by using the proper doctype. Frankly, these five probably aren’t the most common, in my opinion. But a great article that should have existed for me years ago! Would have saved hours of debugging…
Quevin
July 23rd, 2008
‘Doh! Meant to encode the {span} and {div} in my last post… didn’t show.
Vladimir
July 23rd, 2008
Great article! You did submit on Dzone
Vladimir
July 23rd, 2008
I meant: I have submited this article to Dzone.com
Pedantic Critic
July 23rd, 2008
first line of first main paragraph =
“Wow, this text would like great if it were bright red.”
shouldn’t it be:
“Wow, this text would LOOK great if it were bright red.”
no ??
Matt
July 23rd, 2008
Only thing i don’t think will work all the time is the points system. I always remember specificity by using 4 numbers:
Inline: 0 Id: 0 Class: 0 element: 0.
so something like body #wrapper div#myBox will have a specicifity of 0 2 0 1.
One thing to remember is something with an inline style (1 0 0 0) will never be beaten by adding more and more id’s (0 250 0 0). The 1 always trumps the id’s (and so on). You can overide the inline style using !important (not good).
To conclude: don’t use inline styles
Great article though, keep them coming!
BeyondRandom
July 23rd, 2008
You guys are awesome! This cleared up a few questions I have had for a while! Thanks alot
James
July 23rd, 2008
Nice aritcle Jeffrey!
Specificity still kinda confuses me!
Matt Radel
July 23rd, 2008
Conditional comments are a godsend. If I ever come across the person that thought of ‘em, I’d…well…be grateful.
Freddie
July 23rd, 2008
I think you are in this world for feed my rookie’s soul!
Thanks a lot!
Nate
July 23rd, 2008
Thanks for a great article.
Chris
July 23rd, 2008
I disagree that you can never leave !important in a stylesheet. There are many circumstances that you might need it such as the designer not having control over the cascade. It does feel a little like you’re “hacking,” but it’s an approved way none-the-less.
Ian Yates
July 23rd, 2008
Handy little point about ‘The Underscore Hack’ - I have a habit of using * HTML before the class or id for styles which I want only IE6 to implement.. Perhaps an underscore here and there will clean things up a bit!
Cheers
Drupal Museum
July 23rd, 2008
“First, pay a visit to your CSS file and make sure that there aren’t any typos.”
The easiest way to do this is to validate your code. When you have huge or multiple css files, manually looking for typos will make you pull your hair out. Firebug is indispensable, but I also rely heavily on the web developer extension for Firefox. I constantly use it to run my code through the validator. It has handy menu items for validating live sites as well as local sites.
Thanks for the tut!
w1sh
July 23rd, 2008
@Matt ha! You’d be more than grateful, that’s the 2nd time I’ve seen you spaz over conditional comments.
I just discovered them.
I’m gonna start linking stylesheets like a mother-..
@Pavs - Thanks for suggesting Firebug. It looks like it’ll put a lot of CSS layouts into perspective.
Isn’t IE5 obsolete yet?
Also: some of the article headers aren’t making page breaks and wrapping next to the images. It doesn’t look intentional.
Great article! Thanks for enlightening me to specificity.
Jeremy Davis
July 23rd, 2008
One css frustration that I’ve encountered recently is getting the vertical height on columns to be equal without using a technique such as faux columns.
Any tips on this issue would be great.
ilham saibi
July 23rd, 2008
it’s very useful , thanks for the info
Sean Landry
July 23rd, 2008
Hmmm I think your specificity rules are incorrect. What you wrote about scoring is correct under CSS2 but under CSS2.1 they abandoned the “total” score in favor of a specificity matrix
http://www.w3.org/TR/REC-CSS2/cascade.html#specificity (old)
http://www.w3.org/TR/CSS21/cascade.html#specificity (new)
It’s kinda like the new math
Connor
July 23rd, 2008
Good Article
Eric Thayne
July 23rd, 2008
I needed this! It’s good to have a little extra help on positioning. I think I get it now!
Alvin
July 23rd, 2008
Pls clear the header h3 of your article.
José Mario
July 23rd, 2008
Hey for all of you that need debugging for IE here’s a great app I’ve tried myself, is in BETA still but working fine most of the time.
http://www.my-debugbar.com/wiki/IETester/HomePage
JM
Braden Keith
July 23rd, 2008
Wow alright I change my opinion of NETTUTS. If we can keep these kind of things up, it’s the best.
One thing I need elaboration on is the “point system” never heard of this, where do the pts go?
Braden Keith
July 23rd, 2008
@Jose
I would also suggest Firebug, a Firefox plug in. That is a dream come true.
Adam
July 23rd, 2008
It is amazing to me how many CSS tutorials are out there, but how few times I have ever seen the weight of selectors mentioned. I often speak to people who do CSS and have no idea that is even a part of CSS! And I admit myself, I did CSS for over a year before realizing it, so in other words, thanks for stating some of the core issues for newbies who are luckier than I was.
Joefrey Mahusay
July 23rd, 2008
Interesting article. Thanks for this!
Lamin Barrow
July 24th, 2008
I am not familiar with the IE 6 double margin bug but thanks so much for the enlightenment.
Mark Abucayon
July 24th, 2008
Thanks for the help and sharing this one. really nice
Sandie
July 24th, 2008
I’ve first discovered this blog a few days ago, and today I really love this blog!
Nice article… Lot’s of traps for a beginner css’stylist!
Thanks for heads up!
Jeffrey Way
July 24th, 2008
@Braden - The points don’t go anywhere. It’s simply a concept for determining which of your styles will take precedence.
Marc Robinsone Caballero
July 24th, 2008
Unbelievably yummy, awesome, and cute LIFE SAVER!
This just saved my life from the IE CSS Hell.
Will there be a dream-come-true moment for us developers and not worry about IE6 users?
Phillip
July 24th, 2008
Excellent post! More CSS posts!
noble
July 24th, 2008
awasome. Its a great help for me.
Jennifer Kyrnin
July 24th, 2008
Thanks. This is a great article.
skn
July 25th, 2008
this is a good article
dlv
July 25th, 2008
thanks, really great information posted!
Jeremy
July 25th, 2008
You should write about getting two floated columns to line up. Like the typical header two-col footer layout, there are some fundamental issues in building them that many people don’t seem to understand (I know HOW, I don’t know WHY). For instance, in order to get a parent div to wrap around a floated div, you have to set the overflow to a specific value, otherwise your parent div will just be 10px high or so, even if the div inside is 800px high. This doesn’t make any sense to me, but I do it anyway
Gabe Diaz
July 25th, 2008
Don’t forget the html>body CSS child hack, look it up for more details but it’s basically:
*IE6 Will read these lines of CSS as it can’t read html>body
#contentmain{
background-color:#000;
color:#fff;
}
*IE7, Firefox, Safari and others will read these lines of CSS
html>body #contentmain{
background-color:#fff;
color:#000;
}
IE6 will display a black background with white text, and other browsers will display a white background with black text. With this hack you specify your margins to specific placements between “regular” browsers and IE6 aka the devil browser!!
Also if you have a PC and would like to install older versions of IE you can try:
http://tredosoft.com/Multiple_IE
When I’m on a PC I use the above, much easier than rendering IE pages or just looking at snapshots of your page in IE6.
Sean
July 26th, 2008
Anyone care to explain why “hello” turns out blue in this one?
#first #second #third {
color: red;
}
#fourth {
color: blue;
}
hello
Sean
July 26th, 2008
There were supposed to be 4 nested divs where the ID order goes outer to inner: first, second, third, fourth. Stupid me didn’t think about how posting html is a no no.
Jatin Meshiya
July 29th, 2008
This type of article provides information that you should call as “jack” information. We want “master” information. If it is possible then please make tutorials like this with full information.
It is really very impressive tutorial. We really appreciate your efforts! Keep it up!
Eric
July 29th, 2008
Download Safari - the develop tab really helps so you don’t have to do all that browser-switching.
Tracey Grady
July 29th, 2008
This is a seriously useful article. Thanks.
Tommy M
July 29th, 2008
I used the underscore hack often on a few websites a couple months ago. The problem is that it breaks CSS compliance. Although, curing IE6’s stupidity (margin errors) with IE’s stupidity (an underscore) might be good antidote.
Marcos
July 30th, 2008
Man, this is a great article!!!!
congratulations.
p.s. that browsershots.org is perfect !!!!
best regards
Tom
July 30th, 2008
Very nice article. I first encountered the double-margin bug a few weeks ago when I was building my portfolio site. I must have tried everything imaginable and then display: inline did the trick. Weird.
Tom Leo
August 1st, 2008
If the IE Tab FF plug-in works, I well be quite happy. Good article BTW.
Los
August 2nd, 2008
I work on Mac’s at home. So my question is about testing on browsers. But as a Apple user how can I test for Windows users. It seems the only remedy is actually owning or testing on a actual Windows OS. Is there a solution for testing for Windows on a Mac environment? Is there huh, tell me there is Ahhhhh!!! Ha ha. O yea keep up the great work.
Cezary Tomczyk
August 2nd, 2008
For the test compatibility with IE you may use this site: http://ipinfo.info/netrenderer/
Seth
August 2nd, 2008
Something I do often is style both IE6 and IE7 specific styles in one IE sheet by using the conditionals above but using the standard:
#my_id { /* Style IE7 */ }
* html #my_id { /* Style IE6 */ }
This way I have more control over my site with less stylesheets and conditionals.
DIYGuy
August 2nd, 2008
I think your first example is going to cause confusion for some.
IDs are meant to be used only once per page. The way you resolved the issue will only work if there is only one ‘p’ on the page — that’s a really long read
#wrap p#myStyle
{
color: red;
}
Instead, you could modify the style as such;
#wrap #myStyle p
{
color: red;
}
This way, any ‘p’ within #myStyle will have a different color — thus over riding the cascade — and you don’t have to add individual classes to each ‘p’.
Freelance Web Design
August 3rd, 2008
I greatly dislike the inconsistencies in IE. Even IE7 is a bit wacky at times. Fortunately there are always many ways to get something done. It’s a good thing too, otherwise there’d be no pleasing anyone!
Grant @ BTP
August 3rd, 2008
In the IE6 double-margin solution, you use conditional comments to target (supposedly) IE6 and lower, but the specified conditional comment parameter ‘lt IE6′ actually targets versions of IE lower than IE6 (not including IE6).
Shouldn’t it be ‘lte IE6′ (Less Than or Equal to) ?
Aside from that, great article; I definitely learned from the specificity and weight info!
Starnberg
August 4th, 2008
thanks for the nice article
firebug is the best extension i ever used
Eric Levay
August 4th, 2008
Nicely written and clearly explained.
Using CSS and relative positioning to me has been the standard for page layout, however with relative positioning when a user re sizes the browser window, the page remains a fixed size causing the page to require scrolling.
I’d like to know if using percentages would make the page grow and shrink according to the window size resulting in a better planned layout. If so I’d also like to request an article about it.
Jens Schulze
August 6th, 2008
Im looking for a possibility using more IE versions to check browser compatibility on one PC!. Can anybody help me?
Dwayne Charrington
August 6th, 2008
Thanks for the informative article. As it has been mentioned, Firebug is an invaluable source when it comes to CSS debugging.
Collagist
August 8th, 2008
Useful article. As you said CSS is very easy to start with but can get very complicated when you are doing across multiple browsers.
Are there any special care that one has to take for Opera and Safari? Looking forward to more articles. Appreciate all the additional resources that you provided.
Jeffrey Way
August 8th, 2008
@Collagist - Opera and Safari are very standards aware. You shouldn’t have to worry about them too much - other than something small here and there. Just make sure that you validate your code as often as possible.
Oyun Siteleri
August 11th, 2008
CSS “Cascading Style Sheets” Lessons
css list style Properties and examples — http://css-lessons.ucoz.com/list-css-examples.htm
Manchester Web Design
August 12th, 2008
Fantastic tips - Some valuable resources in this post. Many thanks!
Dave
August 12th, 2008
Matt’s comment is a key fact worth knowing — the whole point system thing is like “i before e except after c”: it works to explain some basic examples, but really falls apart and generally teaches an incorrect concept.
I guess going by the 1-10-100-1000 point system is a decision coders have to make…am I going to understand how specificity actually works or am I going to get things accidentally right and not care because my clients won’t know the difference?
Jeffrey Way
August 12th, 2008
@Dave, @Matt - Very good points. It’s true that the base 10 system isn’t 100% accurate. However, I’ve found that it works just fine for my projects - unless I get into some extremely complicated selectors…which is a bigger problem.
There is a more efficient way to determine specificity - we might write a tutorial on it sometime in the near future.
Krishna Reddy
August 18th, 2008
It is very nice article
Jatin Meshiya
September 1st, 2008
I want to know more about selectors. How it works and how, when and where we have to apply it properly. Can anybody put focus on this point? Please….
anonymous coward
September 3rd, 2008
litmusapp.com is the real answer to question #5
Jatin Mehshiya
September 3rd, 2008
@Jens Schulze: Hello dear. If you want to check all IE versions and more browsers along with their different versions then you can use on site that is bells for us : http://browsershots.org/
You can check here different browsers with their version and also different OS platforms. Try and check your web designs here and be fear free!
Ivan Nikolic
September 5th, 2008
Nice article, you have one mistake in “How Can I Compensate For Internet Explorer 6’s Double-Margin Bug?” - it should state “lte IE 6″ if you want to target IE6 and lower.