CSS Programming
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Introduction
[edit] What is CSS?
CSS is one way of describing the "style" -- the colors, fonts, layout, and other "presentation" aspects of a document.
A single CSS file can describe a common "style" to be applied to many HTML, XHTML, and XML documents (which describe the content and structure of the elements of each document).
Typically a particular element in a XHTML file has a "cascade" of CSS style rules that can be applied to it. The highest priority style rule is applied to each element.
[edit] Why would I want to use CSS rather than something else?
CSS is a powerful tool that gives web designers flexibility and modularity in the presentation layer of a web site.
There are 4 ways that text can be styled
- no styling -- looks bland
- proprietary styling -- Flash, PDF, AbiWord, etc. -- requires the user to download and install an application in order to view it, which some users are unwilling or unable to do because they (or their security administrator) fear trojans.
- HTML presentational styling -- one standard way to style documents presented on the web
- CSS styling of HTML documents -- the newer standard way to style documents presented on the web (HTML and XHTML documents).
A web page presented to the end user looks the same -- for almost every visual effect that can be shown using CSS, there is a way to make a web page that looks the same using HTML presentational styling.
So why use CSS+HTML instead of HTML?
CSS allows you to have every format rule defined for later use (here "format" means how things appear). So if you are writing a large website and you want a consistent appearance for every title, sub-title, how examples of code appear, how paragraphs are aligned, (I could go on, CSS covers a wide range of presentation options) then CSS is the way to go.
Let's say you have a 1200 page website that took you months to complete. Your current boss gets a promotion and another person fills his place. Your new boss says to change the font, the size, the background, the appearance of tables, etc. everywhere on your 1200-page site to comply with some new corporate policy. If you engineered your site appropriately with CSS, you could do this by editing one linked CSS file that has all your appearance (format) rules in one place.
Or you could do it the hard way, and hammer the appearance changes on each and every of your 1200 HTML pages. Remember sleep? Your constitutional rights allow you to take the hard way (this is meant as humor, not an insult).
One other means to gain some understanding is to dissect other (good/professional) websites and figure out how their CSS interacts with their (X)HTML and XML documents.
CSS is commonly applied to HTML, XHTML and XML documents. CSS is essential in XHTML 1.1, which permits very little 'presentation' markup and instead must use CSS. (Note: it is a common myth that XHTML 1.0 Transitional requires CSS but in fact it allows 'presentation' markup.)
[edit] Basic CSS
With enough hands-on experience, CSS is very easy to learn. The first few sections discuss how (X)HTML and XML documents can make use of CSS. The following sections then describe CSS itself.
- Applying CSS to (X)HTML - Each method has its place however the linked method is the most important and the one this wikibook will reference.
- Linked CSS Method (using an external CSS file to link to multiple pages on a web site)
- Embedded CSS Method (linked CSS is preferred in most cases)
- Inline CSS Method (linked CSS is preferred in most cases)
- Applying CSS to XML
- CSS Construction
- CSS Presentation
- CSS Layout
[edit] Advanced CSS
CSS provides the web developer and the end user with remarkable flexibility and power, however for CSS to be used to its full potential the use of table-less layouts is essential.
- CSS and Divs
-in progress
[edit] Troubleshooting
- Standards Mode and Quirks Mode
- Browser Compatibility
- CSS order of Rules (Troubleshooting multiple instances)
[edit] Index
[edit] Hacks & Filters
CSS Hacks
All web browsers are not equal and different rendering of the same code, even if the code is validated, can destroy a web page's presentation. To work around this a large community of browser hacks has been developed. These hacks should be avoided at all cost because they may break your code in other browsers or slow down performance. However sometimes it is impossible to avoid the use of these hacks.
Be aware that constant browser updates may cause hacks to not work in the future.
- MediaQueryHack Making CSS visible for Opera 7 only
- ImportHack hides files from NetscapeFour and older browsers
- CaioHack hides rules from Netscape 4, even 'inline' CSS
- AntiNavFourSelectors simply hide single rulesets from Nav4
- BoxModelHack feeds alternative properties to IE 5.x; several new methods now possible
- StarHtmlHack feeds rules to Mac and Windows IE
- LoneStarHack feeds rules to Mac OS9/Classic and Windows IE5.x+
- HighPassFilter hides files from browsers other than IE6/Win, IE5/Mac, Opera 6, Netscape 6
- Owen Hack hides rules from Opera 6 (but not Opera 7!), Amaya 6;
- GhostInTheBox hack Makes position:fixed work in IE/Win 6.0
- IE5 Mac Hack Targets only IE 5 Mac
- IE 5 Mac Backslash Hack Hides styles from IE 5 Mac except for MSN 8
- Alternate Stylesheet Hack Hides files from Mozilla, Safari, Konqueror 3
- AlphaBetaPngSupport Allows cross-browser PNG suport with pure CSS, supports: IE5.5, IE6, Gecko, Opera and kde. All on PC, Linux and MAC. not in Mac opera6 Mac IE5.1
- ChildHack Hides from IE and other less capable browsers
- Max-width in IE Hack Uses expression() to fake max-width for IE/Win 5 and 6
- IE Underscore Hack Feed rules to IE/Win 5+ using underscores in property names
- PseudoCommentHack Different rule sets for IE4, IE5.0/5.5 and IE6.0
- Character Escape Hack filters all IE user-agents, due to IE's missing syntax support
Filters
- alpha
- BlendTrans
- blur
- chroma
- dropShadow
- FlipH
- FlipV
- glow
- Gray
- Invert
- light
- mask
- shadow
- wave
- Xray
- RevealTrans
[edit] Links
Other wiki about CSS
- The css-discuss Wiki
- WikiKnowledge: Cascading Style Sheets, a public-domain collaboratively developed book on CSS.
All of these references are free
- World Wide Web Consortium's Cascading Style Sheets Homepage includes CSS tips & tricks, and Learning CSS with non-English resources
- CSS 2.1 Specification
- Cascading Style Sheet References - L. Carlson.
- Web Design Update - A a plain text email digest newsletter. It typically goes out once a week and has a section on CSS. All web designers and developers are invited to join.
- Cascading Style Sheets Books
- CSS Tutorials - step by step lessons.
- Vertical-Align Misuse Vertical-Align Misuse. An attempt to demystify one of the most misused properties of CSS.
- Eric Meyer
- Eric Meyer CSS Expert Eric Meyer is a recognized expert in CSS and gives many of his resources in print away at this site.
- CSS/Edge by Eric Meyer Filled with great unique examples to learn from.
- Eric Meyer on CSS Eric Meyer's free online version of his CSS workbook
- More Eric Meyer on CSS This is his 2nd in the same series where he gives away his print resources online.
- Brainjar.com Using CSS Overview/basics explained in clarity
- Brainjar.com Michael Hall's great site for CSS (and other related web-programming).
- CSS "A Guide for the Unglued". He also goes over the pain of browser compatibility issues.
- CSS Design Article Great article on CSS (he calls it a "rant"). Very informative.
- CSS and "Tables". Glish.com on CSS Box ModelTables are not evil, but you can avert much code with CSS box forms if used right.
- Why bother with CSS | Owen Briggs
- A List Apart | Great Resource
- Online version of Webucator's CSS Manual
- W3 Schools | CSS Course All hands on, pure code. These people have other courses on HTML and other web-programming languages.
- CSS, HTML, XHTML Code Validator Courtesy of the World Wide Web Consortium
- Web Standards Software and Learning, A free CSS reference for cascading style sheets.
- CSS "Zen" Garden See how a single HTML file can be radically changed by dozens of different CSS files. You can even submit your own after you've learned the principles. New examples each week.
- CSS lessons
[edit] Contributors
- Monkeymatt: Put in all of the linked pages on wikibooks up to the selectors, may come back and do more later

