前端技术学习之选择器(九)

来源:互联网 发布:网络层协议 编辑:程序博客网 时间:2024/06/05 14:19

:only-child

    :only-child,一看就是选择一个元素,且该元素是其父元素唯一的子元素。

 

代码例子:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>选择器</title>

<style type="text/css">

.post p {

  background: green;

  color: #fff;

  padding: 10px;

}

.post p:only-child {

  background: orange;

}

</style>

</head>

<body>

<div class="post">

  <p>我是一个段落</p>

  <p>我是一个段落</p>

</div>

<div class="post">

  <p>我是一个段落</p>

</div>

</body>

</html>

 

运行效果:


十一:only-of-type

    :only-of-type选择器是对:only-child展,选择某种型的子元素,且子元素是其父元素中唯一一个该类 选择器。

 

代码例子:修改容器中有一个div元素的背景色橙色。

 

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>选择器</title>

<style type="text/css">

.wrapper > div:only-of-type {

  background: orange;

}

</style>

</head>

<body>

<div class="wrapper">

  <p>我是一个段落</p>

  <p>我是一个段落</p>

  <p>我是一个段落</p>

  <div>我是一个Div元素</div>

</div>

</body>

</html>

 

运行效果:


0 0
原创粉丝点击