06. Basic Selectors, Combinators & Attribute Selectors

🟢 Beginner

1. Definition

A CSS Selector is the part of a CSS rule set that targets specific HTML elements on a webpage so that property declarations can be applied. Basic types include Universal (*), Element (p), Class (.btn), ID (#main), Grouping (h1, h2), Combinators (>, +, ~), and Attribute Selectors ([type="text"]).

2. Why It Is Used

Selectors enable precise targeting control. Without selectors, CSS would have no way of distinguishing between headings, action buttons, paragraphs, or hero banners.

3. How It Works

The browser engine matches selector syntax against DOM nodes. Element selectors match tag names, class selectors match class="..." attributes, ID selectors match id="..." attributes, and combinators match structural parent-child relationships.

4. Syntax

/* Basic selector syntax formats */
* { box-sizing: border-box; }        /* Universal */
p { color: #333; }                   /* Element */
.btn { padding: 10px; }               /* Class */
#hero { font-size: 2rem; }           /* ID */
[type="text"] { border: 1px solid; } /* Attribute */

5. Basic Selector Types Reference

Selector TypeSyntax FormatTarget DescriptionSpecificity Score
Universal*Targets all elements on page0, 0, 0, 0
ElementpTargets elements by HTML tag name0, 0, 0, 1
Class.classnameTargets elements with matching class attribute0, 0, 1, 0
ID#idnameTargets single unique element with matching ID0, 1, 0, 0
Direct Childdiv > pTargets paragraphs that are direct children of div0, 0, 0, 2
Adjacent Siblingh1 + pTargets paragraph immediately following h10, 0, 0, 2

6. Beginner Example

/* Element & class selectors */
h2 {
  color: #1e3a8a;
}

.text-muted {
  color: #64748b;
}

7. Intermediate Example

/* Combinator selectors */
.card > .card-title {
  font-size: 1.25rem;
  font-weight: 700;
}

.card-title + .card-subtitle {
  margin-top: 4px;
}

8. Advanced Attribute Selector Example

/* Advanced attribute matching */
a[href^="https://"] {
  color: #2563eb;
  padding-right: 18px;
}

input[type="submit"]:hover {
  background-color: #1d4ed8;
}

9. Line-by-Line Code Explanation

  • .card > .card-title: Direct child combinator matching .card-title only if it is a direct child of .card.
  • a[href^="https://"]: Attribute selector matching links whose href starts with https://.

10. Expected Terminal / Browser Output

Card component elements and external links formatted precisely based on tag, class, and structural relationship.

11. Common Mistakes & Pitfalls

  • Reusing the same ID on multiple elements: IDs must be completely unique per page. Use classes for reusable styles.

12. Important Rules & Limitations

  • Class names in CSS start with a dot .; ID names start with a hash #.

13. Multilingual Explanation: English

Basic selectors target HTML elements by tag, class, ID, or structural parent-child relationships.

14. Multilingual Explanation: Hindi (Roman Script)

Selectors HTML elements ko target karte hain. Class ke liye . aur ID ke liye # ka use karein.

15. Multilingual Explanation: Marathi (Roman Script)

Selectors HTML elements na target kartat. Class sathi .class ani ID sathi #id vapartat.

16. Practice Questions

  1. What is the difference between a descendant combinator (div p) and a direct child combinator (div > p)?

17. Practical Coding Challenge

Write CSS selectors targeting all external links starting with https:// and give them a green accent color `#16a34a`.

18. Related Topics