Noupe Editorial Team December 18th, 2009

CSS Techniques I Wish I Knew When I Started Designing Websites

By Tim Wright and TJ Kelly CSS is the best thing to happen to the web since Tim Berners-Lee. It's simple, powerful, and easy to use. But even with all its simplicity, it hides some important capabilities. Ask any designer, and they'll tell you that the majority of their code headaches are caused and ultimately solved by CSS. All designers at some point in their career go through the process of encountering a weird display issue, searching for a resolution, and discovering a trick, technique, or hack could have saved them hours of frustration—if they had only known when they started. We've put together a list of the most frustrating and time-consuming CSS headaches and, more importantly, their solutions (along with examples and further resources). I hope this list will help you save some gray hairs. As for me, I think I feel some coming in right now...

Resets & Browser Inconsistencies

Not all browsers are created equal. In fact, every browser is different. What is the default margin, padding, or font-size of a <p> element? You might be surprised at the wide range of values. To handle the differences between browsers, many of us want to level the playing field and start from scratch, by using CSS reset styles. The early stages of resets, designers dealt with differing margin and padding values, using a global reset: * { margin: 0; padding: 0; } But, as more people used and discussed resets, it became clear that resetting only margin & padding wasn't enough (and, applying the above rule to every element is taxing on the rendering engine). Thanks to the work of Eric Meyer and other CSS pioneers, a better, more complete collection of reset rules was created:
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
	margin: 0;
	padding: 0;
	border: 0;
	outline: 0;
	font-weight: inherit;
	font-style: inherit;
	font-size: 100%;
	font-family: inherit;
	vertical-align: baseline;
	}
/* remember to define focus styles! */
:focus {
	outline: 0;
	}
body {
	line-height: 1;
	color: black;
	background: white;
	}
ol, ul {
	list-style: none;
	}
/* tables still need 'cellspacing="0"' in the markup */
table {
	border-collapse: separate;
	border-spacing: 0;
	}
caption, th, td {
	text-align: left;
	font-weight: normal;
	}
blockquote:before, blockquote:after,
q:before, q:after {
	content: "";
	}
blockquote, q {
	quotes: "" "";
	}
As important as it is to note which elements are included in the most popular CSS reset available today .It's also important to note some of the elements that are deliberately excluded from this list:
  • input
  • button
  • hr
These elements were excluded because their cross-browser differences are so vast that you would have to completely unstyle them to create a "bulletproof" element. They're so weird, that even then, there's no guarantee.

Resources for Resets

Box Model — Margin, Padding & Borders

The box model is the basis for all layouts. It governs the dimensions & spacing of the elements on a page. To understand it, we have to understand the difference between block-level elements and inline elements. Block-level elements, by default, take up the entire width of their containing element and the height of the default line-height. They will stack up underneath each other, top-to-bottom. Therefore, by default, they will take up their own line on a page. Some block-level elements are: <div>, <pre>, <p>, <ul>, <ol>, <li>, etc. Inline elements are, just as their name implies, in-line. They will stack up next to each other, left-to-right. When given content, they will take up the exact width and height of that content. Given no content, they will collapse down and have no width or height. Some in-line elements are: <img>, <em>, <strong>, <a>, <span>, etc. All HTML block-level elements have five spacing properties: height, width, margin, padding, and border (inline elements have them too, they just work a bit differently). Width and height are tricky attributes, as they require a bit of calculation. When measuring an element's width, designers must consider the entire box. In the example below, the box has a total width of 260px. The margin, padding, and border are 30px each (remember, that's 30px on top, 30 on bottom, 30 right, and 30 left). So, in this example, the margin takes up 60 pixels of the box's width. Likewise, the border and padding consume 60px each. All together, the margin, border, and padding amount to 180 pixels of the box's total width. Screenshot We know that the box's total width is 260px, but in CSS the width attribute refers to the content area on the inside of the box. So, for this example, we have to subtract 180 pixels (for margin, border, and padding) from 260 (total box width), leaving us with a content area of 80px. Therefore, our CSS looks like this:
div {
  margin:30px;
  border:30px solid yellow;
  padding:30px;
  width:80px;
  height:80px;
}

