The Idea Behind CSS

tagged CSS by Jon

If you have found your way here, you have more than likely used CSS to some degree. The syntax is easy to learn, but don’t be fooled into thinking that the concept behind it is. The idea behind CSS is a complex one.

This article will explain the idea behind CSS and how it will simplify web development and give you the freedom to design mind-blowing websites.

The idea behind this article

I’ve been thinking for a while about a good topic to get this blog rolling. I went back and forth on a couple different ideas until a little incident the other day brought me to the concept I had been looking for. I’m currently taking online college courses to finish up my degree. As of the time I’m writing this, I am taking a beginning HTML class (lame, I know…). One of the features of the online college is a forum where I interact with the teacher and other students. One of the students posted a complaint that she shouldn’t have to learn about the ‘outdated’ em, strong and h1 tags. And, that there were better ways to format text and gave an example of a font tag. I replied to her explaining that those tags are far from outdated. I even quoted the W3C (World Wide Web Consortium) and felt that I had a pretty solid argument. She was quick to respond telling me that I “clearly don’t know what I’m talking about”. If possible, I would have reached through the monitor and strangled her! The instructor was quick to mediate and put an end to our argument.

Separating presentation from structure

Cascading Style Sheets (CSS) is a language that is used to describe the presentation of markup language such as HTML and XML. The W3C states that, “by separating the presentation style of documents from the content of documents, [CSS] simplifies Web authoring and site maintenance”. In other words, CSS is meant to separate presentation from structure and make your life as a web developer easier. I consider those those two things the laws of CSS. If your experience is ever contrary to one of those two things, you are definitely doing something wrong.

This is how I define presentation and structure

Presentation
This is the style of a document. It consists of the colors, fonts and even sounds that are in a web page. The idea behind good presentation is to enhance the content in a way that will make it more accessible to the user.
Structure
This is the markup that describes the content and semantics. A good structured document will make it more accessible to other machines or programs. The structure contains meta data and descriptive tags that will allow it to be interoperable.

To separate the two, keep your styles in an external stylesheet and out of the head or body of the document unless you have a really good reason not to. Overusing inline styles is just as bad as using the font tag and completely contradicts the idea behind CSS.

It is still common to throw structure, presentation and functionality all into one big tangled mess and call it a website. However, the advancements of web languages and standards are leaving little excuse to do so. Even browsers are starting to play fair and are more standards compliant.

  1.  
  2. <table CELLSPACING=0 CELLPADDING=3 BORDER=0 WIDTH="674" HEIGHT="100%"
  3. background="rawimages/bottombanner.jpg" STYLE="border:0px solid #FFFFFF;">
  4.   <td VALIGN="" ALIGN="center">
  5.     <div CLASS="text">
  6.        
  7. <b>sen - si - tive. </b> <i>(adj.)</i> 6. Readily altered by the action of an agent.
  8.     </div>
  9.   </td>
  10.  </tr>
  11. </table>
  12.  

The is actual HTML that I borrowed from a website. Notice the amount of markup compared to actual content. All of this was made to write a definition.

  1.  
  2.     <dt>sen - si - tive. <i>(adj.)</i> </dt>
  3.     <dd>6. Readily altered by the action of an agent.</dd>
  4. </dl>
  5.  

Again, compare the amount of markup and content. This will accomplish the same as the above but is much lighter, has semantic markup and will make your life a lot easier to write and update.

Good CSS requires good HTML

Before you can go busting out fancy schmancy websites with your amazing CSS skillz, you need to know the ins and outs of HTML. Not one line of CSS should be written before your structure is solid. You need to use the appropriate tags that describe the content. Put your titles in header tags, navigation in lists, tabular data in tables and so forth. Your document should be neat and clean even without any kind of styling involved. After you have done this, add a few div’s and start adding presentation to your website. Go around to some of your favorite sites and try to read through them with the stylesheet disabled. You will quickly notice who has and hasn’t mastered this concept.

The ability to do this is sometimes impeded by the site’s design. I have worked with designers who have no web experience and lay out a website that is impossible to build bending a few rules and using hacks to get IE 6 to cooperate. Not all designs will allow you to develop a standards compliant website. That’s why it is important for the designer to not only be familiar with HTML and CSS, but to have a vision of how the website will bend and fold under different conditions.

