- Is There a CSS Selector for Elements Containing Certain Text?
- Method 1: Selecting Element Containing Certain Text Using CSS Attribute Selector
- Example
- Style “items-div” class
- Style “h2” Element
- Select “book” text
- Method 2: Selecting Element Containing Certain Text Using CSS General Sibling Selector
- Example
- Conclusion
- About the author
- Nadia Bano
- Is there any selector for elements containing certain text in CSS?
- Syntax
- Example 1
- Using the [attribute=value] CSS selector to target an element based on its attribute value
- Example 2
- Using the [attribute~=value] CSS selector to target an element based on its attribute value
- Example 3
- Using the [attribute*=value] CSS selector to target an element based on its attribute value
- Example 4
- Using the [attribute*=value] CSS selector to target an element based on its attribute value
- How to select HTML elements by text using CSS Selectors?
Is There a CSS Selector for Elements Containing Certain Text?
The CSS selectors select the HTML elements to apply styling properties. These are utilized to find the elements which you want to style. There are certain categories of CSS selectors, such as pseudo-class, simple selectors, and others. More specifically, the attribute selector is the best option for selecting the element having definite text. The combinator selector can also be applied for this purpose.
This article will demonstrate the following methods:
Method 1: Selecting Element Containing Certain Text Using CSS Attribute Selector
The CSS attribute selector “CSS [attribute~=“value”] Selector” can help to select the element with definite text.
Let’s understand the syntax of the CSS attribute selector:
- “attribute-name”: The attribute used in the HTML element having certain text is specified here.
- “~”: This symbol is a general sibling combinator. It is utilized to choose every element that is a given element’s next sibling.
- “value”: It indicates the attribute value.
Example
In the HTML file, follow the listed instructions:
- First, add the “ ” element and assign a class “items-div”.
- Add the “ ”, “ ”, and “ ” elements within the div.
- The “” and “” adjust the headings on the page. The “” heading is slightly smaller than the “” element.
- The “
” element specifies the text content on the page:
Let’s style the div “items-div” and the heading “” by applying the CSS properties as mentioned below. After that, we will see how to select the “
” element’s text using the CSS attribute selectors.
Style “items-div” class
Following CSS properties are applied to the “items-div” class:
- “text-align” determines the text alignment.
- “font-size” adjusts the element font size.
- “font-family” picks out the font style of the element.
Style “h2” Element
“h2” is also known as the element selector. It is used to style the “” element with the following properties:
- “letter-spacing” adjusts the spacing between the element’s characters.
- “color” assigns the color to the text.
- “font-size” property is defined in the above section.
Select “book” text
To select a particular text from the document, there is no specific selector. The element’s attribute only needs to be specified. Then, with the help of the attribute value, we can select the text in CSS:
Here, in CSS, the “background-color” property is adjusted to the text by specifying the attribute “stationary” having value “book”.
The below output shows that all the elements having the attribute value “book” are styled:
Now, let’s check the example to apply styles to the “pencil” text. For this purpose, the attribute stationary with the value “pencil” is fetched in CSS through the attribute selector:
background-color : rgb ( 240 , 168 , 234 ) ;
- “background-color” specified the color of the element’s background.
- “padding” assigns space around the element’s content.
Method 2: Selecting Element Containing Certain Text Using CSS General Sibling Selector
There is another method to apply styles on the elements having text. But this method should be utilized when you have multiple “ ” elements having some text, and you need to style them all at once. To do so, specify the element that is just before the “
” elements, then add the general sibling combinator (~) symbol, and mention the element having text to apply styles.
Here, is the syntax of the above-mentioned scenario:
Example
Let’s add styling to all “ ” elements that are direct siblings of the preceded element.
The “h4~p” signifies the “ ” elements that are direct siblings of the “” element. The following described properties are added to it:
- The “font-weight” property designates the thickness of the element’s font.
- The “color” property fixes the font’s color.
Using these ways, you can select elements having certain text to style in CSS.
Conclusion
To select the elements containing certain text, the CSS “attribute selectors” and “general sibling combinator selector” can be used. The attribute selectors specify the attribute name of the element and its value. Then, declare the CSS properties for it. The general sibling combinator selector selects every element that is followed by the previous selector. This article has demonstrated the ways to select elements in CSS having certain text.
About the author
Nadia Bano
I am an enthusiastic and forward-looking software engineer with an aim to explore the global software industry. I love to write articles on advanced-level designing, coding, and testing.
Is there any selector for elements containing certain text in CSS?
In CSS, selectors are used to select an HTML element to style. There are various CSS selectors available in CSS, such as class selector, id selector, name selector, etc.
Sometimes, developers need to select an HTML element based on the custom attribute value instead of selecting it based on the class name or id. We can use the attribute-value CSS selector to select the HTML element based on the particular attribute value. We can also add the custom attribute instead of only pre-defined attributes in HTML.
Syntax
Users can follow the syntax below to use the attribute-value CSS selector to select elements containing certain text.
[attribute name="attribute value"] < /* CSS code */ >
In the above syntax, the attribute name can be the name of the attribute that HTML element contains, and you want to select an element based on attribute value. The attribute value is a value of the particular attribute.
Example 1
We have created the ‘social’ div element in the example below. In the div element, we have added the child div elements containing the ‘app_name’ attribute and its value. In CSS, we used the ‘app_name’ as an attribute name and different values to select different div elements.
In the output, users can observe that it has styled the div elements containing the ‘Facebook’ and ‘Instagram’ attribute value.
[app_name="facebook"] < background-color: #3b5998; color: white; >[app_name="instagram"]Using the [attribute=value] CSS selector to target an element based on its attribute value
youtube
Example 2
In the example below, we have used the attribute-value selector, but we have used the ‘~’ sign for the partial selector. It selects the HTML element, even if the attribute contains a particular word in a value, rather than matching the whole word in the first example.
We used the ‘app’ word in the attribute-value CSS selector here. So, it will select all HTML elements whose type attribute contains the ‘app’ word. The output shows that it has styled the first three div elements as it contains the app word.
[type~="app"]Using the [attribute~=value] CSS selector to target an element based on its attribute value
Web applicationMobile applicationDesktop applicationAnroid device
Example 3
We used the ‘^’ sign with the attribute-value CSS selector in the example below. It allows the selection of all elements whose attribute value starts with a particular word.
Here, we have created the div elements with different class names. In CSS, we use the attribute-value selector to select all HTML elements whose class name starts with the ‘a’ character.
[class ^="a"]Using the [attribute*=value] CSS selector to target an element based on its attribute value
ABCADEBCDCDE
Example 4
In the example below, we created the table containing the snack’s name, expiry date, and price. Also, we added a status equal to expired for the expired snacks. We selected the ‘expired’ snacks in CSS and styled them using the attribute-value CSS selector.
td[status*=expired]Using the [attribute*=value] CSS selector to target an element based on its attribute value
Snack | Expiry | Price |
---|---|---|
Chips | 01/01/2024 | 20 INR |
Chocolate | 01/09/2023 | 30 INR |
Candy | 01/01/2022 | 25 INR |
Users learned to select an HTML element based on a certain text. We can add custom attributes to the HTML element, and after that, we can use the attribute-value CSS selector to select elements based on the attribute value.
How to select HTML elements by text using CSS Selectors?
There used to be a way to select HTML elements by text using CSS Selectors by making use of :contains(text) . However, this has been deprecated for a long time and is no longer supported by the W3C standard. If you want to select an element by text, you should look into other options. Most Python libraries provide a way for you to do so.
For instance, you can select an element by text using XPath Selectors in Selenium like this:
from selenium import webdriver from selenium.webdriver.common.by import By DRIVER_PATH = '/path/to/chromedriver' driver = webdriver.Chrome(executable_path=DRIVER_PATH) # Open Scrapingbee's website driver.get("https://www.scrapingbee.com") # Match the h1 tag that contains the word "Tired" h1 = driver.find_element(By.XPATH, "//h1[contains(text(), 'Tired')]")
Alternatively, if you are using BeautifulSoup then you can do something like this:
import re import requests from bs4 import BeautifulSoup # Open Scrapingbee's website html = requests.get("https://scrapingbee.com").text soup = BeautifulSoup(html) # Match the h1 tag that contains the word "Tired" h1 = soup.find("h1", text=re.compile("Tired")) print(h1) # Output: