Everything You Know About CSS is Wrong
videos

Everything you Know is Wrong!

Everything is changing.
This weekend, I read a very interesting book: “Everything You Know About CSS is Wrong”. The book illustrates how, now that IE has passed the ACID 2 test, we can finally begin to build our websites the proper ways. Up until now, we’ve been forced to implement hacks to form the perfect layout – absolute positioning, precarious floats, etc. But that’s all beginning to change! I’ll show you how in twenty minutes.

CSS tables are so much easier to use that it’s worth adopting them as soon as you possibly can.
-Kevin Yank

This tutorial will show you how you’ll be building your layouts in the future. Actually, you should be using these methods right now! Rather than relying on floats and absolute positioning to build our layouts, we’ll instead focus on utilizing css tables.

Step 1: Creating A Two-Column Layout the Current Way

Knowing that IE7 and below do not recognize the CSS table properties, we first need to build our incredibly simple site the “old” way. Paste in the following code into your “index.html” file.

<body>
    <div id="wrap">
        <div id="header">
            <h1> My Header</h1>
        </div><!--end header-->
        <div id="main">
            <ul id="nav">
                <li><a href="#">1</a></li>
                <li><a href="#">2</a></li>
                <li><a href="#">3</a></li>
                <li><a href="#">4</a></li>
            </ul>
            <div id="primaryContent"> -- dummy text-- </div>
        </div><!--end main-->

        <div id="footer">
            <h1>My Footer</h1>
        </div><!--end footer -->
    </div><!--end wrap-->
</body>

There isn’t too much that we need to go over. We’ve created the content for our two column layout. The navigation (#nav) will be floated to the left of the main content (#primaryContent). Let’s go ahead and add some quick styling. I’m going to use background colors (ugly ones too!) so that you can easily determine where each div begins and ends.

Step 2: Adding the CSS

/* Just resetting a few elements */

h1, ul, li {
	margin: 0; padding: 0; list-style: none;
}

p {
	margin: 0; padding: .5em 0;
}

/* Main content styling */

#wrap {
	width: 800px;
	margin: auto;
}

#header, #footer {
	background: red;
}

#nav {
	background: gray;
	width: 150px;
	float: left;
}

#primaryContent {
	background: yellow;
	margin-left: 150px;
	padding: 0 .5em;
}

We’ve set a width to our sidebar (#nav) of 150px and have floated it to the left. Rather than floating the primaryContent section as well, it would be better to simply add a left margin that is equal to the width of our sidebar. We’ll also add a bit of padding to push the text away from the edges of the div.

#primaryContent {
	background: yellow;
	margin-left: 150px;
	padding: 0 .5em;
}

You should end up with something similar to this:

Product

Immediately (after the glare from those awful colors goes away) you’ll see that the gray background in the sidebar isn’t stretching to the bottom. This is because floated elements only take up as much space as is necessary.

One solution would be to hard-code a specific height, but this is a bad idea. What if the main content changes? You’ll have to go back into your CSS file to adjust the height value again. So don’t do that! Instead, the accepted solution is to create a background image and tile it vertically. This is referred to as creating faux columns” Simply move to Photoshop, create a 800×10 pixel canvas, and insert the appropriate colors. You know that the sidebar has a gray background and is 150 pixels wide, so simply use the marquee tool to the select that area, and fill it with gray. After you do the same for the awful yellow, you’ll end up with this:

BG

Save it into the root of your solution and call it “bg.png”. We can now vertically tile this image, thus creating faux columns.

#main {
	background: url(../bg.png) repeat-y;
}

Now, we’ve successfully created the illusion of equal columns.

Faux Columns

Step 3: The New Way

So that wasn’t all together too difficult! But it still required too much time to create something as simple as a two column layout. We even had to go into Photoshop to complete the look! This time, let’s recreate the site using CSS tables. Don’t worry, very little needs to change!

