首頁 >web前端 >css教學 >CSS 可以選擇類別名稱以特定字串開頭的元素嗎?

CSS 可以選擇類別名稱以特定字串開頭的元素嗎?

DDD
DDD原創
2024-11-14 16:05:021052瀏覽

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.

以上是CSS 可以選擇類別名稱以特定字串開頭的元素嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn