A | B | C | D | E | F | G | H | CH | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Filename extension |
.css |
---|---|
Internet media type |
text/css |
Uniform Type Identifier (UTI) | public.css |
Developed by | World Wide Web Consortium (W3C) |
Initial release | 17 December 1996 |
Latest release | CSS 2.1 : Level 2 Revision 1 12 April 2016 |
Type of format | Style sheet language |
Container for | Style rules for HTML elements (tags) |
Contained by | HTML Documents |
Open format? | Yes |
Website | w3 |
Cascading Style Sheets |
---|
Concepts |
Philosophies |
Tools |
Comparisons |
HTML |
---|
Comparisons |
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;[further explanation needed] 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.
Syntax
CSS has a simple syntax and uses a number of English keywords to specify the names of various style properties.
Style sheet
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.
Selector types
Selectors may apply to the following:
- all elements of a specific type, e.g. the second-level headers h2
- elements specified by attribute, in particular:
- id: an identifier unique within the document, denoted in the selector language by a hash prefix e.g.
#id
- class: an identifier that can annotate multiple elements in a document, denoted by a dot prefix e.g.
.classname
(the phrase "CSS class", although sometimes used, is a misnomer, as element classes—specified with the HTML class attribute—is a markup feature that is distinct from browsers' CSS subsystem and the related W3C/WHATWG standards work on document styles; see RDF and microformats for the origins of the "class" system of the Web content model)
- id: an identifier unique within the document, denoted in the selector language by a hash prefix e.g.
- elements depending on how they are placed relative to others in the document tree.
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
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] Note the distinction between the double-colon notation used for pseudo-elements and the single-colon notation used for pseudo-classes.
Combinators
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, 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.
Summary of selector syntax
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 |
---|---|---|
E |
an element of type E | 1 |
E:link |
an E element that is the source anchor of a hyperlink whose target is either not yet visited (:link) or already visited (:visited) | 1 |
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 |
E.warning |
an E element whose class is "warning" (the document language specifies how class is determined) | 1 |
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 |
an E element with a "foo" attribute | 2 |
E |
an E element whose "foo" attribute value is exactly equal to "bar" | 2 |
E |
an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar" | 2 |
E |
an E element whose "foo" attribute has a hyphen-separated list of values beginning (from the left) with "en" | 2 |
E:first-child |
an E element, first child of its parent | 2 |
E:lang(fr) |
an element of type E in language "fr" (the document language specifies how language is determined) | 2 |
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 |
E |
an E element whose "foo" attribute value begins exactly with the string "bar" | 3 |
E |
an E element whose "foo" attribute value ends exactly with the string "bar" | 3 |
E |
an E element whose "foo" attribute value contains the substring "bar" | 3 |
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 |