Is there a CSS selector for text content? (select elements by text)
Codemia Knowledge Hub ·
No, standard CSS does not have a selector that matches elements based on their text content. Selectors work on DOM structure (element names, classes, IDs, attributes, and relationships between elements) and cannot inspect text nodes. To style elements by the text they contain, add semantic markup (classes or data attributes) or use JavaScript to inspect the text and apply classes.
Why CSS cannot select by text
CSS is a declarative styling language, not a DOM query language. The selector specification matches on structural properties of elements:
| CSS Can Match | CSS Cannot Match |
Element type (div, p, span) | Text content of an element |
Class names (.warning) | Substring of inner text |
IDs (#header) | Text of child text nodes |
Attributes ([data-status]) | Computed text from pseudo-elements |
Pseudo-classes (:hover, :focus, :checked) | Text generated by JavaScript |
Structural position (:first-child, :nth-of-type) | Dynamic content changes |
Parent-child relationships (article > p) | Text across nested elements |
This boundary is deliberate. Text matching would force the CSS engine to parse and compare strings during layout, which is expensive and blurs the line between styling and application logic.
Encode the meaning in classes or data attributes
To style elements by their meaning, put that meaning in the markup. Classes and data attributes are the most reliable options.
Using classes
Using data attributes
When the condition maps to a value rather than a boolean category, data attributes are cleaner:
Data attributes also support partial matching:
These attribute selectors are native CSS and work in all browsers. They match attribute values, not text content.
Matching text with JavaScript
If you must style by text content and cannot change the markup (third-party widgets, CMS output, legacy systems), use JavaScript.
Vanilla JavaScript
MutationObserver for content added later
If the DOM changes after load (e.g., streaming log output), use a MutationObserver to apply classes as new elements appear:
Text inspection stays in JavaScript, and CSS handles only the visual styling.
jQuery's :contains() Is Not CSS
:contains("text") is not CSS. It was proposed in early CSS3 drafts (Selectors Level 3) but removed from the specification before browsers implemented it. It survives in two places:
jQuery and Cypress implement their own selector extensions. :contains() does not work in a stylesheet or in document.querySelector().
Why :has() does not help
The :has() selector (now supported in Chrome, Safari, and Firefox) selects based on descendant elements, not text content.
:has() is structural: it can check for child elements but not their text.
Assigning classes at render time in React and Vue
Component frameworks handle this when rendering rather than in CSS:
React
Vue
The class is assigned from component state during rendering, not by inspecting rendered text. Components decide which classes apply, and CSS decides what they look like.
Workarounds that break
The most frequent mistake is treating jQuery selectors or test framework locators as standard CSS. :contains() works in jQuery and Cypress but browsers do not support it in stylesheets or querySelector.
Attribute selectors like [title*="warning"] only stand in for text matching if the information is duplicated in an attribute, which is fragile and adds redundant data to the DOM.
Scanning and classifying text in JavaScript when you could add a class in the rendering component adds needless runtime cost. If you control rendering, add classes there; reserve text scanning for markup from external sources you cannot modify.
Text-based rules break under localization. A rule looking for "Warning" fails for users seeing "Advertencia" or "Avertissement". Classes and data attributes are language-independent.
Leaning on :has() as a replacement for text matching leads to brittle selectors. If the condition is truly text-based, use JavaScript.
Related reading
- Is there a good object mapper for Amazons dynamodbthrough aws sdk which can be used in nodejs?
- Is there a good way to Promise.all an array of objects which has a property as promise?
- Is there a JavaScript preprocessor that makes callbacks look nice?
- Is there a "null coalescing" operator in JavaScript?
- Is there a shortcut to create padded array in JavaScript?
- Is there a Thrift or Cassandra client for Node.js/JavaScript
- Is there a way to deal with values emitted for multiple observables independently, and then do stuff when all observables are complete?
- Is there a way to get the version from the 'package.json' file in Node.js code?
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.