Forum Moderators: open
I imagine this is a common need and wonder h0ow others have approached it. Any thoughts?
Thanks,
Ross
Occasionaly I would find that I had to climb up into an attic, or even down into a shallow crawl space to reach the problem area. These were difficult places to get to but since the specific task only required certain tools and a few select parts, I did not take everything I had back in the van with me into the far reaches of the dark and dank crawl space: I took just what I needed and nothing more.
A huge fully stocked, well equipped style sheet is a lot like my old service van: it has EVERYTHING needed to to the job, and then some! It's agreat feeling knowing you have all the tools for the job... But! Do you want to lug all those tools and parts down into the crawl space or up into the attic when all that is need can be carried in a small tool belt? I didn't think so.
Caching? Point to note: How are your visitors entering your site? Are they coming through the "front door" exclusively? Or have your SEO efforts finally paid off and visitors are accessing specific pages directly. Do they need their browsers to parse your 25kb, "every-thing-but-the-kitchen-sink" style sheet? Or will that directory, grouped element or page specific 1-2kb style sheet serve the purpose?
Don't be afraid to write small, limited use style sheets that get the job done without unloading the entire van.
sitewide.css
directory.css
color-schemes/red.css
color-schemes/blue.css
elements/forms.css
elements/lists.css
furniture/antique.css
furniture/modern.css
business/accounting.css
business/real-estate.css
arts/night.css
arts/cubist.css
layouts/three-column-header.css
layouts/two-column-liquid.css
layouts/shopping-cart.css
You can and should use specific style sheets for large sites that have a lot of diversity. Don't try to cram it all in a super-mega-master.css file... It doesn't make sense to when you might only need a few styles to do the job.
Now say you have a product page that offers several antique desks plus an order form:
You might use:
<link rel="stylesheet" type="text/css" href="antique.css">
<link rel="stylesheet" type="text/css" href="../elements/forms.css">
Using tailored CSS for specific applications can make life much easier both now and in the future. Don't be afraid - just specific. Plan well, style happy! ;)
You might have a general style sheet that includes core body styles as well as styles for common elements. For instance, you might declare all of your element/font combinations, line-height, padding, etc., but leave the colors for individual color style sheets. This could be an efficient way to style pages that, depending on the theme, may have different color schemes. The default, or general styles for fonts and elements apply for any of the color-themed pages, but separate color-handling style sheets address the color schemes:
<link rel="stylesheet" type="text/css" href="global.css">
<link rel="stylesheet" type="text/css" href="blue.css">
then you might have a red themed page that also includes unordered lists as well as definition lists:
<link rel="stylesheet" type="text/css" href="global.css">
<link rel="stylesheet" type="text/css" href="red.css">
<link rel="stylesheet" type="text/css" href="lists.css">
If you have 300 pages on a site but use lists only on 5 pages, then why would you want to include your list styles in a global style sheet? The same thing goes with color variants: 300 pages total, 260 blue themed (blue.css), 20 red (red.css), 15 green (green.css) and 5 night themed (night.css) - Why burden your global style sheet with ALL the variants for all the elements when you can break them down into custom sets?
Multiple style sheets, when well planned are a great opportunity to reduce another kind of CSS code bloat: the "monster-super-sized something for every occasion, swiss army knife" style sheet! And to think, you only needed the corkscrew!!! ;)
<link rel="stylesheet" type="text/css" href="global.css">
<link rel="stylesheet" type="text/css" href="stanley-turrentine.css">
global:
body {
padding:0;
margin:0;
font:1em georgia, "Book Antiqua", palatino, serif;
}
p {
some styles
}
...etc.
stanley-turrentine.css:
body {
background:#8FBC8F url(stanley_turrentine.jpg) no-repeat;
}
In the stanley-turrentine.css the addition of a background image for the page is added; the body is referenced twice, once in the global (padding, font, margins) and a second time in the page/topic specific sheet.
Now lets say you had several pages about Stanley Turrentine: 2 general, one with several lists and a fourth with a form. For the two general pages:
<link rel="stylesheet" type="text/css" href="global.css">
<link rel="stylesheet" type="text/css" href="stanley-turrentine.css">
For the page with the lists:
<link rel="stylesheet" type="text/css" href="global.css">
<link rel="stylesheet" type="text/css" href="stanley-turrentine.css">
<link rel="stylesheet" type="text/css" href="lists.css">
and finally for the page with the form:
<link rel="stylesheet" type="text/css" href="global.css">
<link rel="stylesheet" type="text/css" href="stanley-turrentine.css">
<link rel="stylesheet" type="text/css" href="forms.css">
This does not maen you NEED to use multiple style sheets, but it does provide examples where special use style sheets may be the best way to proceed. It all depends on the scope and variance of the site.
Nick
[edited by: Nick_W at 6:33 am (utc) on July 25, 2002]
It is clear that on the element level, it is possible to have seperate style sheets that control individual aspects of each element. You can also define the color properties of a class in one stylesheet, and its geometrical properties in another etc.
But if I understand Ross correctly, then he would like to define classes with color names in one style sheet, and to reference those definitions in another one. This could be called a "top-down" approach to defining styles. Assuming we have declared some colors in one style sheet, then a hypothetical (but invalid) syntax for such a reference could look like this:
div.leftcolumn {width:##;margin-left:##;font-color:class(greenish_red);} A theoretical alternative I just had to try out of curiosity was to assign several classes to an element at the same time, and it doesn't work either:
<div class="greenish_red" class="leftcolumn"> Obviuosly, we have to think the other way, and define classes for specific elements in the page, and then to declare the specifications of those classes in how many stylesheets as necessary and practical. In other words, we need to think "bottom-up".
One possible workaround to the dilemma would be to use nested elements (probably divs in most cases), which allows to seperate different aspects of their style. An inner div would then control the color and font used, while the outer can be used for positioning.
<div class="leftcolumn">
<div class="greenish_red">
blah
</div>
</div>
For example, say you reference the following three style sheets:
red.css
blue.css
layout.css
You might then style a <div> using classes from all three:
<div class="txt-red list-blue left-column-one">This uses styles from three individual style sheets. >> txt-red from red.css, list-blue from blue.css and left-column-one from layout.css</div>
.one {font: bold 10pt arial,sans-serif; color: red}
#grey {color:#eee}
#nobold {font-weight: normal; text-indent: 20px}
<p class=one id=grey>
this paragraph text is not red but grey because the ID setting overrides the one from the class.
</p>
<p class=one id=nobold>
this paragraph text is not bold and has indent at the first line due to the same reason.
</p>
I have one master stylesheet that defines site colors, fonts, and what nots. That will drive the general look and feel of the site. I'd like to avoid specifying colors anywhere but there. It contains class definitions like shade1 (Red), shade2 (gray), etc.
Now I'm creating more specialized stylesheets specific to pages or areas in the site (it's pretty big). I am creating container divs with borders. Since I don't want to specify the border colors in the specialized stylesheets, I've added class definitions like shade1shade2 to the master stylesheet like so:
.shade1shade2 {
background: grey;
border: 3px solid red;
}
Now I can make a div like this:
<div class="NewsItem shade1shade2>blahblah</div>
This mixes more style with the content than I'd like, but it's workable for me. Is there a better way? I'd prefer for the definition of NewsItem to reference shade1 and shade2, but that doesn't seem to fly.
In your example:
<div class="NewsItem shade1shade2">blahblah</div>
The div is styled by two classes: NewsItem, the first in the cascade, and then shade1shade2, the second in the cascade. Cascade you say? Definately. This is an example of the inline cascade!
Lets say that the .NewsItem class came from the element.css style sheet and the .shade1shade2 class came from the colors.css style sheet. Both classes have been assigned to a single div and both will "deliver the goods" so to speak...
But what if there are background colors declared in both applied classes? Well, then the cascade will determine the which color prevails.
NewsItem {
font:1em verdana, arial, san-serif;
text-align:justify;
padding:3px
color:#fff;
background:#006;
}
.shade1shade2 {
background: grey;
border: 3px solid red;
}
The <div class="NewsItem shade1shade2"> will take all the styles properties from the first class, then over-ride the initial background color (#006;) and display a grey background with a 3px solid red border.
The Cascade is a powerful tool. It can be very tricky at times, depending on the element and browser, but once you get comfortable with the concept, you will find it much easier to create smaller, specialty style sheets.
Mixing .class and #id is perfectly acceptable as well - providing you understand that only a single instance of an #id can be used on each page. You may have 500 pages that all use the #banner id, since there is only a single instance on each of the 500 pages. It's all in how you choose to manage your styles.
At first this bothered me and seemed counterintuitive... I expected the <div> statement to control things as the html is interpreted. Then I decided it's for the best. Regardless of how the html is coded, I can have either the local styles supercede the sitewide ones, or the other way around, depending upon the structuring of the @import statements.
Now the question becomes one of phylosophy. Should the sitewide stylesheet be the overlord, winning all conflicts, or should the local styllesheets override it at will? Hmmmm.
When linking in style sheets, is it correct to say that using:
<link rel ... />
<link rel ... />
<link rel ... />
<link rel ... />
...will only allow you to call certain styles from each sheet explicitly, whereas, using:
@import "...";
@import "...";
@import "...";
...will automatically cascade with your imported style sheets without having to make a call to them? Or which method is better?
M
.box1, .box2 {
background-color: #ffffff;
border: 1px solid #999999;
padding:3px 10px 2px 10px;
margin-bottom:5px;
width:100%;
color: #000000;
text-align:right;
}
span.box2{
width:70%;
float:right;
padding-right:20px;
margin-top:10px;
}
to overwrite the other properties .... its self-explanatory ... =)
lesser code is better.
When linking in style sheets, is it correct to say that using:<link rel ... />
<link rel ... />
<link rel ... />
<link rel ... />...will only allow you to call certain styles from each sheet explicitly, whereas, using:
@import "...";
@import "...";
@import "...";...will automatically cascade with your imported style sheets without having to make a call to them? Or which method is better?
Either, or both! Mix n' match if you will. I use @import the majority of the time, but I have many pages where CSS is culled from files referenced by both methods.
Use the top-down approach for cascading issues: last parsed prevails, unless !important is assigned upstream in an earlier sheet.
Review the W3C explanation of the !important rule. Be aware that the end-user can set their own CSS preferences for their browser. This is extremely easy using Opera. If you have sized text within a fixed size element such as a div for example, your text might break out of the containing box if an end-user specifies a larger font than the fixed-dimension container can hold.
We all know that is best to let font-sizng up the end-users preference. We also know that this does not always happen and with some designs there are certain elements where fixed size is imperative.
By deploying the !important rule, you can override end-user settings - which is after all, the last segment of the cascade. However, the end-user does have the final say if they are savvy enough to use !important in their own CSS settings.
Once again because it is... !important
[w3.org...]
To apply your own style sheet and surf using your own CSS preferences Opera users can Alt + P / Preferences/Page Look/My Style Sheet - In the end, the user rules!
- papabaer
Style with DOM and page structure in mind! Order those styles into a logical fashion... You will be glad that you did.
style-a-holic and style-a-phile:
- papabaer ;)
Imagine a user, fed up of white text on black backgrounds, has the following in his style sheet:
body { background-color: #fff; background-image: none; color: #000; }
...and you have something like this:
body { background-color: #000; color: #fff !important; }
What will happen? The user will not be able to read your text, which will be white on white.
That's a very crass (and obvious) example, but the sort of thing you have to bear in mind.