CSS Attribute Selectors

CSS attribute selectors are used to select and style HTML elements with a specific attribute or attribute value, or both.

Attribute selectors are enclosed in square brackets [].

CSS has the following attribute selectors:

CSS [attribute] Selector

The [attribute] selector is used to select elements with the specified attribute.

The following example selects all <a> elements with a target attribute:

Example

Result

CSS [attribute=”value”] Selector

The [attribute="value"] selector is used to select elements with a specific attribute with an exact value.

The following example selects all <a> elements with a target=”_blank” attribute:

Example

CSS Child (>) Combinator

Definition and Usage

The CSS child combinator (>) is used to select elements that are direct children of a specific parent.

Elements matched by the second selector must be the immediate children of the elements matched by the first selector.

Example

Result

CSS Pseudo-classes

A CSS pseudo-class is a keyword that can be added to a selector, to define a style for a special state of an element.

Some common use for pseudo-classes:

Interactive Pseudo-classes

Interactive pseudo-classes apply styles based on user interaction:

Structural Pseudo-classes

Structural pseudo-classes select elements based on their position in the document tree:

CSS :hover Pseudo-class

Definition and Usage

The CSS :hover pseudo-class is used to select elements when you mouse over them.

Example

CSS :focus Pseudo-class

Definition and Usage

The CSS :focus pseudo-class is used to select and style the element that gets focus.

Example

CSS :link Pseudo-class

Definition and Usage

The CSS :link pseudo-class is used to select and style unvisited links.

Example

CSS :visited Pseudo-class

Definition and Usage

The CSS :visited pseudo-class is used to style visited links.

Example

CSS :active Pseudo-class

Definition and Usage

The CSS :active pseudo-class is used to select and style an element that is being activated by the user.

Example

:focus on <input>

Here is an example of using the :focus pseudo-class to style an input field when it gets focus:

Example

:hover on <div>

Here is an example of using the :hover pseudo-class on a <div> element:

Example

CSS Pseudo-Elements

A CSS pseudo-element is a keyword that can be added to a selector, to style a specific part of an element.

Some common use for pseudo-elements:

Text Pseudo-elements

Text pseudo-elements style specific parts of text content:

Content Pseudo-elements

Content pseudo-elements insert or style generated content:

Pseudo-elements for Text

Pseudo-elements for text, allow you to style specific parts of text content, such as the first line or first letter.


The CSS ::first-line Pseudo-element

The ::first-line pseudo-element is used to add a special style to the first line of a text.

Example

Result

The CSS ::first-letter Pseudo-element

The ::first-letter pseudo-element is used to add a special style to the first letter of a text.

Example

Result

Pseudo-elements and HTML Classes

Pseudo-elements can easily be combined with HTML classes.

Example

Result

Pseudo-elements for Content

Pseudo-elements for content allow you to insert content before or after elements, style list markers, and more.

The CSS ::before Pseudo-element

The ::before pseudo-element is used to insert some content before the content of a specified element.

Use the content property to specify the content to insert.

Example

Result

The CSS ::after Pseudo-element

The ::after pseudo-element is used to insert some content after the content of a specified element.

Use the content property to specify the content to insert.

Example

Result 

The CSS ::marker Pseudo-element

The ::marker pseudo-element is used to style the list item markers.

Example

Result

The CSS ::selection Pseudo-element

The ::selection pseudo-element is used to style the part of a text that is selected by a user.

Example

Result

The CSS ::backdrop Pseudo-element

The ::backdrop pseudo-element is used to style the viewbox behind a dialog box or popover element.

Example

Result