Extras

  1. All of the examples and rules we're discussed for the width property also apply to height.
  2. margin can support negative values. Use them cautiously but they can prove to be very strong design elements.
  3. Don't forget the units with the box model. Only a zero-value (margin:0;) can be written without a unit specified.

Resources for CSS Box Model

Dimensions — Width, Height, Min & Max

Now that we understand how to use width and height in unison with the box model, let's look at some of the flexibility of CSS dimensions. Modern browsers support min- and max-width (and the same for height), allowing us to get creative with dimensions and create flexible layouts. Width/height specify the space an object should occupy. They can be measured in pixels (10px), ems (10em) and percentages (10%), as well as a few other units. Defining a width or height for an element will force that element to keep those dimensions, regardless of the content inside it. So, if our content is too big for its container, it will be cut off, hiding the bottom of our content (or just look really messed up).

Min-width & min-height

Giving an element a min-width or min-height will set the element to our exact dimension by default. But, since we only provided a minimum dimension, as the content grows, the containing element will allowed to stretch and all of our content will remain visible. Min-width & min-height can he useful for form elements like <input /> and <textarea>. We can define them with a minimum width/height and let them expand as the user types. In IE6, "height" acts the same way "min-height" does in modern browsers as a container will grow with content (something to be aware of when using building for IE6).

Max-width & max-height

If we give an element a max-width or max-height, it will collapse down to the size of our content by default. As our content grows, the container will stretch—until it reaches our maximum. Then our remaining content will be cut off or look really weird hanging of the bottom of a content block. Max-width & max-height can be useful for browsing long lists (if you correctly manage your overflow - create scrolling if the list goes too long).

Using Max & Min width in IE6

Min & Max width are both great tools to have in your design arsenal, but unfortunately they don't work in IE6, so what do we do? We usually lock down the width of our site instead of creating the ideal fluid layout we want, sacrificing user experience for out of date browsers. Luckily, we can use a short JavaScript command targetting IE to accomplish a nice fluid layout with max & min widths in IE6:
#page-wrap{
min-width:800px;
max-width:1000px;
width:expression(document.body.clientWidth < 800? "800px" : document.body.clientWidth > 1000? "1000px" : "auto");
}
In the above example, you can see that the smallest width for this page is 800px, and the largest is 1000px; not much of a flex, but the concept can be applied to any element. And if you only wanted to use min-width:
#page-wrap{
min-width:800px;
width:expression(document.body.clientWidth < 800? "800px": "auto" );
} 

Resources for dimensions

Floats & Clearing

A float will place an element outside the normal flow of elements and moves the element right or left until it reaches the margin or padding of another block-level element. Float and clear are some of the most powerful—and the most misunderstood—properties in CSS. To understand it, we must refer back to block-level vs. inline elements. Applying float to an element will automatically make it a block-level element. This means two things: 1) by default, all floated elements (even <span> and <strong>) will behave like block-level elements, 2) Giving an element both float:left; and display:block; is redundant and unnecessary. Also, using display:inline on a floated image is actually a very popular method of fixing many cross-browser quirks.

Floating a div to clear a div

Floating a div to clear a div is one of those things you stumble upon with some experimenting and frustration. What basically happens is that you can apply a float to a parent element that contains other floated elements and they will all equalize and clear properly:
HTML
<div class="parent-element">
     <div class="floating-element"></div><!--/.floating-element-->
     <div class="floating-element"></div><!--/.floating-element-->
     <div class="floating-element"></div><!--/.floating-element-->
</div><!--/.parent-element-->
CSS
.floating-element   { float:left;width:33%; }
.parent-element     { float:left; }
And, of course you'll need some content in there to fill out the divs. There are a lot of clearing techniques you can use to clear floated divs. Some require extra markup, some don't work in IE 6 and some are great, but don't apply to every situation. What did I wish I knew about clearing floats when I started? Everything.

Resources for clear and float

Conditional Comments

Back in 2004, when I really got into full-time Web design I used a lot of IE hacks and techniques to patch together sites in an attempt to achieve some level of cross browser compatibility. This can cause very bloated and slow loading style sheets. Everyone was doing it, heck, there were even articles about how to best organize your IE hacks. Little by little the design community discovered a hidden gem buried within the functionality of the Triton rendering engine (Internet Explorer). You could use IE specific HTML to target specific versions of the browser and then load a special style sheet just to deal with those issues.
<!--[if IE]>
Target all versions of IE
<![endif]-->

