CSS Wisdom :: Advice on Cascading Style Sheets
How I may help
LinkedIn Profile Email me!

Permalink CSS (Cascading Style Sheets)

Here are notes on how to figure out CSS to format web pages. In one page are practical advice, links, and examples.

The layers presented by internet browsers (user agents):

  • HTML — the structure layer
  • CSS — the presentation layer on this page
  • JavaScript — the behavior layer

 

Topics this page:

Site Map Go to a list of all pages on this sites
About this site About/Help on this site
First Topic


CSSPlay lists experiments with CSS

Transcending CSS: The Fine Art of Web Design (Voices That Matter) (New Riders, Nov 15, 2006) by Andy Clarke and Molly E. Holzschlag has a lot of "wasted" space, extraneous photos, and chatty text. But all that makes sense because in this book CSS is presented in the context of excellent web design. Like a conversation with an eccentric artist, there are enough gems of wisdom among the cryptic text and images that makes the exchange interesting, such as its comments on CSS3 support by Firebox 2 and IE7.

Go to top of page Advice on Getting Started With CSS

  1. Do some reading before diving into CSS. You need to be prepared for quirks, especially with IE browsers.
  2. Examine the templates that already incorporates the various hacks to work around bugs in various versions of internet browser user agents. It is not cost effective to go through extra trouble to accomodate those who ignore the security threats that have forced almost all computer users to upgrade to versions of operating systems that have more modern (and less buggy) internet browsers. But to accomodate the needs of mobile internet phones, we need to make web pages flexible. For example, the internet browser within Microsoft Pocket PCs identifies itself as "IE4".
  3. Take a picture of websites that you like. My list of sites I covet. Save the page as "Complete" so you can analyze it later, even if the site is changed.
  4. Look through websites in my gallery to get an idea of what is possible visually. Make notes of page design elements you want and don't want in your website.
  5. Arrange for tutoring before you need it. I found a tutor by putting ads on Craigslist and various boards. I also looked through galleries of CSS work and contacting the person who created the format I liked the best. We installed VNC and Skye VOIP so we can see each other's desktops and talk at the same time.
  6. Mock up the "look" you want by moving elements around. If you're comfortable with a graphics layout program such as Photoshop, Microsoft Powerpoint, or Visio, use it so you can save various versions. If you're a kinesthetic learner, have individual elements you want on paper, cut out them out and move them around the paper as if you're arranging furniture in a room. Take photos of various arrangements. The advantage of using Photoshop is that it has transparaent layers so that in the mock-up, you can note commonly used position locations such as column locations (4 column designs need them).
  7. Work first on positioning, then on fonts and colors. Work outside, in; top, down. This way, you can catch unintended effects of cascading as soon as it can be observed.
  8. Resist the temptation to go back to using tables to layout pages. Separate content from presentation. Know The Meaning of Semantics Remember that tables for layout is stupid.
  9. When debugging CSS, make only one change at a time. If you approach your time learning CSS as an experiment -- a learning process -- you'll be less frustrated with the journey.
  10. Backup often. Every time you get it working, It helps to use a text editor that saves changes incrementally so that you have an unlimited number of undos. But even then occassionally zip up file sets to a different (preferrably sequential) archive file name.
  11. Work to a list of what you want to fix or what you need to learn. Examples: A). Fix floated images that "stick out" of the bottom of previous div elements B). How do I align text vertically within its box?
  12. Keep a diary of what you tried and the identification of the backup file associated with each variation.
  13. Test each change to see what happens.
    Expand and contract the window to see if the text flows the way you want.
    Enlarge and reduce font sizes.
    If your site supports it, switch css files to set different color schemes.
  14. Validate your html and css files to spot errors using these debugging tools
  15. Look at the page in various versions of several other browsers. Code first within a standards-compliant brower like Firefox, then add work-arounds for Internet Explorer browsers.
  16. Backup often. Better yet, get a text editor that saves every variation automatically.
  17. Did I mention the importance of backing up data often?
3 column layout examples:

Go to top of page The Looks I Covet

Go to top of page Templates and Working Examples

Go to top of page Template Galleries

Websites Laid Out Using Stictly CSS

Go to top of page What I have Learned from Studying Other Sites

Go to top of page Drupal Blocks

Go to top of page Cascading

Go to top of page Default Style Reset

Go to top of page CSS Concatenation and Rollovers

Go to top of page Tabs For Navigation

Go to top of page Rounded Corners

Go to top of page Styling Forms

Go to top of page Movin' on up (to XHTML Strict)

"Navigating the Cascade" Webreference article

