Advanced CSS Selectors

    Lesson 12 โ€ข Expert Track

    What You'll Learn

    Pseudo-classes: :hover, :focus, :nth-child, :not()
    Pseudo-elements: ::before, ::after, ::first-letter
    Combinators: descendant, child, sibling selectors
    Attribute selectors: [href], [type='email'], [class*='btn']
    Form state selectors: :valid, :invalid, :checked
    Build a styled list and form with zero extra classes

    ๐Ÿ’ก Think of It Like This

    Basic selectors are like addressing a letter to "John". Advanced selectors are like saying "the third John who lives on Main Street, but only when he's at home." They give you surgical precision โ€” you can target any element based on its state, position, attributes, or relationship to other elements.

    1. Pseudo-classes โ€” Targeting Element States

    Pseudo-classes select elements based on their current state โ€” whether they're hovered, focused, the first child, or match a pattern.

    CategorySelectorWhat It Targets
    Interactive:hoverMouse is over element
    :focusElement has keyboard focus
    :activeElement is being clicked
    :visitedLink has been visited
    Position:first-childFirst child of parent
    :last-childLast child of parent
    :nth-child(n)Nth child (odd, even, 3n, 2n+1)
    :not(selector)Everything except the selector
    Form:checkedChecked checkbox/radio
    :valid / :invalidForm validation state
    :disabledDisabled form element

    Pseudo-classes in Action

    See :hover, :nth-child, :first-child, and :not() at work

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>Pseudo-classes</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 30px;
          color: #e5e7eb;
        }
    
        h3 { color: #22c55e; margin: 25px 0 10px; }
    
        /* Styled list with nth-child */
        ul { list-style: none; padding: 0; max-width: 400px; }
    
        li {
          padding: 12px 16px;
          border-radius: 6px;
          margin: 4px 0;
          transition: all 0.2s;
        }
    
        /* Alternate row colours */
       
    ...

    2. Pseudo-elements โ€” Virtual Content

    Pseudo-elements create virtual elements that don't exist in the HTML. They're perfect for decorative content, icons, and styling parts of text.

    Pseudo-elementWhat It Does
    ::beforeInserts content before element (requires content)
    ::afterInserts content after element (requires content)
    ::first-letterStyles only the first letter of text
    ::first-lineStyles only the first line of a paragraph
    ::selectionStyles highlighted/selected text

    Pseudo-elements

    Add decorative content and style text parts without extra HTML

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>Pseudo-elements</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 30px;
          color: #e5e7eb;
        }
    
        h3 { color: #22c55e; margin: 25px 0 10px; }
    
        /* ::before and ::after */
        .quote {
          background: #1e293b;
          padding: 20px 25px;
          border-radius: 8px;
          font-style: italic;
          font-size: 1.1rem;
          position: relative;
          margin: 15px 0;
        }
    
        .quote::before 
    ...

    3. Combinators โ€” Relationship Selectors

    Combinators target elements based on their relationship to other elements in the DOM tree.

    CombinatorSyntaxTargets
    Descendantdiv pAll p elements inside div (any depth)
    Childdiv > pOnly direct child p elements of div
    Adjacent siblingh2 + pFirst p immediately after h2
    General siblingh2 ~ pAll p elements after h2 (same parent)

    4. Attribute Selectors

    Target elements based on their HTML attributes โ€” incredibly useful for styling links, inputs, and data attributes.

    SelectorMatchesExample Use
    [href]Has the attributeAny element with href
    [type="email"]Exact matchEmail inputs only
    [href*="youtube"]Contains substringLinks containing "youtube"
    [href^="https"]Starts withSecure links
    [href$=".pdf"]Ends withPDF download links

    Attribute Selectors

    Style links, inputs, and elements based on their attributes

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>Attribute Selectors</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 30px;
          color: #e5e7eb;
        }
    
        h3 { color: #22c55e; margin: 25px 0 10px; }
    
        .links { display: flex; flex-direction: column; gap: 8px; max-width: 400px; }
    
        a {
          display: block;
          padding: 12px 16px;
          background: #1e293b;
          color: #94a3b8;
          text-decoration: none;
          border-radius: 6px;
     
    ...

    โš ๏ธ Common Mistakes

    • Confusing : and :: โ€” Single colon :hover is a pseudo-class (state). Double colon ::before is a pseudo-element (virtual content).
    • Forgetting content for ::before/::after โ€” These pseudo-elements require the content property, even if it's empty: content: "";
    • Over-specific selectors โ€” div.container ul li a:hover is overly specific and fragile. Keep selectors as short as possible.
    • Using :nth-child when you mean :nth-of-type โ€” :nth-child(2) targets the 2nd child regardless of type. :nth-of-type(2) targets the 2nd element of that specific type.

    ๐ŸŽ‰ Lesson Complete

    You now have surgical precision when targeting CSS elements:

    • โœ… Pseudo-classes target states: :hover, :focus, :nth-child, :not()
    • โœ… Pseudo-elements create virtual content: ::before, ::after, ::first-letter
    • โœ… Combinators target relationships: child (>), sibling (+, ~)
    • โœ… Attribute selectors target by attribute: [href$=".pdf"], [type="email"]

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service