Keep reminding yourself, IE7 and below don’t recognize CSS tables. So, at least for now, we still need to use the method that we performed above. Create a new stylesheet and call it “ie.css”. Then copy and paste all of the CSS that we’ve written into this file. Now don’t delete it from “default.css”. We’ll simply remove a few styles. We won’t be implementing any hacks or floats to create our columns this time.

Remove the following styles from your “default.css” stylesheet.


/* Because we'll be using a "table" layout, we won't need to tile a background image! */
#main {
	background: url(../bg.png) repeat-y;
}

/* No floats! */
#nav {
	float: left;
}

/* No floats = no left margin. */
#primaryContent {
	margin-left: 150px;
}

Implementing the CSS Tables

Now that you’ve removed the unnecessary styles, let’s add some new ones! First, we need to create two columns within our “main” div: one for the navigation, and the other for the primary content. Let’s set the display type of #main to be a table.

#main {
display: table;
}

Next, we should declare the #nav and #primaryContent elements to be table cells.

#nav, #primiaryContent {
display: table-cell;
}

Believe it or not, that’s all that we need to do to create our column layout. We already set the width of the navigation to be 150px wide. The primaryContent div will simply fill the remaining space. Here is a snapshot of our layout using CSS tables.

Faux Columns

It looks exactly the same! But we didn’t use any floats or absolute positioning! However, as I said before, if we now view our site in IE, we’ll see.

IE doesn't recognize css tables yet

Though IE8 will display the site correctly, IE7 and below will not. To compensate, we need to include a conditional stylesheet. (That’s why we saved the CSS from our original layout and placed it into “ie.css”).

<!--[if lte IE 7]>
  <link href="css/ie.css" rel="stylesheet" type="text/css" />
<![endif]-->

Now, all is well! Modern browsers will render the site using CSS tables, and IE will use the traditional float method.

The True Power

The true power comes into play when we make modifications. For example, let’s imagine that, down the road, we decide to add a third column to our site. We can accomplish this in about thirty seconds.

Step 1: Add the Additional Markup

After the closing tag on the primaryContent div, add your new column.

....
</div><!--end primaryContent-->
<div id="secondaryContent"> -- dummy text -- </div>
...

Step 2: CSS

Access your stylesheet and set the display property of secondaryContent to “table-cell”. You’ll also need to determine how wide it should be. In this case, I chose 90px.

#secondaryContent {
	width: 90px;
	display: table-cell;
	background: orange;
}

That’s it. No floats, no zeroing out margins, no browser inconsistencies.

Three columns

