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

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

DDD
發布: 2024-11-14 16:05:02
原創
1010 人瀏覽過

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中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板