是否可以将 CSS 应用于名称包含特定字符串的元素?

chi*_*apa 2 html css

我知道我可以根据元素的名称将 CSS 样式应用到该元素:

input[name=description] {    
    width: 200px;
}
Run Code Online (Sandbox Code Playgroud)

但是是否可以将 CSS 样式应用到名称中包含特定字符串的元素呢?

想象一下几个输入:

<input type="text" name="projectDescription"/>
<input type="text" name="themeDescription"/>
<input type="text" name="methodologyDescription"/>
<input type="text" name="somethingElse"/>
etc...
Run Code Online (Sandbox Code Playgroud)

因此,我只想将样式应用于名称包含单词“Description”的输入。这可以在不编写脚本的情况下完成吗?

Lin*_*TED 5

是的你可以:

\n\n
input[name*="Description"] {\n    width: 200px;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

演示版

\n\n

更多信息

\n\n
[attr] \n    Represents an element with an attribute name of attr.\n[attr=value]\n    Represents an element with an attribute name of attr \n    and whose value is exactly "value".\n[attr~=value]\n    Represents an element with an attribute name of attr \n    whose value is a whitespace-separated list of words, \n    one of which is exactly "value".\n[attr|=value]\n    Represents an element with an attribute name of attr. \n    Its value can be exactly \xe2\x80\x9cvalue\xe2\x80\x9d or can begin with \xe2\x80\x9cvalue\xe2\x80\x9d\n    immediately followed by \xe2\x80\x9c-\xe2\x80\x9d (U+002D). It can be used for \n    language subcode matches.\n[attr^=value]\n    Represents an element with an attribute name of attr \n    and whose value is prefixed by "value".\n[attr$=value]\n    Represents an element with an attribute name of attr \n    and whose value is suffixed by "value".\n[attr*=value]\n    Represents an element with an attribute name of attr \n    and whose value contains at least one occurrence of \n    string "value" as substring.\n
Run Code Online (Sandbox Code Playgroud)\n\n

来源

\n