Cascading Style Sheets


Cascading Style Sheets is a style sheet language used for describing the presentation of a document written in a markup language like HTML. CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript.
CSS is designed to enable the separation of presentation and content, including layout, colors, and fonts. This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple web pages to share formatting by specifying the relevant CSS in a separate.css file which reduces complexity and repetition in the structural content as well as enabling the.css file to be cached to improve the page load speed between the pages that share the file and its formatting.
Separation of formatting and content also makes it feasible to present the same markup page in different styles for different rendering methods, such as on-screen, in print, by voice, and on Braille-based tactile devices. CSS also has rules for alternate formatting if the content is accessed on a mobile device.
The name cascading comes from the specified priority scheme to determine which style rule applies if more than one rule matches a particular element. This cascading priority scheme is predictable.
The CSS specifications are maintained by the World Wide Web Consortium. Internet media type text/css is registered for use with CSS by RFC 2318. The W3C operates a free CSS validation service for CSS documents.
In addition to HTML, other markup languages support the use of CSS including XHTML, plain XML, SVG, and XUL.

Syntax

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.

Selector

In CSS, selectors declare which part of the markup a style applies to by matching tags and attributes in the markup itself.
Selectors may apply to the following:
Classes and IDs are case-sensitive, start with letters, and can include alphanumeric characters, hyphens and underscores. A class may apply to any number of instances of any elements. An ID may only be applied to a single element.
Pseudo-classes are used in CSS selectors to permit formatting based on information that is not contained in the document tree. One example of a widely used pseudo-class is, which identifies content 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 or. A pseudo-class classifies document elements, such as or, whereas a pseudo-element makes a selection that may consist of partial elements, such as or.
Selectors may be combined in many ways to achieve great specificity and flexibility. Multiple selectors may be joined in a spaced list to specify elements by location, element type, id, class, or any combination thereof. The order of the selectors is important. For example,
div.myClass
applies to all elements of class myClass that are inside div elements, whereas .myClass div applies to all div elements that are in elements of class myClass.
The following table provides a summary of selector syntax indicating usage and the version of CSS that introduced it.
PatternMatchesFirst defined
in CSS level
an element of type E1
an E element is the source anchor of a hyperlink of which the target is not yet visited or already visited 1
an E element during certain user actions1
the first formatted line of an E element1
the first formatted letter of an E element1
all elements with class="c"1
the element with id="myid"1
an E element whose class is "warning" 1
an E element with ID equal to "myid"1
the element with class="c" and ID equal to "myid"1
an F element descendant of an E element1
any element2
an E element with a "foo" attribute2
an E element whose "foo" attribute value is exactly equal to "bar"2
an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar"2
an E element whose "foo" attribute has a hyphen-separated list of values beginning with "en"2
an E element, first child of its parent2
an element of type E in language "fr" 2
generated content before an E element's content2
generated content after an E element's content2
an F element child of an E element2
an F element immediately preceded by an E element2
an E element whose "foo" attribute value begins exactly with the string "bar"3
an E element whose "foo" attribute value ends exactly with the string "bar"3
an E element whose "foo" attribute value contains the substring "bar"3
an E element, root of the document3
an E element, the n-th child of its parent3
an E element, the n-th child of its parent, counting from the last one3
an E element, the n-th sibling of its type3
an E element, the n-th sibling of its type, counting from the last one3
an E element, last child of its parent3
an E element, first sibling of its type3
an E element, last sibling of its type3
an E element, only child of its parent3
an E element, only sibling of its type3
an E element that has no children 3
an E element being the target of the referring URI3
a user interface element E that is enabled3
a user interface element E that is disabled3
a user interface element E that is checked 3
an E element that does not match simple selector s3
an F element preceded by an E element3

Declaration block

A declaration block consists of a list of declarations in braces. Each declaration itself consists of a property, a colon, and a value. If there are multiple declarations in a block, a semi-colon must be inserted to separate each declaration.
Properties are specified in the CSS standard. Each property has a set of possible values. Some properties can affect any type of element, and others apply only to particular groups of elements.
Values may be keywords, such as "center" or "inherit", or numerical values, such as , or . Color values can be specified with keywords, hexadecimal values, RGB values on a 0 to 255 scale, RGBA values that specify both color and alpha transparency, or HSL or HSLA values.

Length units

Non-zero numeric values representing linear measures must include a length unit, which is either an alphabetic code or abbreviation, as in 200px or 50vw; or a percentage sign, as in 80%. Some units – cm ; in ; mm ; pc ; and pt – are absolute, which means that the rendered dimension does not depend upon the structure of the page; others – em ; ex and px – are relative, which means that factors such as the font size of a parent element can affect the rendered measurement. These eight units were a feature of CSS 1 and retained in all subsequent revisions. The proposed CSS Values and Units Module Level 3 will, if adopted as a W3C Recommendation, provide seven further length units: ch; Q; rem; vh; vmax; vmin; and vw.

Use

Before CSS, nearly all presentational attributes of HTML documents were contained within the HTML markup. All font colors, background styles, element alignments, borders and sizes had to be explicitly described, often repeatedly, within the HTML. CSS lets authors move much of that information to another file, the style sheet, resulting in considerably simpler HTML.
For example, headings, sub-headings, sub-sub-headings, etc., are defined structurally using HTML. In print and on the screen, choice of font, size, color and emphasis for these elements is presentational.
Before CSS, document authors who wanted to assign such typographic characteristics to, say, all h2 headings had to repeat HTML presentational markup for each occurrence of that heading type. This made documents more complex, larger, and more error-prone and difficult to maintain. CSS allows the separation of presentation from structure. CSS can define color, font, text alignment, size, borders, spacing, layout and many other typographic characteristics, and can do so independently for on-screen and printed views. CSS also defines non-visual styles, such as reading speed and emphasis for aural text readers. The W3C has now deprecated the use of all presentational HTML markup.
For example, under pre-CSS HTML, a heading element defined with red text would be written as:

Chapter 1.



Using CSS, the same element can be coded using style properties instead of HTML presentational attributes:

Chapter 1.



The advantages of this may not be immediately clear but the power of CSS becomes more apparent when the style properties are placed in an internal style element or, even better, an external CSS file. For example, suppose the document contains the style element:



All h1 elements in the document will then automatically become red without requiring any explicit code. If the author later wanted to make h1 elements blue instead, this could be done by changing the style element to:



rather than by laboriously going through the document and changing the color for each individual h1 element.
The styles can also be placed in an external CSS file, as described below, and loaded using syntax similar to:



This further decouples the styling from the HTML document, and makes it possible to restyle multiple documents by simply editing a shared external CSS file.

Specificity

Specificity refers to the relative weights of various rules. It determines which styles apply to an element when more than one rule could apply. Based on specification, a simple selector has a specificity of 1, class selectors have a specificity of 1,0, and ID selectors a specificity of 1,0,0. Because the specificity values do not carry over as in the decimal system, commas are used to separate the "digits".
Thus the following rules selectors result in the indicated specificity:
SelectorsSpecificity
H1 0, 0, 0, 1
P EM 0, 0, 0, 2
.grape 0, 0, 1, 0
P.bright 0, 0, 1, 1
P.bright EM.dark 0, 0, 2, 2
#id218 0, 1, 0, 0
style=" "1, 0, 0, 0

Examples

Consider this HTML fragment:








To demonstrate specificity





In the above example, the declaration in the style attribute overrides the one in the