views:

926

answers:

9

I'm just wondering what others do in this respect:

Do you try to keep positional CSS (layout) separate from color/flavor CSS (color, background-color, background-images, font-size and family) ?

Use two stylesheets? Combine two stylesheets server-side? Abstraction layer for the CSS? or you don't even try?

I know sometimes after working on the same web project for six months I can usually live with the positional CSS but end up wanting to change the colors/images.

+10  A: 

I tend to keep all the CSS together, without separating "color styles" from "positional styles" or "layout styles". I find that when I often try to debug a specific "module" it's easier to have all the CSS rules applied to one selector, and not spread out over the style sheet.

However, I do suggest you read Creating Sexy Stylesheets over at thinkvitamin.com. One thing I do is list the rules in a certain order everytime, so I know within the declaration block where to find what I want.

More info at Jina Bolton's http://creatingsexystylesheets.com/

Mark W
Oh, and as a postscript, at Scripps (http://foodnetwork.com, http://hgtv.com), we keep several css files separate based on what they are for (i.e. header.css, footer.css, search.css) and concatenate them on a development server before pushing them to production.
Mark W
@Mark W: Any special way you do that? I've been thinking about doing that, and then passing it through a minifier afterwards. Are you using a simple script or a package for that?
Ross
Mark, Scripps is actually one of our clients :) Small world!
Plan B
@Ross: We have a script on our FrontEnd server that takes all of these separate CSS files, and pushes them through a CSS minifier, than concatenates it into one file. We then take the output of that file and use that as our production style sheet.@Plan B: Cool!
Mark W
I'll have to check out this site more from home this week. Unfortunately, the website name/URL is not very proxy friendly.
tyndall
A: 

I keep everything in one file and only provide different files for alternative styles (e.g. for printing).

Within that file I keep the overall layout (columns, headeer & footer) seperate from the actual contents (paragraphs, headings, lists...)

I am used to thinking object oriented, so I group the styles for different objects (menus, blog posts) together. From that perspective, colour and position both belong to the same object and therefore are kept together.

I am wishing for the ability to define colours once in a style sheet, assign them a declarative name (e.g. 'HeadingColour') and then use the name when assigning the colour to a selector...

Treb
I like this style too. Maybe its just that CSS files can become so large and hard to maintain. I think many of the better WordPress themes I think use this CSS style. (+1)
tyndall
+2  A: 

I keep everything together in a single file and use the folders feature in CSSEdit to keep it organised. Web design company Viget have a blog post about this technique here.

Philip Morton
+1 - I do something like this with commented sections - although, I don't have an editor that does anything with this information. I'm a Windows / Linux guy, anything like CSSEdit for those platforms?
tyndall
+3  A: 

You'll find that in large-scale projects, layout and color/flavor CSS will (if you're smart about it) usually just happen to be separate. Firstly, if you're catching yourself setting color/font-size/font-family style rules over and over, you're wasting your time. Typically you should define your fonts in one place: the body tag. Any additional fonts should be defined in their respective tags... h1, h2, p, etc. In my opinion it's not good practice to give these tags positional directives; they should be placed inside a div that will be responsible for their layout. Same goes for color and font size. I think the only exception to the rule would typically be background stuff, which is especially true if you have lots of gradients and fancy things like that.

Really what it comes down to is planning; a well-planned project needs very few color/flavor style rules. So to answer your question, yes, I usually have a "Global.css" file that defines all of my fonts and colors for h1-h5, a, p, and any other tags that will contain text.

Edit:

Usually, since the projects I work in are fairly large-scale and have a number of different modules, we separate the styles with in a sort of hierarchy; this makes sense because of the way CSS works -- as long as you don't change the style-rules put into place at the "base" (or in our case, the global.css) somewhere down the line, the styles will stick. This helps because when we want to modify the font of our site, we simply change the font-family rule at the "body" tag, and it will propagate throughout the entire site.

So, our stylesheet layout works something like this:

