This site is designed for accessibility. Content is obtainable and functional to any browser or Internet device. This site's full visual experience is available in a graphical browser that supports web standards. Please consider upgrading your web browser.
Term | Meaning |
---|---|
Adjacent sibling selectors |
Adjacent sibling selectors have the following syntax: E1 + E2, where
E2 is the subject of the selector. The selector matches if E1 and E2 share
the same parent in the document tree and E1 immediately precedes E2. + = sibling (immediately follows) body + p This is rather unlike any other aspect of selectors, as it relates two elements that are at the same depth in the tree. Support isn't universal, but adjacent sibling selectorsr are still pretty useful. |
Ancestor | An element A is called an ancestor of an element B, if and only if B is a descendant of A. |
Attribute selectors |
CSS2 allows authors to specify rules that match attributes defined in the source document. Attribute selectors may match in four ways:
Attribute values must be identifiers or strings. The case-sensitivity of attribute names and values in selectors depends on the document language. |
Child | An element A is called the child of element B if an only if B is the parent of A. |
child selector |
> = child body > p > constrains the relationship between two elements to be parent-child. It is related to, but not quite the same as, descendant selection. Support isn't universal, but the child selector is still pretty useful. |
Class selectors | For style sheets used with HTML, authors may use the dot (.) notation as an alternative to the "~=" notation when matching on the "class" attribute. Thus, for HTML, "DIV.value" and "DIV[class~=value]" have the same meaning. The attribute value must immediately follow the ".". |
Descendant | element element = descendant An element A is called a descendant of an element B, if either (1) A is a child of B, or (2) A is the child of some element C that is a descendant of B. |
Descendant selectors |
At times, authors may want selectors to match an element that is the descendant of another element in the document tree (e.g., "Match those EM elements that are contained by an H1 element"). Descendant selectors express such a relationship in a pattern. A descendant selector is made up of two or more selectors separated by whitespace. A descendant selector of the form "A B" matches when an element B is an arbitrary descendant of some ancestor element A. body p |
Document tree | The tree of elements encoded in the source document. Each element in this tree has exactly one parent, with the exception of the root element, which has none. |
Following element | An element A is called a following element of an element B, if and only if B is a preceding element of A. |
ID selectors |
Document languages may contain attributes that are declared to be of type ID. What makes attributes of type ID special is that no two such attributes can have the same value; whatever the document language, an ID attribute can be used to uniquely identify its element. In HTML all ID attributes are named "id"; XML applications may name ID attributes differently, but the same restriction applies. The ID attribute of a document language allows authors to assign an identifier to one element instance in the document tree. CSS ID selectors match an element instance based on its identifier. A CSS ID selector contains a "#" immediately followed by the ID value. ID selectors have a higher precedence than attribute selectors. For example, in HTML, the selector #p123 is more specific than [ID=p123] in terms of the cascade. |
Pseudo-classes |
Pseudo-classes classify elements on characteristics other than their name, attributes or content; in principle characteristics that cannot be deduced from the document tree. Pseudo-classes may be dynamic, in the sense that an element may acquire or lose a pseudo-class while a user interacts with the document. The exception is ':first-child', which can be deduced from the document tree. Examples:
|
Pseudo-elements |
The :first-line pseudo-element applies special styles to the first formatted line of a paragraph Examples:
|
Selectors |
The parts of a rule that select the elements to be styled with the associated declaration block. Without selectors, CSS couldn't work!. Selectors tie styles and the document structure together. Any element which can be described by a selector is said to match that selector. For example, <em> matches the selector em, and will almost certainly match the selector body em. Selectors can be grouped for efficiency. Grouped selectors are separated by commas: h1, h2, h3, div.subsec {color: rgb(100%,80%,20%);} This is the same as writing several rules with identical declarations and a single selector each. There are many varieties of selector
|
Universal selector |
This one's sort of a wild card Acts like a type element, except it matches any and every element it can Therefore, * {color: red;} would turn every element in the entire document red Can be used as part of other selector types For example, div * li {font-weight: bold;} would boldface any li element that is at least a grandchild of a div element You could color every element with a class attribute by writing*[class] {color: green;} Match any element that immediately follows an h1 with h1 + p { } Legacy note: *.class { } is equivalent to .class { } (ditto for IDs)* = any But its position in a selector can affect the scope of "any." Some examples. * {color: red;} That will select any (and all) elements in a document. Every single last one of them. ul * {color: purple;} That will select any element that's descended from a ul element. That would include its li children, and any descednants of those list items. So a ul nested inside an li would be selected, as would all of its descendants. ul * li {color: blue;} That selects any li that's descended from any element that's descended from a ul. So the list items in an unordered list would not be selected, unless that unordered list were descended from another unordered list. |
|= |
means equals or begins with |
~= |
means contains |