By Peter Bell

CSS Frameworks and Semantics

I've been doing a lot of thinking about CSS recently. We need a framework for our sites so we can create them more efficiently, but I'm not sure that I love the open source offerings . . .

It is interesting to see the debate that was more common in the CF world now move to the CSS community. Why shouldn't you use a CSS framework? Funnily enough, pretty much the same (mostly lame) reasons raised in the CF world. It is OK not to know everything going on under the hood as long as it doesn't come back to bite you, and a little extra CSS to download is a small price to pay for more rapid development for most use cases. That said, I still don't like OSS CSS frameworks.

Smashing magazine has a good overview of the topic (as usual, the comments are as instructive as the article). The issue I have with CSS frameworks is that they are usually not semantically meaningful (well, if you want to be pedantic, their semantic meaning refers to the positioning rather than the meaning of the content).I want to have meaningful divs with names like header, primary navigation, secondary navigation, primary content and the like, so if I decide to move primary navigation from the topbar into a left or right hand sidebar it doesn't affect my content (why should it - it is only a presentational change). There are issues with such an approach (often you need to put content like a "testimonials" pod into your secondary navigation area), but it's better than just having names like topbar and sidebar for your divs.

What I'm working on now is a framework that'll work for most web apps that'll allow us to have semantically meaningful HTML while still having flexible CSS and not having to start from scratch with each project. I'll keep you posted on how it goes!

Comments
Pete, I was reading through Bullet Proof Web Design last night (thanks Javi!) and they mentioned something that had never crossed my mind before, but may be a common CSS technique. It involved setting an ID on the body and then altering page elements based on that body ID. I am sure you have come across this in your research, but it was new to me, so I thought I would mention it.

It helps to keep things as semantic as possible. Let's say you have your primary content area:

div#primarycontent {
width: 400px ;
}

but on one page, the client (cause they are crazy) wants to mess with it. Traditionally, I would come up with an alternate DIV name like: primarycontentalt or primarycontentsmall, but this has NO semantic meaning. The better thing, I am seeing now, is that you put an ID on your body:

[body id="careers"]

... and then in your CSS, you simply update the primarycontent CSS with a more specific rule:

div#primarycontent {
width: 400px ;
}

body#careers div#primarycontent {
width: 550px ;
}

Now, the CSS remains highly semantic, but very flexible due the usage of body tag context.

ANyway, this kind of rocked my world cause its a total departure from how I normally do things so I thought you might be interested. Especially when it comes to things like "sidebar"; you could still just have "secondarycontent" but then in body context it floats left and in other body context it floats right.

ps. good luck with the world tour.
# Posted By Ben Nadel | 9/27/07 9:23 AM
Interesting - I read bulletproof (again, thanks Javier - he should be getting a commission on sals of that book!) but missed that point. Will definitely play with this!
# Posted By Peter Bell | 9/27/07 9:58 AM
Thats right give credit where credit is due! :) Glad you guys have been reading that book. I'm sure you are loving it. Don't steal my thunder Peter with your CSS Framework posts! I'm still working on mine and already been working with the crew here at my new job to take it to the next level. I plan sometime in the coming weeks or months to make some posts on the arc90 blog. I'll let you know once I got that out. Have you been enjoying the book and some of the great reads out there Peter? Check out Smashing Magazine daily. I always do as its a great blog that brings everything together for me in one place.

Commission? I love the sound of that! I'll have to bring it up to Dan Cederholm. Lets see if he has the money to pay what I want though! ;) Hope you guys are doing well and that you both are enjoying the book. Dan Cederholm did a great job with it. I know he has something else he's cooking up but no one knows yet what his next book will be on.
# Posted By Javier Julio | 9/27/07 1:42 PM
@Javi,

Yeah, I just recently noticed Smashing Magazine. All the posts I have seen are top quality design and CSS style posts. I gotta start paying attention to that.
# Posted By Ben Nadel | 9/27/07 2:05 PM
Check out
http://www.code-sucks.com/css%20layouts/
for variations on a theme. This person has layed out a bunch of combinations for header/footer/content/nav.
# Posted By Phillip Senn | 9/27/07 2:11 PM
After spending months trying to get our CSS to work cross-browser (and then with MSIE 7), we ditched it in favor of the Yahoo UI library. http://developer.yahoo.com/yui/

Took 45 minutes to drop YUI Grids into our layout templates correctly, then the rest was cake. I'll be bringing this up during Ignite MAX on Sunday.

I can't see trying to build a large site from scratch without it.
# Posted By Adrian J. Moreno | 9/27/07 2:20 PM
@Ben

Yes definitely make sure that site is bookmarked. Without a doubt that website you should be visiting everyday to see if there is anything new. You have no say in this. :) Once I'm back from MAX, the day after I'll be moving into my new place so I'll be settled. We'll have to meet up then doing some CF chatting and some celebrating.
# Posted By Javier Julio | 9/27/07 2:45 PM
Sounds good. And, if possible, I'd love to come check out Arc90 just to see what kind of a place you got up there. But, I don't know how cool they would be with that.
# Posted By Ben Nadel | 9/27/07 2:49 PM
I've got some experience creating proprietary css frameworks for .Net environments, with somewhat mixed results - I'm getting better at it. What I have created works well in my hands but there are a few situations where the framework can make life more difficult for others. Any time a developer wants to use a third-party UI component, there's always the danger that something in the css framework will override something in the component. So it pays to be as specific with your css selectors as you possibly can, to minimize the risk.

My formula is something like this: Always use strict mode doctypes. Start with a baseline css file that works for standards-compliant browsers. Create additional css files containing any overrides necessary for individual browsers, and use server-side UA detection to determine whether to serve them. Don't use hacks to create browser-specific styles.

One technique I use that is maybe a little unique is that I give every structural element a classname, and when I reference them I include every ancestor class in the selector, as follows:

.Home .Content .Sidebar .SidebarInnerWrapper .Links{}

These selectors get pretty lengthy, but they make it very easy to tell at a glance what elements will be affected by a particular selector - kind of like a namespace for the element. (As long as you're disciplined enough to enforce it yourself!) Note that I also restrict myself to only using class selectors. A big issue I used to run into a lot was that other developers couldn't figure out why their elements were being affected in ways they didn't anticipate. Then they'd have to dig through a huge style sheet to figure out what if their element was being referenced by ID or class name, whether it was being affected by some global selector or descendant selector, etc etc. On top of that, .Net auto-generates element ids for controls, so a class name is your only option in some cases. Even though css gives you a variety of ways to skin the proverbial cat, I find it best to sharply limit which ones I use when it comes to creating a framework to be used unpredictably.
# Posted By Tom Lee | 9/27/07 4:38 PM
I've used the YUI Grids css on a couple sites and I really like it. Very fast an easy, and to be honest I've found it to be more semantic than the majority of the CSS I see out there, even if my class names have to have that "yui" junk in them! Some say that CSS frameworks lead to div-itis, but I feel like YUI is a good balance between requiring lots of elements, and control over the layout. Sure, a very simple layout may be doable with fewer elements, but if you want a complex layout or have a large site with lots of page templates where css can quickly get out of control, then YUI ultimately does a great job. The display is reliable and tough when it comes to cross browser compatibility.

Using a CSS framework doesn't mean that you can't give meaningful names to your xhtml elements. For instance with YUI, most of the styles are class names, not element or id styles, which means you can not only give whatever IDs you want to your elements for your presentational styles, but you can also stack additional styles on top of the yui base classes.
# Posted By Rachel | 10/15/07 3:50 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.