html - 伪选择器首创不起作用

标签 html css pseudo-class

所以基本上我让 .round 失去了它的 margin-left 属性与 first-of-type 选择器。有趣的是,last-of-type 选择器工作正常。 我做错了什么?

<div id="box">
    <div id="headline">One big headline</div>
    <div class="round">Button 1</div>
    <div class="round">Button 2</div>
</div>

#headline {
display: block;
width: 90%;
margin-left: 5%;
margin-right: 5%;
top: 50%;
line-height: 1.2em;
font-size: 3.0em;
font-weight: 800;
text-align: center;
text-transform: uppercase;
color: #333;
}
.round {
display: inline-block;
background: #15D43C;
width: auto;
height: 40px;
border-radius: 20px;
padding-left: 2em;
padding-right: 2em;
text-align: center;
font-size: 0.75em;
font-weight: 700;
text-transform: uppercase;
line-height: 40px;
vertical-align: baseline;
color: #fff;
margin-left: 1em;
margin-top: 1em;
}
.round:first-of-type {
    margin-left: 5em;
}

最佳答案

:first-of-type 并不像您想象的那样工作。

.round 的类型是div。因为第一个 .round 不是它的第一个类型(它是父级中的第二个 div),所以您的选择器没有选择任何内容。

.round:nth-child(2) 适用于此特定示例

关于html - 伪选择器首创不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24855024/

相关文章:

html - 如何设置选择标签中文本内部选项之间的间距

javascript - 输入类型编号隐藏输入右侧的箭头

javascript - 使用Jquery,按下标签<a>时如何将表格传递到另一个页面?

html - 如何让我的网站在移动设备上不拖拽?

css - 如何禁用 css :not pseudo class on linked social icons

html - 如何在响应式页面的 div 中居中 ul

mat-list 的 CSS 媒体查询

javascript - 可以为开发环境禁用 Kentico 的脚本和 css 缩小吗?

javascript - 查找悬停 CSS 属性

css - E > E :nth-child(2) pattern doesn't work. 如何解决?