Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

CSS Selector

A CSS selector selects the HTML element(s) you want to style.


CSS Selector

CSS selectors are used to "find" (or select) the HTML elements 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 CSS element selector

The element selector selects HTML elements based on the element name.

Example:
p {
text-align: center;
color: red;
}


The CSS id Selector

  • To select an element with a specific id, write a hash (#) character, followed by the id of the element.
  • The id of an element is unique within a page, so the id selector is used to select one unique element!
  • The id selector uses the id attribute of an HTML element to select a specific element.

Example
The CSS rule below will be applied to the HTML element with id="p1":
#p1 {
text-align: center;
color: blue;
}


The CSS class Selector

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

Example
In this example all HTML elements with class="c1" will be blue and center-aligned:
.c1 {
text-align: center;
color: blue;
}


The CSS Universal Selector

The universal selector (*) selects all HTML elements on the page.

Example
The CSS rule below will affect every HTML element on the page:
* {
text-align: center;
color: blue;
}


The CSS Grouping Selector

  • The grouping selector selects all the HTML elements with the same style definitions.
  • Look at the following CSS code (the h1, h2, and p elements have the same style definitions):

h1, h2, p {
text-align: center;
color: red;
}


Key points of CSS selector

1. Basic Selectors

  • Element Selector: Targets HTML elements by their tag name.
  • Class Selector: Targets elements by a class attribute.
  • ID Selector:Targets a single element by its unique id attribute.

2. Attribute Selectors

  • Basic Attribute Selector: Targets elements with a specific attribute.

3. Combinators

  • Descendant Selector ( ): Selects all elements that are descendants of a specified element.
  • Child Selector (>): Selects elements that are direct children of a specified element.
  • Adjacent Sibling Selector (+): Selects an element that is directly adjacent (immediately follows) a specified element.
  • General Sibling Selector (~): Selects all sibling elements that follow a specified element.

4. Pseudo-Classes

  • :hover: Targets an element when it's being hovered over by a user.
  • :nth-child(): Targets an element based on its order among its siblings.
  • :first-child, :last-child, :nth-of-type(), etc.: These selectors help in selecting elements based on their position in the DOM structure or their type.

5. Pseudo-Elements

  • ::before and ::after: Insert content before or after an element’s content

6. Grouping Selectors

  • Multiple selectors can be grouped together to apply the same styles

7. Universal Selector

  • he * selector selects all elements

8. Specificity

  • ID selectors have higher specificity than class selectors, and class selectors have higher specificity than element selectors.

9. CSS Selectors for States

  • Selectors can apply styles based on the state of form elements
    • :checked: Targets checked checkboxes or radio buttons.
    • :disabled: Targets disabled form elements