Cascading Style Sheets (CSS) : Filename Extension Internet Media Type Uniform Type Identifier (UTI)
Cascading Style Sheets (CSS) : Filename Extension Internet Media Type Uniform Type Identifier (UTI)
Concepts
animations
box model
image replacement
flexbox
grid
Philosophies
Tableless
Responsive
"Holy grail"
Tools
Sass
Less
Stylus
CSSTidy
Comparisons
CSS support
Stylesheet languages
v
t
e
HTML
Dynamic HTML
HTML5
o audio
o canvas
o video
XHTML
o Basic
o Mobile Profile
o C-HTML
HTML element
o span and div
HTML attribute
HTML frame
HTML editor
Character encodings
o Unicode
Language code
Document Object Model
Browser Object Model
Style sheets
o CSS
Font family
Web colors
JavaScript
o WebGL
o WebCL
W3C
o Validator
WHATWG
Quirks mode
Web storage
Rendering engine
Comparisons
Document markup languages
HTML support
XHTML
o 1.1
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation
of a document written in a markup language such as HTML.[1] CSS is a cornerstone
technology of the World Wide Web, alongside HTML and JavaScript.[2]
CSS is designed to enable the separation of presentation and content, including layout, colors,
and fonts.[3] 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 (via
speech-based browser or screen reader), and on Braille-based tactile devices. CSS also has
rules for alternate formatting if the content is accessed on a mobile device.[4]
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 (W3C). Internet
media type (MIME type) text/css is registered for use with CSS by RFC 2318 (March
1998). The W3C operates a free CSS validation service for CSS documents.[5]
In addition to HTML, other markup languages support the use of CSS including XHTML,
plain XML, SVG, and XUL.
Contents
1 Syntax
o 1.1 Selector
o 1.2 Declaration block
1.2.1 Length units
o 1.3 Use
o 1.4 Sources
o 1.5 Specificity
1.5.1 Examples
o 1.6 Inheritance
1.6.1 Example
o 1.7 Whitespace
o 1.8 Positioning
1.8.1 Position property
1.8.2 Float and clear
2 History
o 2.1 Difficulty with adoption
o 2.2 Vendor prefixes
o 2.3 Variations
2.3.1 CSS 1
2.3.2 CSS 2
2.3.3 CSS 2.1
2.3.4 CSS 3
2.3.5 CSS 4
3 Browser support
4 Limitations
o 4.1 Former issues
5 Advantages
6 Standardization
o 6.1 Frameworks
o 6.2 Design methodologies
7 References
8 Further reading
9 See also
10 External links
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.
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 :hover,
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 a:hover or
#elementid:hover. A pseudo-class classifies document 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.[6]
Selectors may be combined in many ways to achieve great specificity and flexibility.[7]
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 {color: red;} applies to all elements of class myClass that are
inside div elements, whereas .myClass div {color: red;} applies to all div elements that
are inside elements of class myClass. This is not to be confused with concatenated identifiers
such as div.myClass {color: red;} which applies to div elements of class myClass.
The following table provides a summary of selector syntax indicating usage and the version
of CSS that introduced it.[8]
First
defined
Pattern Matches
in CSS
level
E an element of type E 1
an E element is the source anchor of a hyperlink of which
E:link the target is not yet visited (:link) or already visited 1
(:visited)
E:active an E element during certain user actions 1
E::first-line the first formatted line of an E element 1
E::first-letter the first formatted letter of an E element 1
.c all elements with class="c" 1
#myid the element with id="myid" 1
an E element whose class is "warning" (the document
E.warning 1
language specifies how class is determined)
E#myid an E element with ID equal to "myid" 1
.c#myid the element with class="c" and ID equal to "myid" 1
E F an F element descendant of an E element 1
* any element 2
E[foo] an E element with a "foo" attribute 2
an E element whose "foo" attribute value is exactly equal to
E[foo="bar"] 2
"bar"
E[foo~="bar"] an E element whose "foo" attribute value is a list of 2
whitespace-separated values, one of which is exactly equal
to "bar"
an E element whose "foo" attribute has a hyphen-separated
E[foo|="en"] 2
list of values beginning (from the left) with "en"
E:first-child an E element, first child of its parent 2
an element of type E in language "fr" (the document
E:lang(fr) 2
language specifies how language is determined)
E::before generated content before an E element's content 2
E::after generated content after an E element's content 2
E > F an F element child of an E element 2
E + F an F element immediately preceded by an E element 2
an E element whose "foo" attribute value begins exactly
E[foo^="bar"] 3
with the string "bar"
an E element whose "foo" attribute value ends exactly with
E[foo$="bar"] 3
the string "bar"
an E element whose "foo" attribute value contains the
E[foo*="bar"] 3
substring "bar"
E:root an E element, root of the document 3
E:nth-child(n) an E element, the n-th child of its parent 3
E:nth-last- an E element, the n-th child of its parent, counting from the
child(n) 3
last one
E:nth-of-type(n) an E element, the n-th sibling of its type 3
E:nth-last-of- an E element, the n-th sibling of its type, counting from the
type(n) 3
last one
E:last-child an E element, last child of its parent 3
E:first-of-type an E element, first sibling of its type 3
E:last-of-type an E element, last sibling of its type 3
E:only-child an E element, only child of its parent 3
E:only-of-type an E element, only sibling of its type 3
E:empty an E element that has no children (including text nodes) 3
E:target an E element being the target of the referring URI 3
E:enabled a user interface element E that is enabled 3
E:disabled a user interface element E that is disabled 3
a user interface element E that is checked (for instance a
E:checked 3
radio-button or checkbox)
E:not(s) an E element that does not match simple selector s 3
E ~ F an F element preceded by an E element 3
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. An optional semi-colon after the last
(or single) declaration may be used.[9]
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.[10][11]
Values may be keywords, such as "center" or "inherit", or numerical values, such as 200px
(200 pixels), 50vw (50 percent of the viewport width) or 80% (80 percent of the parent
element's width). Color values can be specified with keywords (e.g. "red"), hexadecimal
values (e.g. #FF0000, also abbreviated as #F00), RGB values on a 0 to 255 scale (e.g.
rgb(255, 0, 0)), RGBA values that specify both color and alpha transparency (e.g.
rgba(255, 0, 0, 0.8)), or HSL or HSLA values (e.g. hsl(000, 100%, 50%), hsla(000,
100%, 50%, 80%)).[12]
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 (centimetre); in (inch); mm (millimetre); pc (pica); and pt (point) – are
absolute, which means that the rendered dimension does not depend upon the structure of the
page; others – em (em); ex (ex) and px (pixel) – 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[13] 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.[14]
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 (h1 elements), sub-headings (h2), sub-sub-headings (h3), 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.[15]
For example, under pre-CSS HTML, a heading element defined with red text would be
written as:
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:
<style>
h1 {
color: red;
}
</style>
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:
<style>
h1 {
color: blue;
}
</style>
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.
Sources
CSS information can be provided from various sources. These sources can be the web
browser, the user and the author. The information from the author can be further classified
into inline, media type, importance, selector specificity, rule order, inheritance and property
definition. CSS style information can be in a separate document or it can be embedded into
an HTML document. Multiple style sheets can be imported. Different styles can be applied
depending on the output device being used; for example, the screen version can be quite
different from the printed version, so that authors can tailor the presentation appropriately for
each medium.
The style sheet with the highest priority controls the content display. Declarations not set in
the highest priority source are passed on to a source of lower priority, such as the user agent
style. The process is called cascading.
One of the goals of CSS is to allow users greater control over presentation. Someone who
finds red italic headings difficult to read may apply a different style sheet. Depending on the
browser and the web site, a user may choose from various style sheets provided by the
designers, or may remove all added styles and view the site using the browser's default
styling, or may override just the red italic heading style without altering other attributes.
Specificity
Specificity refers to the relative weights of various rules.[16] It determines which styles apply
to an element when more than one rule could apply. Based on specification, a simple selector
(e.g. H1) 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"[17] (a CSS rule having 11 elements and 11 classes
would have a specificity of 11,11, not 121).
Selectors Specificity
h1 {color: white;} 0, 0, 0, 1
p em {color: green;} 0, 0, 0, 2
.grape {color: red;} 0, 0, 1, 0
p.bright {color: blue;} 0, 0, 1, 1
p.bright em.dark {color: yellow;} 0, 0, 2, 2
#id218 {color: brown;} 0, 1, 0, 0
style=" " 1, 0, 0, 0
Examples
Consider this HTML fragment:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
#xyz { color: blue; }
</style>
</head>
<body>
<p id="xyz" style="color: green;">To demonstrate specificity</p>
</body>
</html>
In the above example, the declaration in the style attribute overrides the one in the <style>
element because it has a higher specificity, and thus, the paragraph appears green.
Inheritance
Inheritance can be used to avoid declaring certain properties over and over again in a style
sheet, allowing for shorter CSS.