Cascading Style Sheets (CSS) | |
Extension: | .css |
Icon Size: | 120px |
Iconcaption: | The official logo of the latest version, CSS 3 |
Screenshot Size: | 160px |
Mime: | text/css |
Uniform Type: | public.css |
Developer: | World Wide Web Consortium (W3C) |
Genre: | Style sheet language |
Latest Release Version: | CSS 3 is being developed as multiple separate modules. Regular snapshots summarize their status. |
Container For: | Style rules for HTML elements (tags) |
Contained By: | HTML Documents |
Open: | Yes |
Cascading Style Sheets (CSS) is a style sheet language used for specifying the presentation and styling of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML).[1] CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript.[2]
CSS is designed to enable the separation of content and presentation, including layout, colors, and fonts.[3] This separation can improve content accessibility, since the content can be written without concern for its presentation; 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; and enable 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 declaration applies if more than one declaration of a property match 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. CSS is also used in the GTK widget toolkit.
CSS has a simple syntax and uses a number of English keywords to specify the names of various style properties.
See main article: Style sheet (web development). A style sheet consists of a list of rules. Each rule or rule-set consists of one or more selectors, and a declaration block.
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 element. 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 .[6] Note the distinction between the double-colon notation used for pseudo-elements and the single-colon notation used for pseudo-classes.
Multiple simple selectors may be joined using combinators to specify elements by location, element type, id, class, or any combination thereof.[7] The order of the selectors is important. For example,
The following table provides a summary of selector syntax indicating usage and the version of CSS that introduced it.[8]
Pattern | Matches | First defined in CSS level |
---|---|---|
an element of type E | 1 | |
an E element that is the source anchor of a hyperlink whose target is either not yet visited (:link) or already visited (:visited) | 1 | |
an E element during certain user actions | 1 | |
the first formatted line of an E element | 1 | |
the first formatted letter of an E element | 1 | |
all elements with class="c" | 1 | |
the element with id="myid" | 1 | |
an E element whose class is "warning" (the document language specifies how class is determined) | 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 element | 1 | |
any element | 2 | |
an E element with a "foo" attribute | 2 | |
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 | |