+发表新主题
查看: 1673|回复: 0

[HTML5开发] css高级选择器中兄弟选择器

[复制链接]

[HTML5开发] css高级选择器中兄弟选择器

[复制链接]
下关砣 发表于 2015-11-18 22:10:38 浏览:  1673 回复:  0 [显示全部楼层] |只看大图 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
今天学习css高级部分,各类复杂选择器之兄弟选择器
.兄弟选择器:

     用于指定元素的相邻兄弟元素

         1.相邻兄弟选择器

                    1.匹配相邻的元素

                    2.匹配当前元素【后面】的【一个】元素,前提是两者必须拥有相同父元素

                   语法:

                               通过+作为结合体

                                    egdiv+p{

                                          属性:值;

                                                 }


                 


  • <!DOCTYPE html>
  •                 <head>
  •                <title> 兄弟选择器 </title>
  •                <meta charset="utf-8" />
  •                <style>
  •                   div+p{
  •         color:red;
  •         font-weight:900;
  •       }
  •                </style>
  •                </head>
  •                <body>
  •                <div id="d1">this is the first div</div>
  •               <p>this is the first p</p>
  •               <p>this is the second p</p>
  •               </body>              </html>

复制代码
220629xh5fc1zdimtatain.png
   

                     ****注意:只找与当前元素相关的后面一个紧挨着他的兄弟元素,如果符合则匹配,不符合也不会向下查找



        2.通用兄弟选择器:

                          匹配当前元素【后面】的【所有兄弟】元素

                          语法:

                             选择器1~选择器2

                             使用"~"作为结合符 div~p div.first~span.second

                  ****注意:第二个元素不必紧紧跟着第一个元素后面



  • <!DOCTYPE html>
  • <head>
  •   <title> 通用选择器 </title>
  •   <meta charset="utf-8" />
  •   <style>
  •    div~p{
  •          color:red;
  •    }
  •    </style>
  • </head>

  • <body>
  •   <div id="d1">this is the first div</div>
  •   <p>this is the first p</p>
  •   <p>this is the second p</p>
  •   <p>this is three p</p>
  • </body></html>  

复制代码
220629xh5fc1zdimtatain.png
  

        练习,要求:

        1.通过指定元素修改兄弟元素字体颜色为红色

                  2.通过指定元素修改通用元素背景颜色为#ccc


  • <!DOCTYPE html>
  • <head>
  •   <title> 通用选择器和相邻选择器 </title>
  •   <meta charset="utf-8" />
  •   <style>
  •     div+p{
  •       color:red;
  • }
  • div~p{
  •      background-color:#ccc;
  • }
  •    </style>
  • </head>

  • <body>
  •    <!------通用选择器与兄弟选择器----------->
  •    <P>段落1</p>
  •    <div>指定元素</div>
  •    <P>段落3</p>
  •    <P>段落4</p>
  •   </body>  </html>

复制代码
220629xh5fc1zdimtatain.png
回复

使用道具 举报


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版| 赣南网

© 2013-2016 Comsenz Inc. Powered by Discuz! X3.4

用微信扫一扫

赣南网