Home >Web Front-end >CSS Tutorial >Can CSS Select Elements with Class Names Starting with a Specific String?

Can CSS Select Elements with Class Names Starting with a Specific String?

DDD
DDDOriginal
2024-11-14 16:05:021011browse

Can CSS Select Elements with Class Names Starting with a Specific String?

Can CSS Match Elements with Class Names Starting with a Specific String

Question:

In CSS, is there a way to use a "wildcard" to select elements with class names that start with a particular string?

Answer:

Yes, it is possible to match all elements whose class names begin with a specific string using the following CSS selectors:

  • [class^='string']: Matches elements whose class attribute starts with 'string'.
  • [class*=' string']: Matches elements whose class attribute contains 'string' anywhere.

Example:

To select and style all

elements with class names starting with "myclass":

div[class^='myclass'"], div[class*=' myclass'] {
    color: #f00;
}

In the provided HTML code:

<div class="myclass-one"></div>
<div class="myclass-two"></div>
<div class="myclass-three"></div>

The provided selectors will ensure that all three

elements have their text color set to red.

The above is the detailed content of Can CSS Select Elements with Class Names Starting with a Specific String?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn