Cascading Style Sheets (CSS) is a stylesheet language used to describe the presentation of a document written in a markup language. Its most common application is to style web pages written in HTML and XHTML, but the language can be applied to any kind of XML document, including SVG and XUL. The CSS specifications are maintained by the World Wide Web Consortium (W3C).
Advantages of using CSS include:
Presentation information for an entire website or collection of pages can be held in one CSS file, allowing sweeping changes to be propagated with quick changes to this one file.
Different users can have different style sheets: for example a large text alternative for visually-impaired users, or a layout optimized for small displays for mobile phones.
The document code is reduced in size and complexity, since it does not need to contain any presentational markup.
CSS has a simple syntax, and uses a number of English keywords to specify the names of various style properties. A style sheet consists of a list of rules. Each rule or rule-set consists of one or more selectors and a declaration block. A declaration-block consists of a list of semicolon-separated declarations in curly braces. Each declaration itself consists of a property, a colon (:), a value, then a semi-colon (;).
In CSS, selectors are used to declare which elements a style applies to, a kind of match expression. Here are some examples of selector syntax:
All elements
that is, using the * selector
By element name
e.g. for all p or h2 elements
Descendants
e.g. for a elements that are descendants of li elements (e.g links inside lists) the selector is li a
class or id attributes
e.g. .class and/or #id for elements with class="class" or id="id" in the markup. Also, attribute selectors (see below) can be used, but with reduced specificity.
Adjacent elements
e.g. for all p elements preceded by h2 elements, the selector would be h2 + p
Direct child element
e.g. for all span elements inside p, but no span elements deeper within the hierarchy, the selector would be p > span
By attribute
e.g. for all the selector would be input[type="text"]
In addition to these, a set of pseudo-classes can be used to define further behavior. Probably the best-known of these is :hover, which applies a style only when the user 'points to' the visible element, usually by holding the mouse cursor over it. It is appended to a selector as in a:hover or #elementid:hover. Other pseudo-classes and pseudo-elements are, for example, :first-line, :visited or :before. A special pseudo-class is :lang(c), where the style would be applied on an element only if it is in language "c".
A pseudo-class selects entire elements, such as :link or :visited, whereas a pseudo-element makes a selection that may consist of partial elements, such as :first-line or :first-letter.
Selectors may be combined in other ways too, especially in CSS 2.1, to achieve greater specificity and flexibility.
In the following example, several selectors are grouped using comma separation. The rule sets the font for all HTML headings.
CSS is a standard, like HTML. All major browsers support Cascading Style Sheets. It is browser-independent. Styles Solve a Common Problem:
HTML tags were originally designed to define the content of a document. They
were supposed to say "This is a header", "This is a
paragraph", "This is a table", by using tags like <h1>,
<p>, <table>, and so on. The layout of the document was supposed to
be taken care of by the browser, without using any formatting tags.
As the two major browsers - Netscape and Internet Explorer -
continued to add new HTML tags and attributes (like the <font> tag and the
color attribute) to the original HTML specification, it became
more and more difficult to create Web sites where the content of HTML
documents was clearly separated from the document's presentation
layout.
To solve this problem, the World Wide Web Consortium (W3C) - the non profit,
standard setting consortium, responsible for standardizing HTML - created STYLES
in addition to HTML 4.0.
Style Sheets Can Save a Lot of Work
Styles sheets define HOW HTML elements are to be displayed, just like the font tag and the color attribute in HTML 3.2. Styles are normally saved in external .css files. External style sheets enable you to change the appearance and layout of all the pages in your Web, just by editing one single CSS document!
CSS is a breakthrough in Web design because it allows developers to control the style and layout of multiple Web pages all at once. As a Web developer you can define a style for each HTML element and apply it to as many Web pages as you want. To make a global change, simply change the style, and all elements in the Web are updated automatically.
Flexibility of CSS:
You can apply more than one class per given element, the syntax is:
You can use miltiple order of selectors. It is really "unlimited". Some examples:
It is a standard. It is browser-independent.
You can even change the whole look of your huge site, with just editing the CSS in minutes!