英文:
Apply CSS except for child with specific ID
问题
考虑以下标记:
<ul>
<li class="search-results">
<p>请给我样式</p>
<span id="not-me-please">请不要给我样式</span>
</li>
和CSS:
ul li.search-results {color:green}
显然,段落和 span 都会有绿色文本,但有没有办法使用 :not 选择器来确保 span 不会获得样式?
英文:
Consider the following mark up:
<ul>
<li class="search-results">
<p>Style me please</p>
<span id="not-me-please">Do not style me</span>
</li>
</ul>
And the CSS:
ul li.search-results {color:green}
Obviously both the paragraph and the span will have green text but is there a way to use the :not selector so that the span doesn't get the styling?
答案1
得分: 1
You can use the :not pseudo-class and exclude the #not-me-please
element as below, but I would not recommend this approach as it can be considered bad practice.
<ul>
<li class="search-results">
<p>Style me please</p>
<span id="not-me-please">Do not style me</span>
</li>
</ul>
ul li.search-results > *:not(#not-me-please) {color:green}
Recommendation:
Either add a specific class to the elements intended to be green (i.e., <p class="search-results_label">
) or target the p
tags directly (i.e., ul li.search-results > p
).
Note that I would not advise targeting the global ul tag directly either.
英文:
You can use the :not pseudo-class and exclude the #not-me-please
element as below, but I would not recommend this approach as it can be considered bad practice.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<ul>
<li class="search-results">
<p>Style me please</p>
<span id="not-me-please">Do not style me</span>
</li>
</ul>
<!-- language: lang-css -->
ul li.search-results > *:not(#not-me-please) {color:green}
<!-- end snippet -->
Recommendation
Either add a specific class to the elements intended to be green (i.e., <p class="search-results_label">
) or target the p
tags directly (i.e., ul li.search-results > p
).
> Note that I would not advise targeting the global ul tag directly either.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论