A CSS selector selects the HTML element(s) you want to style
We can divide CSS selectors into five categories:
Simple selectors (select elements based on name, id, class)
Combinator selectors (select elements based on a specific relationship between them)
Pseudo-class selectors (select elements based on a certain state)
Pseudo-elements selectors (select and style a part of an element)
Attribute selectors (select elements based on an attribute or attribute value)
The element selector selects HTML elements based on the element name
Here, all <p> elements on the page will be
center-aligned, with a red text color:
p
{
text-align: center;
color: red;
}
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the element.
The CSS rule below will be applied to the HTML element with id="para1":
#para1
{
text-align: center;
color: red;
}
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name
In this example all HTML elements with class="center" will be red and center-aligned:
.center
{
text-align: center;
color: red;
}
A combinator is something that explains the relationship between the selectors.
A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator.
There are four different combinators in CSS:
The descendant selector matches all elements that are descendants of a specified element.
The following example selects all <p> elements inside <div> elements:
div p {
background-color: yellow;
}
The child selector matches all elements that are child of a specified element.
The following example selects all <p> chiled of <div> elements:
div p >{
background-color: yellow;
}
The adjacent sibling selector is used to select an element that is directly after another specific element.
Sibling elements must have the same parent element, and "adjacent" means "immediately following"
The following example selects the first <p> element that are placed immediately after <div> elements:
div p +{
background-color: yellow;
}
The general sibling selector selects all elements that are next siblings of a specified element.
The following example selects all <p> elements that are next siblings of <div> elements:
div p ~ {
background-color: yellow;
}
A pseudo-class is used to define a special state of an element.
For example, it can be used to:
Style an element when a user mouses over it
Style visited and unvisited links differently
Style an element when it gets focus
selector:pseudo-class {
property: value;
}
selector::pseudo-element {
property: value;
}
The [attribute] selector is used to select elements with a specified attribute.
The following example selects all <a> elements with a target attribute:
a[target] {
background-color: yellow;
}
The [attribute="value"] selector is used to select elements with a specified attribute and value.
The following example selects all <a> elements with a target="_blank" attribute:
a[target="_blank"] {
background-color: yellow;
}
Aenean lorem odio, mollis sed consequat et, pellentesque id purus. Nunc sagittis malesuada urna, ultricies lacinia nisi varius vitae. Aliquam sit amet egestas sapien, nec mollis quam.