css 伪类:last-child等不起作用
【前言】
今天学生练习项目遇到不少问题,这里列举一个:css 伪类:last-child等不起作用?
【详解】
首先我再强调下,伪类选择器`:first-child`和`:last-child`是根据父级进行筛选的。所以如果想对某个尾部元素设置样式,必须在之前加上父级元素。即子元素 父级元素:last-child 子元素。
<style>
#icons{
border: 1px solid bisque;
height: 250px;
}
#icons .column{//每一列
width: 25%;
display: inline-block;
float: left;
text-align: center;
}
#icons .column .txt{//每列文字
width: 100%;
height: 52px;
margin-top: 134px;
border-right: 2px solid #787b83;//在此处加隔条
}
#icons .column .txt:last-child{
border-right: none;
}
#icons .column .txt p{
width: 160px;
height: 52px;
font-size: 14px;
line-height: 18px;
color: #767777;
margin-left: 25%;
}<div id="icons">
<div class="column column1">
<div class="txt">
<p>打造全新世界观,让你更爱你的生活</p>
</div>
</div>
<div class="column column2">
<div class="txt">
<p>丰富多彩的公益活动,发挥新世界的主人公意识</p>
</div>
</div>
<div class="column column3">
<div class="txt">
<p>时尚的新理念,超前体验未知的生活</p>
</div>
</div>
<div class="column column4">
<div class="txt">
<p>完善的培养机制,培养你全新的世界观</p>
</div>
</div>
</div>
</div>效果如下:

学生一般以为在 `#icons .column .txt` 处加隔条后,想把最后一个竖隔条设为 `border:none;` 没想到如上如所示,全部都不见了。
原因在于,伪类选择器`:first-child`和`:last-child`是根据父级进行筛选的,`#icons .column .txt:last-child` 的父级是‘.txt`,在该处上的子级只有一个,最后的子级当然也是自己本身,所以要想达到效果,就应该在‘.txt`的父级添加伪类。
#icons .column:last-child .txt{
border-right: none;
}效果如下:

.
相关推荐
startXUEBA 2018-03-24
Chenliaoyuan 2020-06-11
moyigg 2020-04-20
Chenliaoyuan 2020-04-14
Donven 2019-09-07
88971730 2011-08-18
jackiebobo 2011-04-08
ccgsteel 2019-06-30
<div class="panel-body" style="height: 300px; width: 100%; overflow: auto;" data-platform-scroll="&q
道北吴彦祖 2017-01-15
乔乔 2016-07-20
goodyatou 2016-03-27
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="icons/apple-touch-icon-114x114-precom
道北吴彦祖 2015-09-29
Pig00 2019-06-27
johncena 2014-12-05