Nicely written HTML also allows you to take advantage of the cascading part of stylesheets. This is one of the most important features and shouldn’t be ignored. Instead of adding a class or id to every single element, use just one id that has been added to the parent container. Consider this example.

  1.  
  2. <div id="beverages">
  3.         <h1>Preffered Beverages</h1>
  4.     <ul>
  5.         <li><strong>Caffeinated</strong>
  6.             <ul>
  7.                 <li>Rockstar</li>
  8.                 <li>Mountain Dew</li>
  9.                 <li>Pepsi</li>
  10.             </ul>
  11.         </li>
  12.         <li><strong>Non-Caffeinated</strong>
  13.             <ul>
  14.                 <li>Hot Chocolate</li>
  15.                 <li>Orange Juice</li>
  16.             </ul>
  17.         </li>
  18.     </ul>
  19. </div>
  20.  

With one id I can easily style all of the elements inside of the div. This leaves the markup clean and easy to maintain.

  1.  
  2. #beverages {
  3.         border: solid 1px #333;
  4.         padding: 10px;
  5. }
  6.  
  7. #beverages h1 {
  8.         font-family: Geneva, Arial, Helvetica, sans-serif;
  9.         font-size: 2em;
  10.         color: #CCC;
  11. }
  12.  
  13. #beverages ul li {
  14.         font-size: 1em;
  15.         color: #666;
  16. }
  17.  
  18. #beverages ul ul li {
  19.         font-size: .6em;
  20.         color: #999;
  21. }
  22.  
  23. #beverages ul li strong {
  24.         font-weight: bold;
  25.         text-decoration: underline;
  26. }
  27.  

Here is an example of how I would use the cascading attribute of CSS to style the contents of the #beverages container

Tables vs. CSS

HTML was originally developed as a means of formatting plain text and inserting images into documents. Creative minds quickly matured past the visual capabilities of HTML and began to hack the markup in order to accomplish the design they envisioned. This is where table based layouts came from. Tables were never intended to be used for a site’s layout. They are and always have been intended to, “…arrange data — text, preformatted text, images, links, forms, form fields, other tables, etc. — into rows and columns of cells” (W3C).

I have seen both extremes of table use in website. Those who use tables for every aspect of a site’s presentation and those who refuse to use them even for their intended use. The latter is even worse in my opinion because it fills the document full of div’s and can be sloppier than the former. The overuse of div tags has been referred to as DIVitis. Tables are excellent when used for their intended purposes. Table attributes like the summary as well as the table head, body and foot allow you to better describe the elements inside the table.

Breaking out of the box

Molly E. Holzschlag wrote an awesome article on A List Apart about breaking out of the generic grid design and taking full advantage of a CSS layout. In the article, she contrasts urban planning in two different cities. One has planned city blocks that make up a perfect grid and the other is a giant mess of winding roads and irregular city blocks. She compares this to web design.

The web is maturing, our approaches to it changing, our opportunities for innovation and creativity more apparent than ever before. We’re not limited to a planned city; we can create unique designs and have them work well. As a combined force, the empowered veteran and today’s spontaneous youth inspire a provocative notion that the way the web looks today is nothing like the way it’s going to look tomorrow. And for the most part, I’m sure you’ll agree that’s a very good thing.

Grid based design has its place and shouldn’t be considered a bad thing. The point lies in the fact that CSS gives us the freedom to break out of the box and leave limits of table based layouts behind. As far as web design goes, we are just getting started.

Leave your mark...

If you liked this article please leave a comment and share it.

Add a comment Digg Stumble Upon Reddit

Gravatar
Santiago
December 4th, 2007

You did it again, I’m subscribing to your RSS feed ;)

Gravatar
Andrew
December 5th, 2007

I can’t believe someone actually argued for the tag and then had actually went on to say that you were wrong.

I think my blood-pressure ratcheted up a notch just reading that.

You should have brought up the tag…
;)

Gravatar
Andrew
December 5th, 2007

The word missing above was ‘font’ and then later ‘blink’.

Gravatar
Dewald
October 29th, 2008

Hey, looking good man. Enjoying your articles and love the layout.
Just a little wording error. “use just one class that has been added to the parent container.”

I believe you mean “id” not “class”, since your illustrating your point with id’s after that statement.

Gravatar
Jon
October 29th, 2008

@Dewald

Good observation. Thanks for pointing that out for me.

Gravatar
Sérgio Soares
January 30th, 2009

nice article

hug from portugal

Add a comment

This comment system uses Gravatars for customized avatars. Get your account here.

If this is your first comment, it will be held for moderation. I'll get to it as fast as I can. Thanks :p