[英]I need help with using first-child and last-child CSS selectors
I have a three column layout nested within a row. 我在行中嵌套了三列布局。 I want to add a border to the right of every column except the last one.
我想在除最后一列之外的所有列的右侧添加边框。 I also want to remove the left padding and replace it with a left margin from the first column, and remove the right padding and replace it with a right margin from the last column.
我还想删除左填充并从第一列中将其替换为左边距,并删除右填充并从最后一列中将其替换为右边距。 I tried using the
first-child
and last-child
selectors but they don't work. 我尝试使用第
first-child
和last-child
选择器,但它们不起作用。
Can anyone point me in the right direction? 谁能指出我正确的方向?
#row {
}
.box {
border-right: 1px dotted #e1e1e1;
margin: 0;
padding: 0 10px;
width: 139px;
}
#row div:first-child {
padding-left: 0;
margin-left: 10px;
}
#row div:last-child {
border-right: 0;
margin-right: 10px;
padding-right: 0;
}
<div class="row">
<div class="box">
<h3>First Title</h3>
<div>Stuff</div>
</div>
<div class="box">
<h3>Middle Title</h3>
<div>Stuff</div>
</div>
<div class="box">
<h3>Last Title</h3>
<div>Stuff</div>
</div>
</div>
it is because row
is a class not an id. 这是因为
row
是一个类,而不是id。 change your css to: 将您的CSS更改为:
.row {
}
.box {
border-right: 1px dotted #e1e1e1;
margin: 0;
padding: 0 10px;
width: 139px;
}
.row div:first-child {
padding-left: 0;
margin-left: 10px;
}
.row div:last-child {
border-right: 0;
margin-right: 10px;
padding-right: 0;
}
What about styling all the columns the same and giving the container negative margin on the right side? 将所有列样式设置为相同,并在右侧为容器提供负边距怎么办?
Demo: jsfiddle.net/Marcel/aqmjn 演示: jsfiddle.net/Marcel/aqmjn
#row div:last-child {
border-right: 0;
margin-right: 10px;
padding-right: 0;
}
This block of code is in reference to the last 此代码块参考了最后一个
<div>stuff</div>
. 。 You are referencing the children of the div you are selecting.
您引用的是所选div的子级。
What you want is 你想要的是
.row:last-child
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.