Style every third element?(每隔三个元素设置样式?)
问题描述
如何使用纯 CSS 设置每三个元素的样式.
How can I style every third element with plain CSS.
我见过这样的问题,但它们涉及到 javascript.我想要一个简单的 CSS 解决方案.我知道我可以将 even 或 odd 与 :nth-child 一起使用,但 :nth-child(third) 不能:nth-child(3)
I have seen questions like this, but they involve javascript. I want a plain CSS solution. I know I can use even or odd with :nth-child but :nth-child(third) doesn't work neither does :nth-child(3)
例子:
<section>
<div class="someclass"></div> STYLE
<div id="someId"></div> DON'T STYLE
<div class="someotherclass"></div> DON'T STYLE
<div id="someotherId"></div> STYLE
<div class="someclass"></div> DON'T STYLE
<div id="someId"></div> DON'T STYLE
<div class="someotherclass"></div> STYLE
</section>
这可以通过 CSS 实现吗?没有 javascript 或 jQuery?
Is this possible with CSS - no javascript or jQuery?
推荐答案
纯 CSS 可以做到这一点.无需 javascript.
This is possible with pure CSS. No javascript needed.
使用 nth-child 选择器.1
section > div:nth-child(3n+1) {
background:red;
}
其中1"是样式的开始位置,3 表示应该每隔三个元素设置样式.这里解释得更好.
Where '1' is where the style starts, and 3 indicates that it should style every third element. It is explained much better here.
jsFiddle 演示在这里.
1注意 - 正如其他人所提到的,这是 不完全支持 IE8 及以下版本.
这篇关于每隔三个元素设置样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:每隔三个元素设置样式?
基础教程推荐
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