Go to top of page Starting from inline styles

    CSS stylings defined as attributes in the <div> tag are applied to values between <div> and </div> tags. For example:

    <div class="my_hr"><hr /></div>

    Styling declarations for this are defined in the "my_hr" style rule such as:

    .my_hr { color: #f00; background-color: #f00; height: 5px; }

    We use spaces to separate declaration code from the braces to make them easier to locate using ctrl-left and ctrl-right keys provided by text editors.

    Such rules are defined either in-line at the top of the html file where they are referenced, or in an external cascading style sheet file with a suffix of ".css".

    Embedded styles such as this are embedded along with text content:

    <div style=" color: #f00; background-color: #f00; height: 5px; ">

    Such declarations are bounded by quotes. This is discouraged due to the general approach of separating content from formatting code.

    There is a consequence to where a style is specified. Assumptions about style cascade down from each level in the document hierarchy:

    1. Declarations hidden as default browser styles cascade to
    2. User style sheet declarations cascade to
    3. Declarations authored in external style sheet files cascade to
    4. Declarations embedded (internal) in authored between <style tag (within the <HEAD tag) cascade to
    5. inline styles authored within <div style=" ... " declarations

    If the same property for the same tag is defined in more than one place, style display follows this hierarchy.

    Go to top of page Declarations

    CSS code formats content using declarations. Each declaration contain "property: value" pairs such as color: #000;

    Commas separate styles which share the same rule.

    Spaces separate the definition of greater selectivity, such as <div p { ... }

    Multiple declarations can be specified within a rule.

    Go to top of page External Style Files

    CSS declarations can be placed within a separate file usually have an file extension of .css. External css files have the advantage of being cached on client machines. On subsequent retrievals, caching means faster access because the HTTP protocol has a "304" return code which means that the file does not need to be downloaded again. Thus, Visitors see faster page loads.

    The css file does not need to be retrieved by any other page that references that css file.

    A css file is specified with a LINK tag within the <HEAD> section:

    <link rel="stylesheet" type="text/css" href="./css/printstyle.css" media="print"> 
    <link rel="stylesheet" type="text/css" href="./css/pdastyle.css" media="handheld"> 
    
    These are needed because IE5 incorrectly treats imported print/pda stylesheet as media="all".

    Go to top of page In-line Styles

    Even though it's nice to promote code reuse, CSS code applicable only to a single HTML file is placed within that file as in-line coding placed between HTML comment block <!-- and --> to keep them from being displayed as content text by older browsers which don't know about CSS.

    <style type="text/css" media="all"><!--
    //--></style>
    

    This hack is possible because Navigator 4.x ignores @import. An example is margin rules for standards compliant browsers because Netscape 4 misinterprets margins. We also use @import only for workarounds to IE bugs which are correctly handled by NN 4.x.

Go to top of page Selector Names

    # (pound sign/octothorpe/hash mark) precede the definition of ID selectors such as #authorid.

    <p id="authid">
    

    Even though browsers haven't been complaining, each ID selector is supposed to be used only once on a page. That's why in some situations they carry more "weight" than class selectors.

    . (period/dot) precede the definition of class selector which can be used many times:

    <p class="authid">
    

    Selector names starting with a number doesn't work in Mozilla/Firefox.

    IE doesn't recognize selector names beginning with an underline, such as "_forieonly".

 

  • selectoracle translates CSS2 and CSS3 coding into English or Spanish sentences. Very cool.
  • One commenter at the DaniWeb IT Discussion Community on CSS said "My biggest problems with CSS arise when I get caught up in how I think it should work, instead of trying to find out how it actually works."

  Go to Top of this page.
Previous topic this page
Next topic this page
Tutorials on mixing CSS and tables Here is a true ecumenical service!

The CSS Table Gallery provides examples of mixed use CSS with tables, submitted by designers (78 at last count),

HTML Code Tutorial does a good job explaining.

MaxDesigns provides simple and clear examples for cross-browser complexity.

MaxDesign's Lisamatic links to many variations on lists.

Go to top of page Tables

Go to top of page My CSS

    Two versions of CSS are used in this site.

    The first approach has a single "global.css" file.

    Pages referencing the "all.css" file which imports several css files (with the css at the bottom taking precedent): mambo.css, custom.css, white.css, black.css.

    Use of white.css and black.css alternate depending on user selection. The same will be true of print.css and mobile.css in the future.

    All content is within div id="content_area" (id="container" in csszen).

    The background image for the whole page:

    html { background: transparent url(htmlbg.jpg) repeat-x; }
    

Go to top of page Clearing Floating Divs

    To get one float div to wrap under another float div, PIE describes in detail (including an original attribution to Tony Aslett) and Eric Meyer suggest using this declaration to hack/fix

    This is fixed by positioning mark-up which is anathema to CSS purists: an extra div after a float:

    <div class="clear-block"></div>

    This approach is adopted in file default.css within Drupal's system module, which contains this code:

    .clear-block:after {
      content: ".";
      display: block;
      height: 0;
      clear: both;
      visibility: hidden;
    }
    
    .clear-block {
      display: inline-block;
    }
    
    /* Holly Hack Hides/Overides CSS from IE-mac \*/
    * html .clear-block {
      height: 1%;
    }
    .clear-block {
      display: block;
    }
    /* End hide from IE-mac */
    

 

  Go to Top of this page.
Previous topic this page
Next topic this page
"Navigating the Cascade" Webreference article

Go to top of page Cascading Order and the !Important Directive

  1. Styles marked with !important override non-important styles.
  2. Author styles override user styles, which in turn override user agent styles.
    Important user styles override important author styles, which in turn override important user agent styles.
  3. Rules whose selectors have higher specificity override those with lower specificity (see below)
  4. Lastly, rules that have been specified last override previously specified rules.
    Rules in imported style sheets are considered to have been specified before rules in the style sheet that imported them. This is why @import rules are always placed at the top of a style sheet; it makes it easy to remember that the rules in the style sheets they import are not as "strong" as the rules in the style sheet after the at-rules.

Go to top of page Bug vs. Bug

    IE5x PC mis-implements the "box model" prescribed by the W3C standard that other browsers follow.

    • IE5x PC puts padding and box borders WITHIN its stated width rather being added to the width as W3C specifies.

    • IE5x PC does not support the CSS2 child selector.

      So Tantek Celik found this way to prematurely close a style rule in IE5x PC by taking advantage of a bug in IE5x CSS parsing.

      Earlier versions of the W3C CSS validator incorrectly flags 'voice-family' property as errors in a 'screen' style sheet.

 

  Go to Top of this page.
Previous topic this page
Next topic this page

Go to top of page The Box Model

Go to top of page CSS Tag Reference

Go to top of page Cross-Browser Issues

 

cross-platform, cross-browser font size issues and accessibility by Mark Pilgrim

  Go to Top of this page.
Previous topic this page
Next topic this page

“hasLayout” by Sttzansatz gives a thorough explanation.

Thoughts on IE hack management

Controlling Presentation with Measurement and Location Properties (removed from MSDN but still available in the Internet Archive).

MSHTML Editing Platform in Internet Explorer 5.5 (removed from MSDN but still available in the Internet Archive).

CSS Mastery: Advanced Web Standards Solutions (OReilly, 2004) by Andy Budd, Simon Collison, Cameron Moll

Bulletproof Web Design : Improving flexibility and protecting against worst-case scenarios with XHTML and CSS (friends of ED, February 13, 2006) by Dan Cederholm of simplebits

Also by Dan Cederholm:
Web Standards Solutions: The Markup and Style Handbook (Friends of Ed, 2004, 318 pages)

 

Stylin' with CSS : A Designer's Guide (New Riders, April 26, 2005) and companion website A website external to this site by Charles Wyke-Smith His blog

 

Accessible XHTML and CSS Web Sites: Problem - Design - Solution (Wrox Press, 2005 (480 pages) by Jon Duckett

webpage article 5 Tips for Organizing Your css

Go to top of page Compatibility Among Rendering Engines

Go to top of page Rules for Different Media

    NN4.x only pays attention to style sheets that have a media value of "screen".

    @media screen {
    	.popup 	{ visibility: visible; }
    }
    @media handheld, print {
    	.popup 	{ visibility: hidden; }
    }

    Although the following media types are defined:

    Media Type Used for output to ...
    all all media type devices (the default)
    aural speech sound synthesizers
    braille braille tactile feedback devices
    embossed paged braille printers
    handheld mobile devices
    print printers (static)
    projection projected presentations, like slides (static)
    screen computer screens
    tty media using a fixed-pitch character grid, like teletypes and terminals
    tv television-type devices such as WebT

    List of CSS recognition by User Agent (browser) and by handheld device using HTMLDog's CSS test page and Lachlan Hunt's CSS Media Type Tests

    Web Hypertext Application Technology

    For print, it's better to set body { font-size: 12pt; }

    http://www.alistapart.com/articles/alaprintstyles always un-float large elements, especially long elements containing things like article text. As a general rule, when a float runs to multiple pages, you’re just asking for trouble

    body { float: none !important;
    

    http://www.alistapart.com/articles/goingtoprint/ If a floated element runs past the bottom of a printed page, the rest of the float will effectively disappear, as it won’t be printed on the next page.

 

  Go to Top of this page.
Previous topic this page
Next topic this page

Go to top of page Readings

Set screen
Go to Top of this page.
Previous topic this page
Link to Performance Engineer RSS 2.0 XML feed Atom 1.0 XML feed feeds
for Performance and Capacity Engineers...
    rob

How I may help

Send a message with your email client program


Your rating of this page:
Low High




Your first name:

Your family name:

Your location (city, country):

Your Email address: 

  Top of Page Go to top of page

Thank you!

Human verify:
 
Please retype:
 

Visitor comments are owned by the poster.
All trademarks and copyrights on this page are owned by their respective owners.
The rest ©copyright 1996-2006 Wilson Mar. All rights reserved.
| | | |