最近复习基础,啃啃CSS权威指南,发现选择器相关知识点有些忽视和遗忘了,这里记录一下
1、伪类
:first-child,和 :first-of-type,
:last-child,和 :last-of-type
p:first-child
只选择是p标签且同层级中p为第一个时,才设置
p:first-of-type
选择同层级中所有p标签的第一个
last-child,last-of-type同理
child,孩子,所以先选出同层级的第几个,再判断是不是那个种类;of-type,种类,所以是同层级中同类型的第几个,其他的无视。
<style type="text/css">
p:first-child{
color: red;
}
p:first-of-type{
font-size: 30px;
}
p:last-child{
color: green;
}
p:last-of-type{
font-size: 5px;
}
</style>
</head>
<body>
<div>第一层级的div</div>
<p>第一层级的p1</p>
<p>第一层级的p2</p>
<div>
<p>第二层级的p1</p>
<p>第二层级的p2</p>
</div>
</body>
:nth-child,和 :nth-of-type
同上理解:
:nth-last-child(); 和 :nth-last-of-type()
同上理解
:only-child
同上理解