Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 1: Foundations & Selectors
CSS ⏱ 12 min read

CSS Selectors (Basic, Combinators, Attribute)

Selectors are the targeting system of CSS. Mastering them lets you style any element on the page with surgical precision.

What is it?

CSS selectors define which HTML elements on the page the style rules will apply to. They range from simple element names to complex combinators and attribute filters.

Why is it used?

Using precise selectors keeps your HTML clean and saves you from having to attach redundant classes to every single nested element.

Detailed Explanation

Types of Selectors: 1. Simple Selectors: Tag selector (`p`), Class selector (`.card`), ID selector (`#header`), Universal selector (`*`). 2. Combinator Selectors: • Descendant (`div p`): All `<p>` inside `<div>`. • Child (`div > p`): Direct children `<p>` only. • Adjacent Sibling (`h2 + p`): The `<p>` immediately following an `<h2>`. • General Sibling (`h2 ~ p`): Any `<p>` following an `<h2>`. 3. Attribute Selectors: • `[required]`: Elements with required attribute. • `input[type="email"]`: Input of type email. • `a[href^="https"]`: Links whose URL starts with https.

Syntax Structure

Syntax Blueprint
/* Element */ tag { ... }
/* Class */ .class-name { ... }
/* ID */ #id-name { ... }
/* Child */ parent > child { ... }
/* Attribute */ [attribute="value"] { ... }

Code Example

CSS Code
/* Universal box-sizing reset */
*, *::before, *::after {
  box-sizing: border-box;
}

/* Class selector */
.tutorial-container {
  max-width: 1200px;
  margin: 0 auto;
}

/* Child combinator: only direct children */
.nav-list > li {
  display: inline-block;
  margin-right: 16px;
}

/* Adjacent sibling: first paragraph after an H2 */
h2 + p {
  font-size: 18px;
  color: #5f5f5d;
}

/* Attribute selector */
input[type="text"]:focus {
  border-color: #1c1c1c;
  outline: none;
}

Explanation of the Code

• `*, *::before, *::after` applies border-box globally to all elements. • `.nav-list > li` ensures nested submenus do not inherit top-level horizontal display accidentally. • `input[type="text"]:focus` styles text inputs when clicked.

Expected Output

The layout applies reset dimensions, styles horizontal nav items, enlarges lead paragraphs, and outlines active inputs.

Real-World Application

Design systems like Bootstrap and Tailwind use attribute and combinator selectors to style complex component hierarchies.

Common Mistakes to Avoid

• Over-relying on ID selectors (`#my-btn`) which have excessively high specificity and are hard to override. • Confusing descendant (`div p`) with child (`div > p`).

Quick Practice Challenge

Write a selector that targets only external links (href starting with 'https://') and gives them an underline and bold font.

Key Takeaways

• Prefer class selectors (`.my-class`) for modular, reusable component styling. • Use combinators (`>`, `+`, `~`) to style relationships between elements. • Use attribute selectors for input types and link patterns.