You should start using this method today! The IE devs finally listened and gave us the tools that we need to build sites the way we want to build them. So, we must utilize these tools – even if it requires a bit more work for a couple of years. If you start today, you’ll be ahead of the pack!

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Tags: CSSVideos
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Mike

    I still think the reason why IE6 is around is because developers continue to provide hacks, fixes and workarounds to get site working in it.

    Surely if we stopped supporting it so readily it would encourage [read 'force'] users to update to at least IE7 – ideally to Firefox, Opera, Safari or any of those other free browsers out there – consequently allowing them to view the latest methods of web development technologies, while being more secure and at the same time reducing the work for us and the amount of files on our FTP servers just to combat these inconsistencies that have been around for years.

    Just goes to show that the web is not that ‘fast moving’ as some say it is in my opinion.

    Before I get flamed as to why people don’t upgrade IE6 – I am aware that there are organizations that can’t just do an ‘update’ and all being well, systems needing to be rewritten etc, then shouldn’t they have kept up with this over the years?

    I’ll also point out that downloading and installing a free piece of software and keeping it up to date [particularly with the seemingly standard practice of having an auto update feature built in], is not a trial to achieve.

    IE6 is outdated by several years – people buy / upgrade their mobile phones and cars more than this – usually at great expense. So why they complain about things not working in IE6, in my opinion is their own fault…

    I can argue on more points but I’ll leave it at that for now…

  • ryan j

    now i’ve had the chance to have a proper play with css tables, i can honestly say they’re okayish.

    they’re blatantly primarily intended for displaying tabular data, just like good old fashioned html table tags, and far from being an entirely new concept in webdesign they’re just a quick way of assembling a table.

    they’re also shifting structrual markup into the css, which isn’t just pandering to the blogging-amateur-webdevs mentioned all over the place here – at the most simplistic level it means you need the css to make any sense of the html, which isn’t a good thing.

    That said, there are some very interesting possibilities here…

    i’ve been having a play with class-switching css table elements and i’ve been able to produce some very interesting results. it made me remember how cool i though ye-olde Suckerfish Dropdowns were when i first saw them!

    it also makes properly dynamic tables a viable option without resorting to mind-numbingly dull/complex node transversal. don’t underestimate how useful this could be in a rich webapp.

    think in terms of different media-specific style sheets too. since your HTML doesn’t have a dirty great table stuck in the middle of it, you can choose who gets the table, or even completely flip your beautiful static non-table layout down into a mobile device specific mini-table for users on the move.

    so, css tables: breaking all the rules, but doing it in interesting new ways.

    thinking outside the box(model) isn’t a bad thing, but don’t make the mistake of assuming these tables are just going to be The New Layout Table – thats for the hard of thinking, and isn’t really taking advantage of css tables benefits over layout tables,

    here in the office we’re already starting to Prototype (har har) up some awesome bits and pieces. You should to.

    disclaimer: layout tables will always suck on some level.

  • http://www.aktifotomatikkapi.com kepenk

    good ads about sitepoint book

  • Pingback: links for 2008-12-04 « sySolution

  • Pingback: JeremiahTolbert.com » Blog Archive » links for 2008-12-04

  • CK

    Isn’t it possible to make this simpler, by only including what’s different in the ie.css?

    Maybe I’m misunderstanding something here.
    However, if this is possible, then it allows a designer to change the default.css to fine-tune colors etc. without needing to do much, if anything, to the ie.css.

    LIKE THIS:

    /* Main content styling
    * Just the stuff that’s not in default – CK adpated
    */

    #main
    {
    background: url(../bg.png) repeat-y;
    }

    #nav
    {
    float: left;
    }

    #primaryContent
    {
    margin-left: 150px;
    }

  • http://themeforest.net/user/JeffreyWay Jeffrey Way
    Author

    @CK – Yep. That’s exactly what you should do. I spoke about that in the screencast. Only move the unique differences into ie.css. I just did it this way to expedite things – and because it’s a super simple example.

  • jason

    @ Jeff sorry I didn’t get back to you sooner… I am a technology integrator for the school system… that means I train the teachers how to use their computers to better engage and teach the students. I do this by modeling lessons and through staff development. I do not develop for the school system on a large scale. That being said as of right now the people who make tech decisions here have chosen not to upgrade the county yet. I think that they will roll out IE 7 next year.

    All of our internal web apps are IE only though. :(

    I read this BLOG b/c i freelance too NOT ON SCHOOL TIME… and I use information that i learn on all the TUTS sites to further my education as well as to find solutions to problems that confound some of the art and design teachers here in the county.

    As for forcing someone to upgrade… I don’t know if you can do that. I thought about dropping support for IE6 on my last project and decided that it would not be a good idea as the target audience was largely people over 50. I think that as a developer/designer/trainer I have to consider the audience that the target is geared toward and their technology savvy. In education we often give our students the tools and the knowledge but in the end it is up to them to put them together to really learn.

  • http://www.seraph-design.de Benjamin Schulz

    Well for the future i think this is awesome, but for now i hate using extra css just for the IE family, i count them as “hacks” and prefer to ignore hacks absolute. for example, even if you can add a new column for all other browsers easily, you still have the headache with the IE, so in the end you won nothing.

  • http://mcarthurgfx.com Sean

    Perhaps 20% of IE6 users can’t upgrade because of their corporation.. That sucks. But you what doesn’t help them? Continuing to serve web-sites to IE6. If it was practically impossible for a user to keep IE6, the corporation would be required to upgrade everything.

    They stay behind because they can. Developers (mostly out of fear of our clients) let them.

  • http://twitter.com/taylorsatula Taylor Satula

    Yay for bad color schemes.!!!
    Good tut i jus glased over it but it looked good

  • http://www.craniumdesigns.com Steve

    I bought that book a couple weeks ago. I love Sitepoint’s books. IE8 sounds promising, but the slow adoption rate will continue to make us have to put off this kinda stuff for a few more years.

  • http://butenas.com Ignas

    people do not use IE, save the life of designers :D

  • Mr. Magic

    Only if this site’s PHP articles quality was as good as the CSS article’s quality is. Good work on this one.

  • gordon

    U read too many books ;) and whats worse u get too excited about its content.

    IE 8 will be popular in few years. Now people still using IE6 and IE7. Because IE 8 is coming soon it doesnt mean that we will be able to use css tables in near future ;) I bet that in 2 years there will be still more users of IE6 and IE7 than IE8.

  • Lubes

    @ Andrew, you said:

    ” Most of the 20% of IE6 users are corporates laptops/desktops using XP pre SP2. It’s not the users choice to upgrade or not. In one office I was in this very morning it was IE6 all the way – users (even up to Director level) can’t even install Firefox or Opera. You want to lose 20% of your viewers for dogmatic reasons, fine. I’d never be able to sell that to any of my clients though…”

    I should perhaps clarify what I meant by not supporting IE6: I do not employ hacks or tear my hair out of my head to get a site to look great in IE6. The content will still be accessible and coherent but it will not necessarily “wow” anyone. A user visiting one of my sites will get a message letting them now that they are using an outdated piece of equipment, that they need to upgrade to the latest version or download Firefox if they can’t upgrade their version, and that they are welcome to continue on to the site but that it may not look exactly right.

    And that’s it. HUGE corporate websites do this, so can I.

    I do not block anyone or force them to upgrade before continuing. But I make them aware and I feel it’s my responsibility to do so.

    Our problem is that technology advances more rapidly than the populace, and this will always be the problem; but, instead of succumbing, or catering the lowest common denominator, we can do our parts as developers and designers to educate and inform.

  • Lubes

    And while I’m on my soap box — 20% is not across the board for every site or every business or every person.

    Understand your demographic, understand what statistics mean, but do not be blindly led by them or just accept them without your own input or voice.

  • http://imbuzu.wordpress.com Buzu

    We need to continue providing support for IE 6 Why? Just because behind every IE6 is a potential client. It’s not about forcing people to change their browser, but about educating them so that they can make the decision themselves. Avoiding the use of hacks is just as good as avoiding the use of technology that is not a standard yet (ie CSS3) as well as technology that despite being a standard is not well implemented or adopted.

    Gordon said:

    “U read too many books ;) and whats worse u get too excited about its content.”

    I totally agree.

    Some companies were once a good source, but now are just a shame. I read some of the good sitepoint’s books, but I think their latest books are just bad. They might just want to have something new to sell. Hopefully their next title is worth buying it.

  • http://www.nickfessel.com Nick J. Fessel

    Can someone please explain why we always have to make exceptions for the Internet Explorer browser? Why doesn’t it just work! We shouldn’t have to wait for some “Acid” test; browsers should just render (X)HTML properly. What is so difficult about this?

  • http://imanto.com Anto

    So if i wanted to add another box in the nav, how?

    I try doing the same div right below it like.

    1234
    1234

    I added a margin-bottom to the nav, and it wont break it up..it appers beside it.. any idea’s ?

  • ryan j

    @nick j fessel

    because lots of people use internet explorer?

    sites shouldn’t be telling people to change to XYZ-browser to view them properly, they should just work. a web designers job is more than making alpha-transparency gradients in photoshop all day.

    @Lubes

    if you roll out an intranet you might – MIGHT – persuade the IT dept to push IE7 out six months to a year after it worked its way into auto-update. You will never in a million years persuade a large corporate to roll out IE8, no matter how well it renders Acid2.

    it’s inevitably part of the spec that they use XYZ-browser 1.0, and the rollout of XYZ-browser 2.0 is road-mapped for six months time. they will want it to render perfectly in 1.0, then render perfectly in 2.0 – if you tell the project manager you want them to change their browsers across the company so your three-column layout aligns properly they will just tell you to make it work.

    Even with public-facing sites, every corporate website i’ve worked on has explicitly targeted the browsers that represent the largest visitor demographic (that’d be IE6 + 7 these days. count yourself lucky ie 5.x no longer represents a sizable proportion of web users!) as much higher priority than a handful of Safari visitors. The only standards compliance they give a sh*t about is the WAI stuff on the off-chance somebody tries to sue them.

    making beautiful sites that work perfectly in every browser is the goal, making sites that meet the client’s spec is the priority.

  • http://tendou86.blogspot.com/ Takumi86

    Why IE 7 and below can’t showing this script correctly? though i’m glad that you’d out the code for IE 7 but still wondering why

  • ryan j

    @Takumi86

    Because ie6 and lower don’t support CSS2, which is the spec that sets out the css table functionality. The “display:table-cell” attribute would be ignored because they would not recognize the value of “table-cell”.

    However they would see the HTML, so unless you provide alternative CSS attributes it can recognize (failure cascade? ;)) or hide the table’s HTML it won’t display as a table.

  • BubbaHoTep

    Time for a coalition of small site owners to stop supporting <IE8.

    Sure it sounds crazy, but revolutions are revolutions. If users of IE6 and IE7 begin hitting site after site that pop up a message saying you can not view this site unless you upgrade your browser to IE8 or use FF then we could start a wave.

    Just imagine the possibility.

    yeah I know… It’s like clapping with 1 hand.

    So, we will carry on with the wave instead of taking control.

    So stop your bitching, and constantly wanting some one else to make a change.

    Besides, this is all for IE anyways. Because Microsoft decided to be idiots, they are now loosing in the Browser war… well almost. So while we all suffer, perhaps Microsoft suffers the most. And they should. They should die, they should go to hell and die.

  • Pingback: rascunho » Blog Archive » links for 2008-12-09

  • Pingback: who uses css layout - Page 2 - Small-Business-Forum.net

  • Adam

    Good luck waiting for another 12 years for IE8 to become the standard.

  • Adam

    This was a great tutorial and although very cool, its obvious that flash is the future of the web. One word, “consistency”.

  • Pingback: Cute quote + Recycled article + Unusable technique + Questionable marketing + Awful title = Worst Web Design Book of the Year · Stellify

  • http://www.mariossotiriou.com Marios Sotiriou

    Could someone make an example using this technic in an unordered horizontal menu?

  • freddy

    The title of this book itself is irresponsible, inaccurate, and a blatant attempt to get attention. I’d even go as far to say it’s childish, even if the subject matter is interesting.

    I’ve been coding static sites, RIAs, blogs, etc for a while now and have never had to worry too much about IE when it comes to CSS or display issues (knock on wood). I don’t need this recommended technique and using tables for layout simply because it’s valid CSS isn’t good enough. No offense to proponents of this, but it’s the lazy way out.

  • Pingback: In the Woods - Use Absolute Positioning to Create Equal Columns

  • http://www.egydes.com husien

    good tutorial man !! thanks

  • Pingback: Use Absolute Positioning to Create Equal Columns | zbStudio.net

  • http://www.edgarstudios.co.uk RealToughCookie

    This is awesome!

    i don’t seen no reason why developers can’t start using this technique straight away, IE will just have to make do with a regular faux image for now.

    Im fed up of holding back for IE to be honest, if a better technique is available i will be dammed if im waiting years for IE to sort itself out.

  • Pingback: Woedend 2008 » Blog Archive » handige linkjes

  • http://www.lexiq.ca Dennis

    I agree, we should be using css tables for our layouts while still using floats, margins, etc. for IE (for now…. you live for now IE). When Windows 7 and IE8 is released; screw IE7 and below. We need to force users to upgrade. We need to push the web forward, for our sake, for our user’s sake, and for the whole god-damned internet’s sake!

  • http://anraiki.com Anraiki

    Even if IE8 passed the Acid Test, I will still hate it. Simply because in the “past” it still won’t pass it :\

    1. MSIE 7.x 24281645 (42%)
    2. MSIE 6.x 19726981 (34%)
    3. FireFox 10038015 (17%)

    I don’t see Ie8 gaining a large margin over firefox, and the audience for ie6 + ie7 is not going to vanish over night.

    Until Ie11 or Ie12 comes out, I still HATE IT

  • http://www.reaact.net/ Damien van Holten

    Wouldn’t this be semantically incorrect the same way using tables for visual layouts would be incorrect? We should use tables to display tabular date and nothing else. Even if we do it from the CSS.

    Following that the question arises: why use CSS tables at all? What’s wrong with actual HTML tables (for tabular data)?

  • http://themeforest.net/user/JeffreyWay Jeffrey Way
    Author

    @Damien – No, it wouldn’t. You’re confusing HTML and CSS tables. When we refer to CSS tables, we’re merely referring to the look.

    To answer your second question, you should use HTML tables for tabular data.

  • http://www.firelabstudio.com Aaron

    ISSUE 1:

    I think what’s confusing everyone is the term “CSS tables”. The word tables is right there, and tables are for tabular data. But what CSS tables are is basically saying “display this element *like* a table”, not that it is a table, just acts like one. I think that’s totally the wrong naming convention to use. What if it was “grid” as was suggested, which is what designers could easily associate with layout structure?

    display: table; = display: grid;
    and
    display: table-cell; = display: grid-cell (or grid-block)

    If it were named this way, there’s be a lot less confusion. But as far as the usage of the CSS table attribute goes, it will be VERY useful once the majority of users have browsers to support it.

    ISSUE 2:

    It will be years before the majority of users’ browsers will support its usage. Those saying that the Windows 7 roll-out will help get IE8 out there in large percentages are missing something. The amount of IE6 and 7 users won’t decrease as much as we would like because many machines out there won’t be capable of running Vista or Windows 7 even if they wanted to. Hardware purchases are much less frequent for the “average” user in comparison to us designers/developers. Add in all the businesses that are stretching out the lives of their old PCs to save on expenditures, and it’ll be a while friends. So again it will come down to specific sites and their user statistics determining what we can do.

  • Joe Schmoe

    Can’t say its not interesting, but why do i need to pay $9/p month to get teased by a book review ?

  • InsiteFX

    First off I want to say this, It’s your choice to decide what to do about the older browser.

    I am running IE8 and have always use IE, And if you think that Jeffery is wrong think again. I have a 3 column layout useing javascript that will resize to different widths and change to 2 columns right or left header footer menu bar etc all using css tables.

    If you read the book as qouted it’s up to us make the change now that microsoft has.

    All my systems are running XP Vista etc and all are running IE8.

    All you have to is make a comment on your site stating that if you want to get the most out of this site you will need to upgrade your browser to one of the new ones.

    And if you have not worked with the css tables then you do not know what your missing.

    Build your css table layout and then copy all your ie6 7 stuff over to it not hard to do.

    But remember us as web designers have to make the changes no else will do it for us.

    All new browsers support css2 so there is no reason not to start making the changes, but it is up to all of us to do are part.

    Oh and Jeffery great aricles on CodeIgniter thank you.

    InsiteFX

  • Chey

    Man I like Your Article very much, heard it about four times in a loop.
    To me You described the whole problem confusing people anywhere
    on the planet, in just some nice words and even a very cool pronounciation.
    … Please do more tutorials, maybe sell them but stay doing them
    I like Your words ” Let´s change a few things.” … “… a couple of lines”
    “there is a big differnce” Anyone Who wants to know an easy way-out
    of the confusing browser trouble should watch or just listen to this article.

    Chey