Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Welcome to CSS for Intermediate (Level 2)! 🚀
In Level 1, you learned how to select elements by tag name (p), class name (.card), and ID (#header).
While classes are great, writing classes for every single variation quickly clutters your HTML: <input type="text" class="input input-text input-required input-disabled"> 😵
HTML tags already carry a treasure trove of information inside their attributes: type, href, target, disabled, required, download, and custom data-* attributes.
With CSS Attribute Selectors, you can directly target elements based on what attributes they possess or what text their attributes contain!
In this lesson, you will master:
[disabled], [required])[type="email"])^= (Starts with)$= (Ends with)*= (Contains anywhere)i flagImagine a school librarian searching the Student Admission Database:
+-------------------------------------------------------------------------+ | ATTRIBUTE SELECTORS IN A SCHOOL DATABASE | +-------------------------------------------------------------------------+ | 1. [scholarship] (PRESENCE) | | "Find all students who have a scholarship flag on their file, | | regardless of the scholarship amount." | | | | 2. [section="10-A"] (EXACT MATCH) | | "Find students whose class section is EXACTLY '10-A'." | | | | 3. [roll^="2026"] (STARTS WITH: ^=) | | "Find all students whose roll number starts with the year '2026'!" | | | | 4. [email$="@school.edu"] (ENDS WITH: $=) | | "Find all users whose email ends with the official '@school.edu' | | domain extension!" | | | | 5. [subjects*="Bio"] (CONTAINS: *=) | | "Find any student who has 'Bio' anywhere in their subject list!" | +-------------------------------------------------------------------------+
[attribute]Matches any element that simply has the specified attribute present, regardless of what its value is:
[attribute="value"]Matches elements where the attribute value is an exact, character-for-character match:
^=, $=, *=)Modern websites dynamically generate URLs and IDs. You often cannot predict the exact full string, but you know how it begins, ends, or what word it contains!
^=)Matches when the attribute value begins with the specified string:
$=)Matches when the attribute value ends with the specified string:
This is one of the most beloved tricks in professional web design: Automatically styling download file links by their extension!
*=)Matches when the specified string appears anywhere inside the attribute value:
i FlagBy default, attribute values in CSS can be case-sensitive depending on the attribute. If a user uploads a file with .PDF in uppercase, a[href$=".pdf"] might fail!
To force case-insensitive matching, add the i modifier before the closing bracket:
| Selector | Meaning | Example | Real-World Use Case |
|---|---|---|---|
[attr] | Attribute is present | input[disabled] | Styling disabled form controls |
[attr="val"] | Exact match | input[type="radio"] | Styling radio buttons |
[attr^="val"] | Starts with | a[href^="https://"] | Styling external secure links |
[attr$="val"] | Ends with | a[href$=".pdf"] | Auto-detecting document downloads |
[attr*="val"] | Contains anywhere | a[href*="youtube"] | Video link badges |
[attr~="val"] | Space-separated word | [data-tags~="maths"] | Filtering custom tag lists |
[attr="val" i] | Case-insensitive | a[href$=".jpg" i] | Catching both .jpg and .JPG |
| Bad Practice ❌ | Why It Fails ⚠️ | Better Approach ✅ |
|---|---|---|
Writing input[required="true"] | HTML boolean attributes like required or disabled do not need a value. | Just write input[required] or button[disabled]. |
Confusing ^= (starts with) and $= (ends with) | Writing a[href^=".pdf"] looks for links starting with .pdf, matching zero URLs! | Use $= for file extensions: a[href$=".pdf"]. |
| Forgetting quotes around URLs with special characters | While simple values work without quotes, values with slashes (https://) can cause parsing errors. | Always wrap attribute values in quotes: [href^="https://"]. |
Not adding the i flag for file extensions | Misses downloads ending with capital .PDF or .PNG. | Add i: a[href$=".pdf" i]. |
[attr] checks presence; [attr="val"] checks exact equality.^= = Starts with (href^="https://")$= = Ends with (href$=".pdf")** = Contains anywhere (href*="portal"`)i modifier: Makes substring comparison case-insensitive.href attribute ends with .pdf?^=) signify in an attribute selector like a[href^="https://"]?.pdf and uppercase .PDF?*=) operator match in a[href*="wikipedia"]?school-downloads-center.html.Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| None | Child, Descendant, and Sibling Selectors: Navigating the DOM Tree |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.