CSS explained

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.

Syntax

CSS has a simple syntax and uses a number of English keywords to specify the names of various style properties.

Style sheet

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.

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:

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, 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.

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 applies to all elements of class myClass that are inside div elements, whereas .myClass div 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 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
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

Notes and References

  1. Web site: CSS developer guide. MDN Web Docs. 2015-09-24. https://web.archive.org/web/20150925133829/https://developer.mozilla.org/en-US/docs/Web/Guide/CSS. 2015-09-25. live.
  2. Book: Flanagan, David. JavaScript: the definitive guide. 18 April 2011. O'Reilly. 978-1-4493-9385-4. Beijing; Farnham. 1. English. 686709345. JavaScript is part of the triad of technologies that all Web developers must learn: HTML to specify the content of web pages, CSS to specify the presentation of web pages, and JavaScript to specify the behavior of web pages..
  3. Web site: What is CSS?. World Wide Web Consortium. 2010-12-01. https://web.archive.org/web/20101129081921/https://www.w3.org/standards/webdesign/htmlcss#whatcss. 2010-11-29. live.
  4. News: Web-based Mobile Apps of the Future Using HTML 5, CSS and JavaScript. HTML Goodies . 23 July 2010. HTMLGoodies. 2014-10-16. https://web.archive.org/web/20141020121735/https://www.htmlgoodies.com/beyond/article.php/3893911/Web-based-Mobile-Apps-of-the-Future-Using-HTML-5-CSS-and-JavaScript.htm. 2014-10-20. live. Clark . Scott .
  5. Web site: W3C CSS validation service . 2012-06-30 . https://web.archive.org/web/20110214164625/https://jigsaw.w3.org/css-validator/ . 2011-02-14 . live .
  6. Web site: W3C CSS2.1 specification for pseudo-elements and pseudo-classes . World Wide Web Consortium . 7 June 2011 . 30 April 2012 . https://web.archive.org/web/20120430011514/https://www.w3.org/TR/CSS21/selector.html#pseudo-elements . 30 April 2012 . live .
  7. Web site: live . Selectors . https://web.archive.org/web/20060423174213/https://www.w3.org/TR/CSS21/selector.html . 2006-04-23 . W3C . Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification.
  8. Web site: Selectors Level 3 . W3C . 2014-05-30 . https://web.archive.org/web/20140603165900/https://www.w3.org/TR/selectors/ . 2014-06-03 . live .