CSS: It’s not that hard.

This article isn’t meant to hash over the pro and cons of which one is better in the age-old (at least since 2001 or so) css vs. tables debate. I really don’t care which one you use. If you can get your work done quickly, make money, and sleep well at night, then that is really all that matters. I’m not here to judge. That being said, here’s a tip: css isn’t that hard. Please stop moaning about how it’s hard to make a css layout work across all browsers and platforms, or at all, because it’s not. I spend very little time bashing my head against a wall trying to make my design work. If you do bash your head a lot, then here are a few things that I do to ensure that the code will work. You don’t need a framework, you can get by with just a few lines to start with. I’m not necessarily going to get into the how’s or why’s, just go by these few things (if you don’t already) and I guarantee it will make your life easier:

Set everything to zero

You may have seen frameworks that have a ‘reset’ style sheet in them, basically what they do is set all elements to zero, so that by default everything has no margin and no padding. The purpose of this is to override each browsers default settings, which are all different. Some of the resets I’ve seen are overly complex, but they are good. They take into account all of the cases of the different elements, and set them accordingly, but one way to get a quick start on this is to use this bit of code at the top of your css:

html * {

margin: 0;
padding: 0;

}

That sets everything that is inside the html tag to have a margin and padding to 0. You might want to go back and ‘unreset’ some elements. Maybe you want your lists to have some default margins, for example. At any rate, the point of this is so that you know what any given element is going to do by default.

Float:left + Clearfix

For layout, float everything left and make sure the elements after that clear it. Do not use float: right to layout elements, that will be a rabbit hole you will not get out of. This code, which I’m pretty sure I originally got from Position is Everything (Which, by the way is a great resource, but honestly is something I haven’t had to visit in a long time, fortunately) is something you can set on your floated elements to make the element below it clear it. It doesn’t really matter why it works, it just does.

.clearfix { display: inline-block; }

.clearfix:after {
content: “.”;
display: block;
height: 0;
clear: both;
visibility: hidden;
}

Addition & Subtraction

First grade math, people. If the width of your design is 960 pixels and you have 3 colums, the widths all those columns plus their left and right padding should equal exactly 960. So as long your math adds up and nothing inside those elements is wider, you will be good to go.

Don’t use left and right margins (or negative margins)

Use padding instead of left and right margins. IE6, particularly, will become much harder to work with if you put left and right margins on your elements, this is just the natural order of the world, so get over it and don’t use them. Also, negative margins might seem like a good solution, and they should be, but you are safe just staying away from them.

Test constantly

I work on a mac, so as soon as I get even the basic layout going, I flip over to a PC to make sure it works there, and I keep doing that at every little step of the process. If you wait until you have the whole thing coded to test out somewhere else, you are setting yourself up for failure and frustration. This is probably the most important thing to do. It’s basic troubleshooting and it makes it much easier if you only have to look at the last couple of things you’ve done to see where it went wrong.

So, again, it’s really not that hard if you set yourself up for success. Complaining about IE6, or windows, or how css doesn’t work the way you want it to is waste of energy and brainpower. Just accept that these are the parameters you have to work with, get over it and get the job done.

5 Comments

  1. Joel

    Finding out why something works the way it does will pay dividends in the end, since you can explain to your peers why certain problems arise instead of ‘I don’t know it just works’. Also, if you put the effort in you might find a better way. That clear fix you just posted has a great alternative. If the div wrapping the floats has overflow:auto, or overflow: hidden on it, it will need to calculate the heights of the floating elements within and pop back into place. Clear fix ref: http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/

    Reply
    • Jay

      I’m not sure that’s the perfect solution for that issue, but maybe neither is my solution. And you are correct that knowing why something works is important. I wasn’t trying to get into all that, whatever works for you is whatever works, period. If you want to use tables, go for it. I just get tired of hearing people complain about it or go in circles arguing about it. The main justification that people use for tables is they use them because they know what to expect. I use a set of tools that allow me to know what to expect when using css, as much or more as when I built everything with tables. It doesn’t mean it’s the perfect solution. But at the end of the day, you have to get the job done, and it doesn’t matter at that point why it works, just that once you find a solution that works, use it over and over again.

      Reply
  2. Gene

    I know i’m guilty of waiting too long to test my code out. That’s bitten me in the butt several times…

    Reply
  3. Josh

    I have to agree with the sentiment that CSS isn’t really that hard. The thing I’ve found that really helps me is staying consistent with the way you markup and style things with CSS.

    If you have the same routine with regard to this you’ll notice the same problems pop up each time, and you’ll be better equipped to fix it (or avoid it altogether since you know it’s going to happen).

    Reply
  4. lance22

    Eric Meyer is pretty much the God of CSS or in the very least one of it’s chief evangelists, yet even he says that there are a few limited situations where a table is the better solution.

    CSS is the best tool to ever come out as far as managing and instantly changing the appearance of sites on a global level. But to suggest that it has some small limits here and there is like cutting a loud fart in church.

    There are some database-driven containers that are well served by tables who’s appearance should be managed by CSS. You can plant a series of spans inside of a div row container but the problem is the spans don’t scale vertically very well when the span next to it has major wrap going on. You can fake it out by avoiding borders and shading but the bottom line is the span doesn’t scale vertically very well if there is no content. No big deal at – most people would be fine with that – but if you want every cell to have funky jquery affects (as I like to do) then not so much. But that simple, clean table will let you do it with no work arounds. Imagine that.

    The old addage is still true … css for layouts and jquery / mootools animations but tables for datadriven controls where height and width has to match precisely and you can’t predict how much wrapping will occur.

    Reply

Trackbacks/Pingbacks

  1. The not-so-great debate - Jeremy Frank - [...] more tips read CSS: It’s not that hard, at Unmatched Style. This entry was written by Jeremy, posted on…

Submit a Comment

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

More News & Articles

Sign-Up: Enhancing Conversions through good UX

Sign-Up: Enhancing Conversions through good UX

Optimize your website’s sign-up process by focusing on clarity, ease, and user mindset. Clearly explain the purpose, benefits, and steps, and use social proof to build trust and drive conversions.

The Essential Guide to Getting Started on Freelance Writing

The Essential Guide to Getting Started on Freelance Writing

Explore the lucrative and fulfilling world of freelance writing with our essential guide. Learn about specialties like blogging, social media, article, and technical writing. Build a portfolio, find work, set up your business, and discover the potential earnings. Embrace the freedom of working from home and follow tips for success in your dream career.

EMAIL NEWSLETTER