KuraFire Network


Latest addition: Jan 19, 2007: Times are changing

Log

Starting with CSS-based design

· By Faruk Ateş on Jul 16, 2005 · 10 comments ·

Subject level: Beginner

One of the things you'll notice real soon when you start working with %CSS%-based layouts is that browsers all have their own, fairly unique, default styling for the various elements. When encountering a display problem across multiple browsers, there's a good chance that one of them is applying different default styling to your elements. To accomodate and eliminate this possibility, I've been using an "initial" CSS file that neutralizes a lot of default quirks.

This post has been superseded by Starting with CSS: revisited. Please update your bookmarks to point to the new version!

This initial.css file has been used in all my projects (clients' and my own) for almost a year now, and over time it's expanded a little or, in some cases, a lot. For my CMS, the initial.css file has become rather large (for something that's only nullifying default behaviour) as it incorporates style rules for the output of the CMS itself. Granted, the bulk of that is layout-related by now, but still.

However, the basic core remains and it's still a useful way to start your CSS styling process. Let's analyze!

This example has been superseded by Starting with CSS: revisited. Don't use the code below!

* {
        margin: 0;
        padding: 0;
}

We begin with the star-selector. According to some, this "slows down the renderer," but I've never experienced anything of that kind. Maybe someone on a really slow computer can contact me so that we can do some tests? Regardless, the above CSS basically removes all default margin and padding from all elements. It's as simple as that.

Now, that may have taken all default margins and paddings away from it all, but in many cases you do want some margins and paddings to certain elements. Just consistantly across browsers. So let's put some good stuff back in:

This example has been superseded by Starting with CSS: revisited. Don't use the code below!

h1, h2, h3, h4, h5, h6, p, pre, blockquote,
label, ul, ol, dl, fieldset, address {
        margin: 1em 0;
}
li, dd, blockquote {
        margin-left: 1em;
}

There. Now all your content doesn't look like ants in a farm anymore. We've given lists and blockquotes their nice indent back, too. Time for the rest!

This example has been superseded by Starting with CSS: revisited. Don't use the code below!

html, body {
        font: 80%/130% Verdana, Arial, Helvetica, sans-serif;
}

First we take away that godawful Times New Roman default and replace it with a nice, smooth Verdana, with fallbacks. The font is specified in percentages to accomodate the greatest flexibility in layout without having to worry about text become too small on IE's "smallest" setting. This is explained in great detail in Elastic Design, by Patrick Griffiths.

Here's some advice for people still very new to CSS: now that the font is set to Verdana on the html and body elements (either would work just fine), you won't have to specify it again on any other element, unless you want to specify a different font somewhere.

Getting back to the initial, there is still some rules remaining:

This example has been superseded by Starting with CSS: revisited. Don't use the code below!

form label {
        cursor: pointer;
}
fieldset {
        border: none;
}

The <label> element has the great usability feat that it allows you to click it in order to put the focus on the form field it belongs to. This can be very helpful in complex forms, but it's still handy in even the simplest ones. There is a problem is, however: browsers don't generally indicate to the user that the labels are clickable. The first CSS rule above makes this happen, showing your users the "hand cursor" that they're used to for links (or better yet: clickable objects).

The second CSS rule is to remind you to use fieldsets. They are not mandatory to create a valid (X)HTML Strict document, but they are still worth using. Here's what the specification has to say about them:

The FIELDSET element allows authors to group thematically related controls and labels. Grouping controls makes it easier for users to understand their purpose while simultaneously facilitating tabbing navigation for visual user agents and speech navigation for speech-oriented user agents. The proper use of this element makes documents more accessible.

Because fieldsets have a default border that many have found to be ugly, people have tended to shy away from using them. But a simple CSS rule as seen above will remove that border for you. Alternatively, you can style the border (or the fieldset entirely) to be more visually pleasing and fit in your design.

Let's look at the end result:

This example has been superseded by Starting with CSS: revisited. Don't use the code below!

/* =INITIAL */
* {
        margin: 0;
        padding: 0;
}
h1, h2, h3, h4, h5, h6, p, pre, blockquote, 
label, ul, ol, dl, fieldset, address {
        margin: 1em 0;
}
li, dd, blockquote {
        margin-left: 1em;
}
html, body {
        font: 80%/130% Verdana, Arial, Helvetica, sans-serif;
}
form label {
        cursor: pointer;
}
fieldset {
        border: none;
}

There, a lovely initial.css file that creates a nice, consistent layout, minimizing display problems caused by browser differences.

Note: this particular version has no padding applied to the html, body { ... } rule because there are many cases where the design will hit the edges of the window. In such situations, this padding would be unnecessary and would have to be removed again, or overwritten later on.

Note 2: for those who recognized it, yes I'm using Douglas's CSS Flags and so should you. It's just very convenient and well-structured.

Note 3: Krijn pointed out the important but fine detail that the above example of html, body { ... } will make your initial body font size become 80% of 80%. You may wish to change that for your own purposes.

Bookmark: Newsvine del.icio.us Digg

Comments

10 comments

#1 · Krijn Hoetmer · Jul 16, 2005 (12:36)

For beginners; note that
html, body {
font: 80%/130% Verdana, Arial, Helvetica, sans-serif;
}

and
body {
font: 80%/130% Verdana, Arial, Helvetica, sans-serif;
}

are actually quite different. In the first rule font sizes in body are 80% of 80%.

#2 · Faruk Ateş · Jul 16, 2005 (14:59)

Good point, Krijn. That's something people should keep in mind, indeed.

#3 · Sander · Jul 17, 2005 (18:27)

Although I used to teach the * { margin: 0; padding: 0; } rules, lately I've been moving away from this - at least for form-heavy sites. There are too many permutations of padding messing up form elements, especially buttons.

You don't want to go down the path where you have to re-create Mozilla's default forms.css input[type="button"]:active:hover e.a. rules, because it's virtually impossible to do this in a way that doesn't make things worse in IE or Opera or Safari or unknown browser of the future.

Not doing that, however, while having the star-rule, leads to things like the form-buttons you have here right now (and I know the redesign isn't finished yet), which don't look or act like buttons at all for my Mozilla.

Given that styling form elements is unspecified, you *know* that in the long run you will always run into problems here, and since IE doesn't support the obvious solution of
*:not(input):not(button) { margin: 0; padding: 0; } nowadays I'm just enumerating those elements that I know I use and which are safe and actually useful to have dimensionless:
p, h1, h2, h3, h4, h5, h6, ul, ol, li, .... { margin: 0; padding: 0; }

It's minutely more work to set up early on, and I miss the design-efficiency of starting out in a world where everything was guaranteed 'dimension-less', but in the end it saves me a whole lot of head-aches.

#4 · Faruk Ateş · Jul 17, 2005 (18:46)

Hmmm, I wasn't aware of Mozilla's intensive form button styling and, quite frankly, I'm not sure I'm happy about it.

The problem with form buttons is that there is no acceptable way of dealing with them. None. The only somewhat-decent thing to do is to not touch any form field or button with CSS at all, but that tends to make things look absolutely shoddy in different OS's, browsers or color-configurations (like I have).

#5 · Scott L Holmes · Jul 25, 2005 (00:45)

Typically, the dark background sites are more on the artistic side.

Here's a couple
Making Art With Machine Code
Quaderno
FL Studio

Cheers!

#6 · Faruk Ateş · Jul 25, 2005 (10:37)

Scott... I think you placed your comment in the wrong entry there ;)

But, it's true that the dark backgrounds are generally more artistic.

#7 · Adrian · Sep 12, 2005 (11:36)

please somebody help me...

i love WP-1.5.2 default theme as i'm using it for my blog. i want to make it bigger with variable-width.

a test layout can be found at http://adrian.bahana.net/test and the source can be downloaded at http://adrian.bahana.net/test/test.zip

i'm sorry, my english is too bad to explain my problem...

#8 · Arnika · Sep 12, 2005 (23:25)

thank u !

#9 · coyr · May 29, 2006 (01:58)

Hi faruk, cool stuff, thanks for this idea. I use it in my last proyect, and help me a lot.

I want to ask you the reasons why do you don't recommend to use *{} the universal selector. Sorry i can't find the answer, but i really know this exist.

Thanks again.

#10 · coyr · May 29, 2006 (02:13)

don't care. I just have seen the answer :P. Thanks

All times are in CET. It is now 19:44.