Global.css (Fonts/Text/Primary font colors)
--> genericBase.css (basic page structures such as columns that are used throughout the site)
--> nav.css (left-hand nav and/or top nav bar)
--> formLayout.css (labels, inputs, fieldsets, any other form stuff)
-----> forums.css (individual modules' styles that may deviate a bit from the usual structures, or simply things specific to those pages)
-----> blogs.css
-----> messages.css (etc etc etc)

The arrows here are meant to imply the "order" of the files in the hierarchy. The longer the arrow, the further down in the style-sheet the rules these files contain would be, if we had put all of the styles into one file.

So you see, the whole idea is to start with very general styles and work your way down to the most specific. Remember that the order in which your CSS files load matters to the browser. You can use this to your advantage. The interesting thing is, by the time we get to our specific modules' css files, we have very few styles to write because most of the other important stuff has actually work itself out along the way.

So like I said, planning is vitally important. I've found that this methodology makes it easier to "debug" my styles, and I use almost no hacks at all, usually only for silly ie6 stuff.

Let me know if you need more information. I'm glad this is helpful to you.

Plan B
This does help. Can you update your answer to include a few other details? like if it doesn't go in global.css where does it go? what are some of your naming conventions for files.
tyndall
+1  A: 

I separated my layout and color styles recently, and I now have several css files, which i import as follows:

<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<link rel="stylesheet" type="text/css" href="style-default.css" title="Default Style" media="screen" />
<link rel="alternate stylesheet" type="text/css" href="style-bw.css" title="Black and White" media="screen" />

All layout is in style.css, then colors are in style-default.css. This way I have a standard style, but users also have the option to change the colors. This not only offers options for the user, but it makes it easy to make color changes without touching the layout (I tend to change my colors much more often).

In Firefox my color options show up in the view menu under "Page Style."

chills42
+1 - I had not seen this "alternate stylesheet" thing before. Only works in Firefox though? I have seen a few blogs/sites where they (the developers) change the stylesheet reference server-side to allow the user to select their own color scheme.
tyndall
This has a pretty good explanation of alternate stylesheets. http://www.alistapart.com/articles/alternate/
chills42
Alternative stylesheets also work in Opera (yes, not the most common browser - but its the one I use ;-)
Treb
+2  A: 

I used to separate them but it was more difficult to maintain. The problem is that many "formatting" properties will have an effect on the layout and many "layout" properties may actually be design.

Some examples:

While "border" may be considered a "formatting" property, they do take some amount of space so you will need to adjust your layout when setting or removing borders.

"line-height" is tied to the font-size and may be considered a "formatting" property, but it has a huge influence on the size of your elements and how they vertically align each other.

Margins and paddings are sometimes needed for the layout and sometimes used just for formatting.

If you think hard about it, there are very few properties that actually are purely formatting or purely layout.

It's often easier to just keep everything in the same file and try to keep it clean by having your declarations orders, related properties grouped, etc.

Vincent Robert
+1  A: 

I've fallen into the pattern of separating my CSS out into the following:

  • Layout (headers, footers, logos - general chrome)
  • Typography (fonts, sizes, re-usable inline font styles)
  • Widgets

The latter category is generally made up of CSS code I re-use between projects, and usually gets split down itself into:

  • Forms (left-aligned, right-aligned, styles for required fields, etc)
  • Grids (2-col, 4-col, etc etc etc - about 20 or so varieties)
  • Hacks (IE/other CSS hacks)
  • Other stuff (AJAX widgets, toolbars, comment boxes, etc - anything re-usable)

For colours, I just keep a cheat-sheet text file around. Keeping them in a separate stylesheet will probably only work if you are very, very disciplined.

Keith Williams
+1  A: 

I have started to use classes to handle colors specifically.

.element{margin, padding, layout stuff}

.ourcolor{#some color}

It lengthens the class attribute though: < div class="element ourcolor" >

However, I can reuse the color: < span class="ourcolor" >Some text

So far I prefer it as adjusting colors is much easier.

+1  A: 

As Mark W pointed out, Creating Sexy Stylesheets is a fantastic read. One thing they do advocate is separating the styling concerns through a framework:

  • screen.css - A screen CSS file can either have all your styles you want to be used for on screen, and/or can import additional styles, such as the following:
    • reset.css - A reset CSS file can be used to “reset” all the default browser styling, which can help make it easier to achieve cross-browser compatibility.
    • typography.css - A typography CSS file can define your typefaces, sizes, leading, kerning, and possibly even color.
    • grid.css - A grid CSS file can have your layout structure (and act as the wireframe of your site, by defining the basic header, footer, and column set up).
  • print.css - A print CSS file would include your styles you want to be used when the page is printed.

If you follow this pattern, the colors would go in your typography.css, and layout would be in your grid.css.

Mike Griffith
I left a comment on my answer concerning how my company concatenates our style sheets together. I would suggest that you do a similar practice on your production sites, since each @import is an additional HTTP request that can slow page-load time.
Mark W
+1. I like these ideas. I haven't started using the reset.css yet. Seen it before. I might try it on my next project though.
tyndall