Discover how a few simple symbols can save you hours of tedious CSS work!
Why Starts-with and ends-with selectors in CSS? - Purpose & Use Cases
Imagine you have a long list of product codes on a webpage. You want to highlight all products whose codes start with "A" or end with "Z" by changing their color.
If you try to do this manually, you must write separate CSS rules for each exact code or add special classes to each item. This is slow, error-prone, and hard to maintain when new products are added.
Starts-with and ends-with selectors let you target elements based on the beginning or ending of attribute values. This means you can write one rule to style all matching items automatically.
li[data-code='A123'] { color: red; } li[data-code='A456'] { color: red; } li[data-code='XYZ'] { color: blue; }
li[data-code^='A'] { color: red; } li[data-code$='Z'] { color: blue; }
You can style groups of elements dynamically based on attribute patterns without extra HTML changes.
On an online store, highlight all items starting with "NEW" to show new arrivals, or all items ending with "-sale" to mark discounted products.
Manual styling for many similar items is slow and error-prone.
Starts-with (^=) and ends-with ($=) selectors target attribute values by their start or end.
This makes styling dynamic groups easy and maintainable.