Selectors of CSS

There are five main selectors of CSS:

  1. SIMPLE SELECTOR
  2. COMBINATOR SELECTOR
  3. ATTRIBUTE SELECTOR
  4. PSEUDO CLASS SELECTOR
  5. PSEUDO ELEMENT SELECTOR

1. SIMPLE SELECTOR

A "simple selector" in CSS refers to the most basic way to target an HTML element for styling. It can be one of the following:

  1. Type Selector: Targets all elements of a specific type (e.g., div, p, h1).
  2. Class Selector: Targets elements with a specific class (e.g., .class-name).
  3. ID Selector: Targets a specific element with a given ID (e.g., #id-name).
  4. Universal Selector: Targets all elements on the page (e.g., *).

Example of Simple Selectors


  1. BY TAG NAME
  2. For example, styling all <p> tags:

    <style>
    p {
      color: blue;
    }
    </style>
    <body>
        <p>I am a paragraph</p>
        <div>I am a div tag</div>
    </body>
  3. BY CLASS
  4. For example, styling elements with a specific class:

    <style>
    .example-class {
      color: red;
    }
    </style>
    <body>
        <div class="example-class">I am a div with class "example-class"</div>
    </body>
  5. BY ID
  6. For example, styling an element with a specific ID:

    <style>
    #unique {
      color: green;
    }
    </style>
    <body>
        <div id="unique">I am a div with ID "unique"</div>
    </body>
  7. UNIVERSAL SELECTOR
  8. For example, styling all elements:

    <style>
    * {
      margin: 0;
      padding: 0;
    }
    </style>
    <body>
        <p>This is a paragraph.</p>
        <div>This is a div.</div>
    </body>

    The universal selector (*) targets all elements on the page and sets their margin and padding to zero, providing a consistent layout across different browsers.

  9. GROUPING SELECTOR
  10. The "grouping selector" in CSS allows you to apply the same styles to multiple selectors, separated by a comma. This helps reduce redundancy in your code.

    GROUPING SELECTOR EXAMPLE:

    For example, applying the same style to both <h1> and <p> tags:

    <style>
    h1, p {
      color: purple;
    }
    </style>
    <body>
        <h1>This is a Heading</h1>
        <p>This is a paragraph.</p>
    </body>

    In this example, both the <h1> and <p> elements are styled with the same font and color. The h1, p selector groups the two elements together.


2. COMBINATOR SELECTOR

Combinator selectors in CSS allow you to select elements based on their relationships with other elements.

PARENT CHILD RELATIONSHIP

<div>
    <p>Immediate sibling.</p>
    <p>Direct child.</p>
    <span>
      <p>Indirect child.</p>
    </span>
</div>
<p>Immediate sibling.</p>
    <p>General sibling.</p>
  1. DESCENDENT SELECTOR ( )
  2. The descendant selector targets an element that is inside another element, regardless of how deep inside it is.

    <style>
    div p {
      color: blue;
    }
    </style>
    <div>
      <p>This is a paragraph inside a div element.</p>
    </div>
  3. DIRECT CHILD SELECTOR (>)
  4. The direct child selector selects elements that are immediate children of another element.

    <style>
    div > p {
      color: red;
    }
    </style>
    <div>
      <p>This is a direct child paragraph.</p>
      <div>
        <p>This is not a direct child paragraph.</p>
      </div>
    </div>
  5. IMMEDIATE OR ADJACENT SIBLING SELECTOR (+)
  6. The adjacent sibling selector selects an element that is immediately preceded by a specified element.

    <style>
    h1 + p {
      color: green;
    }
    </style>
    <h1>Heading 1</h1>
    <p>This paragraph is immediately after an h1 element.</p>
  7. GENERAL SIBLING SELECTOR (~)
  8. The general sibling selector selects all elements that are siblings of a specified element.

    <style>
    h1 ~ p {
      color: orange;
    }
    </style>
    <h1>Heading 1</h1>
    <p>This is a sibling of h1.</p>
    <p>This is another sibling of h1.</p>

3. ATTRIBUTE SELECTOR

The attribute selector in CSS targets elements with a specific attribute or attribute value.

<body>
<p class="ptag">I am a paragraph with a class attribute</p>
<p id="ptag">I am another paragraph with an id attribute</p>
</body>
  1. Tag Name[Attribute Name]
  2. This targets elements that have a specified attribute, regardless of the attribute's value.

    
    p[class] {
      background-color: blue;
    }
    
  3. Tag Name[Attribute="Value"]
  4. This targets elements where the attribute has an exact value.

    
    p[class="ptag"] {
      background-color: red;
    }
    
  5. Prefix Value Selector (^=)
  6. This targets elements where the attribute value starts with a specific prefix.

    
    div[class^="div"] {
      background-color: blue;
    }
    
  7. Suffix Value Selector ($=)
  8. This targets elements where the attribute value ends with a specific suffix.

    
    div[class$="container"] {
      background-color: green;
    }
    
  9. Substring Value Selector (*=)
  10. This targets elements where the attribute value contains a specific substring.

    
    div[class*="content"] {
      background-color: yellow;
    }
    
  11. Whitespace-Separated Value Selector (~=)
  12. This targets elements where the attribute value is a whitespace-separated list and one of the values matches exactly.

    
    div[class~="container"] {
      background-color: pink;
    }
    

4. PSEUDO CLASS SELECTOR

Pseudo-classes are used to define the special state of an element.

Common Pseudo-Classes

  1. :hover - Targets an element when the mouse hovers over it.
  2. <style>
    a:hover {
      color: red;
    }
    </style>
    <a href="https://example.com">Hover me</a>
  3. :focus - Targets an element when it receives focus, typically on form fields.
  4. <style>
    input:focus {
      border-color: blue;
    }
    </style>
    <input type="text">
  5. :active - Targets an element when it is being clicked.
  6. <style>
    button:active {
      background-color: gray;
    }
    </style>
    <button>Click me</button>
  7. :first-child - Targets the first child element of a parent.
  8. <style>
    div p:first-child {
      color: green;
    }
    </style>
    <div>
      <p>This is the first paragraph</p>
      <p>This is the second paragraph</p>
    </div>
  9. :nth-child(n) - Targets the nth child of a parent.
  10. <style>
    ul li:nth-child(2) {
      color: red;
    }
    </style>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>

5. PSEUDO ELEMENT SELECTOR

Pseudo-elements allow you to style specific parts of an element.

Pseudo-elements are used to style specific parts of an element, such as the first letter or line of text, or to insert content before or after an element.

Common Pseudo-Elements

  1. ::first-letter - Targets the first letter of a block-level element.
  2. <style>
    p::first-letter {
      font-size: 2em;
      color: red;
    }
    </style>
    <p>This is a paragraph.</p>
  3. ::first-line - Targets the first line of a block-level element.
  4. <style>
    p::first-line {
      font-weight: bold;
      color: blue;
    }
    </style>
    <p>This is a paragraph with multiple lines. The first line will be styled differently.</p>
  5. ::before - Inserts content before an element.
  6. <style>
    p::before {
      content: "Note: ";
      color: green;
    }
    </style>
    <p>This is a paragraph.</p>
  7. ::after - Inserts content after an element.
  8. <style>
    p::after {
      content: " - End";
      color: gray;
    }
    </style>
    <p>This is a paragraph.</p>
  9. ::selection - Targets the portion of an element that is selected by the user.
  10. <style>
    ::selection {
      background-color: yellow;
      color: black;
    }
    </style>
    <p>Select this text to see the effect.</p>