Using Starts-with and Ends-with Attribute Selectors in CSS
📖 Scenario: You are creating a simple webpage for a bookstore. The page has several links to different book categories. Each link has a href attribute that starts with "/books/" and ends with the category name, like "/books/fiction" or "/books/science".You want to style these links differently based on whether their href attribute starts with or ends with certain text.
🎯 Goal: Build CSS rules that use the starts-with (^=) and ends-with ($=) attribute selectors to style links whose href attribute starts with "/books/" and ends with "fiction".The links starting with "/books/" should have a blue color. The links ending with "fiction" should have a green background.
📋 What You'll Learn
Create CSS rules using the starts-with selector
[href^="/books/"] to color links blueCreate CSS rules using the ends-with selector
[href$="fiction"] to give links a green backgroundUse semantic CSS selectors without adding extra classes or IDs
Ensure the CSS is valid and applies correctly to the example HTML links
💡 Why This Matters
🌍 Real World
Websites often need to style links or elements differently based on parts of their attributes, like URLs or IDs. Using starts-with and ends-with selectors helps target these elements without extra classes.
💼 Career
Front-end developers use CSS attribute selectors to write clean, maintainable styles that adapt to dynamic content. Understanding these selectors is essential for styling navigation menus, buttons, and links efficiently.
Progress0 / 4 steps