<!--[if lte IE 7]>
Target all versions of IE that are less than or equal to "7"
<![endif]-->

<!--[if IE 6]>
Target IE 6
<![endif]-->
Using conditional comments to target IE and cut out your hacks, will slim down your main style sheet, and help load the page quicker in browsers that don't need the correction code.

Resources for Conditional Comments

Overflow & Zoom

Like many designers, if there's time to be wasted on a project, it usually gets wasted dealing with IE 6 and some of it's weirder quirks. This is where overflow and zoom have helped me a great deal.

Overflow

Many-a-times when I've encountered a serious layout issue, a simple application of overflow:hidden on the offensive div would solve the problem. It's difficult to pin point specific issues for this, but it can happen in any browser (I just had it happen in Safari a couple weeks ago) and the overflow property can provide a quick solution to your cross browser woes.

Zoom

To my surprise, the zoom property is not very well know, but a very powerful tool in dealing with cross browser issues; particularly IE 6/7. Zoom is a proprietary IE property that actually does effect the zoom level of a page (if you set the zoom to 2 you'll see everything get bigger). As a site effect it also triggers hasLayout on the offending element, and fixes a lot of layout issues. Try it out, it actually saves a lot of time.
body { zoom:1; }
Since hasLayout won't trigger in any other browser, it's generally thought to be safe to put zoom in your main style sheet, but it you're doing IE specific work anyway, I'd advise putting it in with the rest of the IE CSS just in case there are any problems in the future, "future proofing".

Resources for Overflow & Zoom

When CSS isn't the Answer

Believe it or not, CSS is not always the answer. Especially now when we live on a Web that is in the middle of the biggest browser war to date. Sometimes CSS just doesn't do everything we want on as many platforms as we need it to. Spending hours searching for a CSS solution to a non-critical element that can be easily fixed with JavaScript usually just isn't worth it. Now that we have great libraries like jQuery readily available to us, anyone well versed in CSS can quickly pick up JavaScript. And let me tell you... if I new more about JavaScript when I started it would have saved me a lot of time and frustration.

Conclusion

There are a lot of little idiosyncrasies with CSS that you'll encounter over time with experience and frustration, but we're looking for ways to minimize that frustration. The best advice I can give someone in dealing with CSS is the same advice someone gave me when I was in college: go to class. If you read the blogs, keep up with all the information and learn everything you can you'll save a lot of time when you do encounter the inevitable rendering problem. To be honest, half the battle is knowing what a bug is called so you can Google it. But of course an article like this always poses the question: What do you YOU wish you knew when you started?

About the Author

Tim Wright is a web designer/developer and blogger. He has been an advocate for Web standards and accessibility since 2004. You can find more of his writing at CSSKarma or follow Tim on Twitter. TJ Kelly is a web designer/developer from Boston, MA, USA. He specializes in UX/UI design, CSS/jQuery, Wordpress, SEO, and Social Media. He's passionate about W3C Web Standards, accessibility, and usability. Check out his blog and portfolio at www.tjkelly.com and follow TJ on Twitter.

Noupe Editorial Team

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

83 comments

  1. You actually hit on most of the things I wish I’d known starting out. I also wish I’d realized that divs can be just as dangerous (if not more so!) than tables! Specificity would have been a great thing to know, too.

    1. Realize that if you’ve chosen this profession you must accept that danger is inherent to all we do. It’s not just a coincidence that so many TV dramas and action films are based on the lives of Web developers.

  2. For some reason I can’t bring myself to use a CSS reset. I’ve tried, but I hate having multiple style declarations for one element.

    1. “I hate having multiple style declarations for one element”

      Thats what the “C (Cascading)” is CSS is about though, no?

      1. Actually, no. The “Cascade” is the way multiple stylesheets are applied to a document, in that order:

        User agent style
        User style
        Author style(s)

        We’d rather talk about Specificity to name the phenomenon you’re talking about but basically, yes, you’re still right ;-)

  3. I think that I really spent a lot of time getting grey hair because of IE6. The most important lesson I learned from meddling with CSS is that no browser renders the same way. Workarounds are cool and hacks too but what we as web designers need to do is to be consistent: first build with all the known and proven standards, then modify according to the browsers you cross.

    I love the advice: go to class. We never stop learning and get confronted to new challenges, this is what makes Web design so cool!

  4. Great summary of the common pitfalls and workarounds for CSS Tim. Wish I had it when I was starting out too (though more than that I wish ie6 weren’t around then). Also some things I didn’t know about even after years of working with CSS, like the Zoom bit.

    Overflow is one of my favorite cure-alls, but it’s worth noting that line-height is another big cause of differences between IE & everything else.

  5. You should use this method for min/max heights and widths. The use of expressions in expensive, thus they should be avoided at all costs. The following will create styles for a min-height of 100px. Works in all browsers.

    /* Start Min-Height */
    min-height: 100px;
    height: auto !important;
    height: 100px;
    /* End Min-Height */

    Also, here another one for the stack. ie6 doesn’t support inline-block, well at least not in the typical way. Here is how to achieve it in all browsers.

    /* Start Inline-Block */
    display: inline-block;
    *display: inline;
    zoom: 1;
    /* End Inline-Block */

  6. Good points all.

    But it’s worth pointing out the downside to css expressions in IE (“Using Max & Min width in IE6”). They can be evaluated tens of thousands of times per page and may dramatically slow down any page where they are used [1].

    If their use is restricted to IE6, this may not be a large problem (it might even help move a few people away from that dinosaur…), but it’s worth keeping in mind.

    [1] http://developer.yahoo.com/performance/rules.html#css_expressions

  7. Thanks for the comments guys, this was actually a pretty fun article to write trying to revisit the frustration I felt when I was first starting out.

    @roydukkey nice addition, thanks

  8. Windows Internet Explorer, including IE 6, inserts an extra three pixels of space between the edge of a floated block and the edge of the following content. If the following content has a width or height assigned, the three pixel gap shows up as a space between the blocks. If it does not have a width or height, the blocks seem to sit next to each other, but the content within the following box is pushed over by three pixels for as long as the float extends beside it, shifting back over to the edge where it belongs after the end of the float. Setting a margin of zero does not get rid of this mysterious gap, but setting a margin of -3px pulls things back into their proper position.

    More: http://www.communitymx.com/content/article.cfm?page=3&cid=B0029

  9. just wanted to clarify that CSS expressions are insanely expensive to calculate, as they are re-computed on each consecutive event and reflow/repaint of the page, which could lead to tens of thousands of executions. note that from steve souder’s list of rules for “high performance websites”, avoiding CSS expressions is Rule #7.

    another alternative to @roydukkey’s suggestion is to use single expressions to calculate a value only once:

    div {
    -singlex: expression(this.singlex ? 0 : (function(t){ t.singlex = 0; t.height = document.documentElement.clientHeight + “px”;} )(this)); }

    this snippet computes the initial value, assigns it to the node, and adds a singlex property. when future calculations are run, because the singlex property is found, the execution is avoided.

  10. Awesome Post. Behalf of the Freshers am extremely thankful for what you have done.

    It cant get easier than this. You told like a its nothing..

    Your post is awesome..I love it…Very Informative,

    I owe you big time.
    W/ lots of love and Kiss.

    Shawnna

  11. Well … one nice trick is to visit this project:
    http://code.google.com/p/ie7-js/

    It’s a script which upgrades IE6 to IE7 and that saves a lot of %^$#%^# and &*&%* when making new layout :)
    Since IE6 loose popularity, and almost anyone who have it have JS enabled this sollution is really lifesaver :)

  12. Your min-width solution will fail if javascript is disabled. I did a pure css solution for min-width emulation in IE6 without expressions and without using borders, like Stu Nicholls solution which isn’t the best due to the fact you can’t make borders transparent in IE6. My technique uses padding IE6 min-width.

  13. You actually think we are in the “biggest browser war to date”? Have you been around when Netscape and IE have been fighting for the lead?

    We have a lot more variety of browsers today – and that’s no war, that’s competition. And competition is good.

    1. That may be true, but due to the growth of the internet as a whole there are probably more people using firefox now than the total number people using ANY browser during the last browser war :)

  14. It’s pointless to try to use min-height for IE6 as this browser interpret height as min height, right? Very interesting article! By the way, for when a mobile version of the website? Thanks.

  15. Forgot to mention how much easier life would be for all designers if Microsoft would get out of the browser business.

  16. There’s no need for js fixing min-height/width in IE6. You can just declare a height/width (per conditional stylesheet) and IE6 will interprete it similar to the other browsers.

  17. Thanks man, amazin list, amazin techniques… Even last week i asked to myself why my websites are so differnts of the other than a i see, very enlightment this list! =)

  18. The method @roydukkey uses for min-width is by far the best, and it is so simple. IE6 is the only browser to ignore !important and the only browser that interprets height as min-height. So by setting the height to auto !important; is the ultimate solution.

    When I first started I wish I knew that leaving out the DOCTYPE puts IE in to quirks mode, which most notably disobeys the box model. A container with a width of 100px and padding of 25px should have 100px of content but quirks mode will give it only 50px (25×2)

  19. I wish I had known about and used min-height and min-width properties when I first started, not to fix IE bugs, but to allow for greater flexibility when text is resized or more content is added. I also agree with whoever said they wish they’d known about the dangers of ‘divitis’, no one ever told me too many divs were bad!

  20. overflow:hidden — Here’s generally what’s happening when overflow:hidden helps you out: You’re dealing with floated and non-floated content. So if the problem you’re having concerns either a box with floated content inside it or a box next to floated content, adding overflow:hidden is very likely to fix it.

    For instance, if you have a box with floated content inside it, adding overflow:hidden to the outer box will cause it to expand to contain the floated content (in a similar manner as floating the box or adding clearfix rules to the floated box). Also, if you have a floated box, and then a non-floated box next to it, the traditional box model will cause the non-floated box to expand all the way to the left or right (depending on the direction of the float). In other words, only the content of the non-floated box gets shifted, not the box itself. Adding overflow:hidden causes the box itself to be shifted.

  21. Great list, I can’t agree more with the reset! Once I learned that it saved me sooo much trouble! I would also have to add z-index. While some may considerate ‘intermediate’ CSS, I think it can be very helpful to those just starting out to understand the 3rd dimension in web design.

  22. Clear Fix – set your parent div to overflow:hidden, works in IE 6 too. You dont need to set a height on your parent div either.

  23. I’m not a fan of reset stylesheets. In moderation it’s ok, but it’s easy to take it too far. You just have to accept that different browsers and different operating systems are going to render things differently on the web, and you have to design accordingly. Let go of that control a bit, and be flexible in your design.

  24. Thanks man, I learned more of CSS with the articles, many people know how to style with no multiple declarations.

  25. Excellent Post. Web design has evolved quite a bit over the years. Between items like this, Semantic designs etc… its is becoming much less of an art and quite a bit more of a science. The true Art of webdesign comes in applying these concepts to different sites in the intended context of the site with success.

  26. I wish I had a better understanding of CSS specificity and the DOM itself when I first started. It took me a while to finally grasp concept of using CSS specificity, CSS hooks on container elements and CSS declarations. As well as using a combination of tags classes and IDs for style declarations rather than slapping a class on every single element. But then again when I first started CSS was still in its infancy and the DOM barely out of kindergarten.

    A good way to get a better grasp of this concept is to try to style an existing web page page using only CSS and not modifying the HTML at all. It’s a good way to force you to think differently and prevents you from slipping into old bad habits.

  27. Although I am pretty well-versed in programming, I always need to spread love to those who share their findings with those who may not be so educated in things like this. Thank you!

    Check out my work at:

  28. I think conditional comments in particular are important, with so may versions of browsers out there. Also, I’m assuming this would work with conditional formatting for mobile versions of sites as well.

  29. I kinda stopped reading when I saw incorrect information in the very first section:

    “* { margin: 0; padding: 0; }”
    “(and, applying the above rule to every element is taxing on the rendering engine)”

    The “everything” selector is fast and efficient, because browsers special-case it out. Those CSS Resets that explicitly list elements are far slower, and less beneficial due to diminishing returns.

    The reason the “everything” selector has gained a reputation as being slow is because it gets abused. Altering the selector in *any way* means the browser has to traverse the DOM tree to find all the elements that match – the “everything” selector on its own does not contain that step.

  30. I completely agree on the part about JavaScript. I also spent a lot of time learning HTML and CSS. After I learned JavaScript, I viewed both much differently. Thanks for the article.

Leave a Reply

Your email address will not be published. Required fields are marked *