CSS Selectors

CSS Selectors are patterns used to select and style HTML elements. They allow you to target specific elements or groups of elements on a webpage and apply styles to them. Selectors are a core part of CSS and are used in combination with declarations (property-value pairs) to define how elements should be styled.

CSS selectors can be categorized into several types based on their functionality. Below is a detailed explanation of each type:

CSS Syntax

These are the simplest and most commonly used selectors.

  • Purpose: Targets HTML elements by their tag name.
  • Syntax: element
  • Example:
  p {
    color: blue;
  }
CSS

  • This selects all <p> elements and changes their text color to blue.
  • Purpose: Targets elements with a specific class attribute.
  • Syntax: .classname
  • Example:
  .highlight {
    background-color: yellow;
  }
CSS

  • This selects all elements with class="highlight" and gives them a yellow background.
  • Purpose: Targets a single element with a specific id attribute.
  • Syntax: #idname
  • Example:
  #header {
    font-size: 24px;
  }
CSS

  • This selects the element with id="header" and sets its font size to 24px.
  • Purpose: Targets all elements on the page.
  • Syntax: *
  • Example:
  * {
    margin: 0;
    padding: 0;
  }
CSS

  • This removes the default margin and padding from all elements.

These selectors combine multiple selectors to target elements based on their relationship with other elements.

  • Purpose: Targets elements that are descendants of a specified element.
  • Syntax: ancestor descendant
  • Example:
  div p {
    color: red;
  }
CSS

  • This selects all <p> elements that are inside a <div> element.
  • Purpose: Targets direct children of a specified element.
  • Syntax: parent > child
  • Example:
  ul > li {
    list-style: none;
  }
CSS

  • This selects only <li> elements that are direct children of <ul>.
  • Purpose: Targets the element immediately following another element.
  • Syntax: element1 + element2
  • Example:
  h1 + p {
    font-weight: bold;
  }
CSS

  • This selects the <p> element that directly follows an <h1>.
  • Purpose: Targets all siblings of a specified element.
  • Syntax: element1 ~ element2
  • Example:
  h1 ~ p {
    color: green;
  }
CSS

  • This selects all <p> elements that are siblings of an <h1>.

These selectors target elements based on their attributes or attribute values.

  • Purpose: Targets elements with a specific attribute.
  • Syntax: [attribute]
  • Example:
  [target] {
    border: 1px solid black;
  }
CSS

  • This selects all elements with a target attribute.
  • Purpose: Targets elements with a specific attribute value.
  • Syntax: [attribute="value"]
  • Example:
  [type="submit"] {
    background-color: blue;
  }
CSS

  • This selects elements with type="submit".
  • Purpose: Targets elements whose attribute value contains a specific substring.
  • Syntax: [attribute*="value"]
  • Example:
  [href*="example"] {
    color: red;
  }
CSS

  • This selects elements with href containing “example”.

Pseudo-classes target elements based on their state or position.

  • Purpose: Targets an element when the user hovers over it.
  • Syntax: :hover
  • Example:
  a:hover {
    color: orange;
  }
CSS

  • This styles links when hovered.
  • Purpose: Targets an element when it gains focus (e.g., input fields).
  • Syntax: :focus
  • Example:
  input:focus {
    border-color: blue;
  }
CSS

  • This styles input fields when focused.
  • Purpose: Targets the first child of a parent element.
  • Syntax: :first-child
  • Example:
  li:first-child {
    font-weight: bold;
  }
CSS

  • This styles the first <li> in a list.

Pseudo-elements target specific parts of an element.

  • Purpose: Inserts content before an element.
  • Syntax: ::before
  • Example:
  p::before {
    content: "Note: ";
    font-weight: bold;
  }
CSS

  • This adds “Note: ” before every <p> element.
  • Purpose: Inserts content after an element.
  • Syntax: ::after
  • Example:
  p::after {
    content: " (Read more)";
    color: blue;
  }
CSS

  • This adds “(Read more)” after every <p> element.
Selector TypeSyntaxExampleDescription
Element Selectorelementp { color: blue; }Selects all <p> elements.
Class Selector.classname.highlight { background: yellow; }Selects elements with class="highlight".
ID Selector#idname#header { font-size: 24px; }Selects the element with id="header".
Universal Selector** { margin: 0; padding: 0; }Selects all elements.
Descendant Selectorancestor descendantdiv p { color: red; }Selects <p> inside <div>.
Child Selectorparent > childul > li { list-style: none; }Selects direct children of <ul>.
Adjacent Siblingelement1 + element2h1 + p { font-weight: bold; }Selects <p> immediately after <h1>.
General Siblingelement1 ~ element2h1 ~ p { color: green; }Selects all <p> siblings of <h1>.
Attribute Selector[attribute][target] { border: 1px solid black; }Selects elements with target attribute.
Pseudo-Class:pseudo-classa:hover { color: orange; }Styles links when hovered.
Pseudo-Element::pseudo-elementp::before { content: "Note: "; }Adds content before <p>.

CSS selectors are a powerful and essential part of web development, enabling developers to target and style HTML elements with precision. By understanding and utilizing the various types of selectors—such as basic selectors, combinator selectors, attribute selectors, pseudo-classes, and pseudo-elements—you can create dynamic, responsive, and visually appealing web pages.