6 Things You Should Know About CSS

There are a few things people doing CSS styling should know when styling a web page. This is a list of things that I consider mandatory knowledge, not because they are basics, but because not knowing them causes too many headaches and knowing them makes styling so much easier.

Inline vs Block elements

Inline elements

Inline elements are elements that go with the flow of the page. They are just added where they are in the web page and don’t disrupt the layout. For example, a link is just added where it is put, right in the middle of a blob of text.

The most common inline elements are: a, img, span, all form elements (input, select, textarea, etc.)

Complete list of inline elements

Block elements

Block elements on the opposite do disrupt the layout because that’s what you want. They always are on their own line (unless specified otherwise using CSS) and often have top and bottom margins applied by default on them. For example, if I put

<p>some text between p tags, the text goes on another line.</p>

The most common block elements are: blockquote, div, fieldset, form, all the header tags (h1 through h6), ol, p, pre, table, ul

Complete list of block elements

Anchors

Anchors specify the relative point used when positioning elements. To set an anchor, just specify the position of the element.

position: relative/absolute/...;

This will sets the reference point of all the children of the element to the top-left corner of that element. So an element absolutely positioned at 0,0 will be placed at the top-left corner of that element instead of being placed at 0,0 of the viewport (or the closest anchor in the tree).

Example

anchored

position:absolute;top:50px;left:50px;
not anchored

position:absolute;top:50px;left:50px;

Selectors

Selectors is another common misunderstood or “under-known” concept. Knowing your selectors allows you to pinpoint elements much more easily. They are powerful and you should know them.

This page has all the information you need to know about CSS selectors

Examples

Line between elements

One thing I like to use with selectors is to put a line between multiple elements, except the first and last (as in my sidebar). To do this, all I have to do is use the + pattern.

ul li + li
{
    border-top: solid 1px #ddd;
}

The first li won’t be targeted by the selector but all the others will, giving them a small border on top.

Disabled style

I often want to style a disabled element differently. To do so, I don’t want to rewrite the styling completely for my disabled element. So with the help of multiple classes and this selector I can do this:

<a href="link" class="SpecialLinkClass disabled">Click me</a>

.SpecialLinkClass
{
    color: #00f; /* blue */
}

.SpecialLinkClass[class~=disabled]
{
    color: #aaa; /* gray */
}

Of course this is just an example, but I’m sure you can find uses for it in real projects.

Box Model

Understanding the box model is crucial when doing CSS styling. The current box model is counter-intuitive so take a look at my previous blog post for more info.

Multiple Classes

Something important that people tend to forget is that you can apply multiple classes to a single element. For example, my rounded headers are defined this way:

<div class="section-title rounded rounded-large">

The section-title class specifies the background color and the font color among other things. The rounded and rounded-large classes allow me to specify things specific to elements that I want to be rounded (more about this in a future post).

Centering

First of all, don’t even think about using the <center> tag. It’s semantically wrong, it’s not good practice and you can do centering without it. The first thing to know is that inline and block elements are not centered the same way so be sure to know which kind your element is (see first point) before trying to center it.

Inline elements

These are easy to center, just add this CSS property to their parent:

text-align: center;

Block elements

These are a lot harder to center, especially if you’ve never done it since it’s all but intuitive. First, since block elements take all the available width, you must first reduce their width. This can be done using different methods:

  • Add the same margin to the left and right of the element
  • Hardcode the width

Sadly, these are the only options for now. A better option would be using this CSS property:

display: inline-block;

…however, the inline-block display value is not yet supported correctly by Internet Explorer.

So now that the width is specified, all you need to do is tell the element that the space on the left and right is the same. To do this, just add auto margins:

margin-left: auto;
margin-right: auto;

or

margin: XX auto; /* XX is a placeholder the first value is applied to the top and bottom while the second applies to left and right */

End

With this new (or not) knowledge aquired you are now well armed to take on those difficult CSS styling issues.

6 comments
Richard on June 12, 2009

A useful article only when you state what browsers support CSS2.X. Otherwise this article is very misleading, as you wouldn’t use CSS2 patterns and selectors, because it wouldn’t work in a few mainstream browsers.
Please correct me if I am wrong.
Thank you.

Michel Billard on June 12, 2009

As you can see on this link: http://www.quirksmode.org/compatibility.html every major browser supports CSS2 now except that IE7′s support is incomplete. As for IE6′s support it’s incorrect.

So yes, you’re right that you have to be careful about what you use in this article especially the CSS selectors because they are not all supported correctly by all major browsers yet (and by that I mean Internet Explorer doesn’t yet support them correctly).

Richard on June 12, 2009

Unfortunately we all have to continue to support IE 6 7. I know, it sucks.
I’m really not trying to push anyone’s buttons, I just think this article needs to state that.
When the article states that this information is “mandatory knowledge”, part of that mandatory knowledge has to include the knowledge of when and where you can use this information.
Does that make sense?

Michel Billard on June 12, 2009

Yes, I guess knowing what is supported by which browser is important to know too. Although, I’m on the side of not supporting IE6 anymore unless it’s critical (client’s wish for example). As for IE7, it supports most of these CSS selectors and patterns.

Richard on June 12, 2009

I blame Vista. I believe had Vista been a success then the corporate world would have embraced IE7 with open arms.
IE6 needs to be abolished, but it continues to haunt us, and so I am unable to use CSS2 patterns and selectors :( I can only dream of a world where I don’t have to worry about IE6 compatibility. Both our company and our clients agree we shouldn’t be supporting IE6, but the fact of the matter is when our clients go into work and power up their desktops, IE6 is the only browser the sys admins will allow, for reasons I do not understand.
Thank you for your website.

Michel Billard on June 12, 2009

Yes, I agree that IE6 has lived for far too long (http://www.crossbrowser.net/37/ie6-to-support-or-not-to-support/). I’m pretty sure it’s in its final days. However, some unlucky developers like you have to keep supporting it.

Thanks for your feedback.


Leave a comment
Name (required)
Email (will not be shown) (required)
Website