KodingKaro
CSS · Intermediate · Advanced Selectors and Combinators

Attribute selectors and selector strategy

Attribute selectors match elements by the attributes already present in HTML. They are useful for states and link types when a dedicated class would add noise.

✓ What you will learn in this topic

  • Explain the key idea: Attribute selectors read existing markup and should be used when they express clear intent.
  • Apply the concept: Email fields receive a blue edge, external HTTPS links get a stronger underline, and the current page link is emphasized.
  • Avoid this common mistake:

Concept explanation

Selectors can test whether an attribute exists or whether its value equals, begins with, or contains a string. For example, a[href^=\"https\"] matches secure absolute links. Attribute selectors are also useful for styling controls by input type.

Do not use fragile substring matching when a class or semantic state is clearer. A selector should communicate intent and remain understandable to another developer.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

input[type="email"] {
  border-color: #2563eb;
}
a[href^="https"] {
  text-decoration-thickness: 0.12em;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
input[type="email"] {
  border-color: #2563eb;
}
a[href^="https"] {
  text-decoration-thickness: 0.12em;
}

Expected result: Email fields receive a blue edge, external HTTPS links get a stronger underline, and the current page link is emphasized.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card a[aria-current="page"] {
  font-weight: 700;
  text-decoration: underline;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Long chains of selectors become brittle when markup changes; a component class is often a clearer contract.

Best practice

Use the least complex selector that expresses the intended relationship and avoid unnecessary specificity.

Practice exercise

Style a card’s direct heading differently from headings inside nested cards, and add an external-link indicator with an attribute selector. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Attribute selectors read existing markup and should be used when they express clear intent.
  • Use the least complex selector that expresses the intended relationship and avoid unnecessary specificity.

Knowledge check

What does the selector [href^=\"https\"] test?

Reveal the explanation

The ^= operator means “starts with”; exact and contains matching use different operators.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
input[type="email"] {
  border-color: #2563eb;
}
a[href^="https"] {
  text-decoration-thickness: 0.12em;
}

Expected result:

Email fields receive a blue edge, external HTTPS links get a stronger underline, and the current page link is emphasized.

Hands-on practice task

Style a card’s direct heading differently from headings inside nested cards, and add an external-link indicator with an attribute selector.

In this section

Other lessons in Advanced Selectors and Combinators

Broaden your knowledge

Related learning topics