Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS selector

CSS selector


CSS (Cascading Style Sheets) provides a variety of selectors for selecting the HTML elements to which styles are applied.

CSS selectors are patterns used to select the elements whose styles you want.

The "CSS" column in the table below indicates the CSS version in which a property is defined. CSS1, CSS2, and CSS3 are different versions of Cascading Style Sheets, each introducing new features and capabilities to improve the control and design of web page styles.

Common CSS Selectors

The following are some common CSS selectors:

**Element Selector:** Selects HTML elements by element name.

In the following code, the p selector will select all

element:

p {

  color: blue;

}

**Class Selector:** Selects HTML elements with a specific class by class name.

A class selector starts with . followed by the class name.

In the following code, the .highlight selector will select all elements with the class "highlight" .

.highlight {

  background-color: yellow;

}

**ID Selector:** Selects an HTML element by its unique identifier (ID).

An ID selector starts with # followed by the ID name.

In the following code, the #runoob selector will select the element with the ID "runoob" .

#runoob {

  width: 200px;

}

**Attribute Selector:** Selects HTML elements by their attributes. Attribute selectors can select based on attribute name and attribute value.

In the following code, the input[type="text"] selector will select all type attribute to "text" 's .

input[type="text"] {

  border: 1px solid gray;

}

**Descendant Selector:** Selects HTML elements by specifying a descendant relationship between elements.

Descendant selectors separate element names with a space.

In the following code, the div p selector will select all

elements inside

.

div p {

  font-weight: bold;

}

For more selectors, refer to the table below:

Selector Example Example Description CSS
.class .intro Selects all elements with class="intro" 1
#id #firstname Selects all elements with id="firstname" 1
* * Selects all elements 2
element p Selects all

Element

1
element,element div,p Selects all
element and

Element

1
element*.class* p.hometown Selects all

Element

1
element element div p Choice
elements within all

Element

1
element>element div>p Selects all elements whose parent is
elements'

Element

2
element+element div+p Selects all
elements immediately following the first

Element

2
[attribute] [target] Selects all elements with a target attribute 2
[attribute=value] [target=-blank] Selects all elements using target="-blank" 2
[attribute~=value] [title~=flower] Selects all elements whose title attribute contains the word "flower" 2
[attribute|=language] [lang|=en] Selects all elements whose lang attribute equals en or starts with en- 2
:link a:link Selects all unvisited links 1
:visited a:visited Selects all visited links 1
:active a:active Selects the active link 1
:hover a:hover Selects links when the mouse is over them 1
:focus input:focus Selects the input element that has focus 2
:first-letter p:first-letter Selects the first letter of every

element.

1
:first-line p:first-line Selects the first letter of every

The first line of an element

1
:first-child p:first-child Specifies styles only when

an element is the first child of its parent.

2
:before p:before Insert content after each

Inserts content before an element

2
:after p:after Insert content after each

Inserts content after an element

2
:lang(language) p:lang(it) Selects all elements whose lang attribute starts with "it"

Element

2
element1~element2 p~ul Selects every ul element following a p element 3
[attribute^=value] a[src^="https"] Selects every element whose src attribute value starts with "https" 3
[attribute$=value] a[src$=".pdf"] Selects every element whose src attribute value ends with ".pdf" 3
[attribute*=value] a[src*="runoob"] Selects every element whose src attribute value contains the substring "runoob" 3
:first-of-type p:first-of-type Selects every p element that is the first p element of its parent 3
:last-of-type p:last-of-type Selects every p element that is the last p element of its parent 3
:only-of-type p:only-of-type Selects every p element that is the only p element of its parent 3
:only-child p:only-child Selects every p element that is the only child of its parent 3
:nth-child(n) p:nth-child(2) Selects every p element that is the second child of its parent 3
:nth-last-child(n) p:nth-last-child(2) Selects every p element that is the second child of its parent, counted from the last child 3
:nth-of-type(n) p:nth-of-type(2) Selects every p element that is the second p element of its parent 3
:nth-last-of-type(n) p:nth-last-of-type(2) Selects every p element that is the second p element of its parent, counted from the last 3
:last-child p:last-child Selects every p element that is the last child of its parent. 3
:root :root Selects the root element of the document 3
:empty p:empty Selects every p element that has no children (including text nodes) 3
:target #news:target Selects the currently active #news element (the URL clicked containing that anchor name) 3
:enabled input:enabled Selects every enabled input element 3
:disabled input:disabled Selects every disabled input element 3
:checked input:checked Selects every checked input element 3
:not(selector) :not(p) Selects every element that is not a p element 3
::selection ::selection Matches the portion of an element that is selected or highlighted by the user 3
:out-of-range :out-of-range Matches input elements whose value is outside a specified range 3
:in-range :in-range Matches input elements whose value is within a specified range 3
:read-write :read-write Used to match elements that are readable and writable 3
:read-only :read-only Used to match elements with the "readonly" attribute set 3
:optional :optional Used to match optional input elements 3
:required :required Used to match elements with the "required" attribute set 3
:valid :valid Used to match elements whose input value is valid 3
:invalid :invalid Used to match elements whose input value is invalid 3
:has :has Allows selecting an element based on its descendant elements. 3
:is :is Accepts any number of selectors as arguments and returns the union of the elements matched by those selectors. 3