CSS选择器

id选择器

语法格式:#S{…},其中S为选择器名,id为name的标签,会匹配

<style>
#name{
	color:red;  
}
</style>
<p id="name">red text</p>  <!--红色-->

类选择器

语法格式:.S{…},使用.标识,class属性为value的标签,会匹配

<style>
.value{
	text-align:center;
}
</style>
<p class="value">text center</p>  <!--居中-->

元素选择器(标签选择器)

语法格式:S{…},所有P标签都会匹配以下格式

<style>
p{
	font-style:italic;
}
<p style="font-style:italic">itatic text</p>  <!--斜体-->
</style>

包含选择器

指定目标选择器必须处于某个选择器对应的内部元素,语法格式:A B{…} 或 .A B{…}

<style>
p{
	color:red;
}
div p{
	color:yellow;
}
</style>
<p>red text</p>  <!--红色-->
<div>
	<p>yellow text</p>  <!--黄色-->
</div>
<style>
.first span{
  color:red;
}
</style>
<body>
<p class="first"><span>内容为红色</span>
<ol>
  <li><span>内容也是红色</span></li>
  <li><span>内容也是红色</span></li>
</ol></p>

</body>

子选择器

语法格式:A>B{…}

<style>
div>p{
  color:red;
}
</style>
<div>
  <p>red text</p><!--文字是红色的-->
  <table>
    <tr>
      <td>
        <p>no red text;</p><!--文字是非红色的-->
      </td>
    </tr>
  </table>
</div>

兄弟选择器

语法格式:A~B{…},当A匹配A,B匹配B时,显示B

<style>
div~p{
  color:red;
}
</style>
<div>
  <p>no red text</p><!--文字是非红色的-->
  <div>no red text</div>
  <p>red text</p><!--文字是红色的,显示P,不显示div-->
</div>

相邻选择器

语法格式:A+B{…}

<!--相邻选择器选择每个div紧邻后的一个元素p-->
<style>
   div+p{
     color: red;
   }
</style>
<div>
   <p>not red text</p>
   <p>not red text</p>
</div>
<p>red text</p>  // 就这一个是挨着的
<p>not red text</p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值