03-CSS选择器

2018-10-28  本文已影响0人  喝酸奶要舔盖__

标签选择器

 <style>
        p{
            color: red;
        }
 </style>
<p>我是段落1</p>
<p>我是段落2</p>
<p>我是段落3</p>
<p>我是段落4</p>

id选择器

<style>
        #identity1{
            color: red;
        }
        #identity2{
            color: green;
        }

        #identity3{
            color: yellow;
        }

        #identity4{
            color: blue;
        }
</style>
<p id="identity1">迟到毁一生</p>
<p id="identity2">早退穷三代</p>
<p id="identity3">按时上下班</p>
<p id="identity4">必成高富帅</p>

类选择器

<style>
        .c1{
            font-size: 50px;
        }
        .c2{
            color: blue;
        }
 </style>
<p class="c1">我是段落1</p>
<p class="c2 c1">我是段落2</p>

id选择器和类选择器区别


后代选择器

<style>
        #identity p{
            color: red;
        }
</style>
<p>我是段落</p>
<div id="identity" class="para">
    <p>我是段落</p>
    <p>我是段落</p>
    <ul>
        <li>
            <!--<p id="iii" class="ccc">我是段落</p>-->
            <p>我是段落</p>
        </li>
        <li>
            <p>我是段落</p>
        </li>
    </ul>
</div>

子元素选择器

<style>
        div>ul>li>p{
            color: red;
        }
 </style>
<div id="identity">
    <p>我是段落</p>
    <p>我是段落</p>
    <ul>
        <li><p>我是段落</p></li>
    </ul>
</div>

后代选择器和子元素选择器区别


交集选择器

<style>
        p.para1{
            font-size: 50px;
        }
</style>
<p>我是段落</p>
<p>我是段落</p>
<p class="para1" id="identity">我是段落</p>
<p class="para1">我是段落</p>
<p>我是段落</p>

并集选择器

<style>
        .ht,.para{
            color: blue;
        }
</style>
<h1 class="ht">我是标题</h1>
<p class="para">我是段落</p>

兄弟选择器

相邻兄弟选择器
格式:
选择器1+选择器2{
    属性:值;
}
通用兄弟选择器
格式:
选择器1~选择器2{
    属性:值;
}
<style>
        /*相邻兄弟选择器*/
        h1+p{
            color: yellow;
        }
      /*通用兄弟选择器*/
        h1~p{
            color: green;
        }
</style>
<h1>我是标题</h1>
<a href="#">我是超链接</a>
<a href="#">我是超链接</a>
<a href="#">我是超链接</a>
<p>我是段落</p>
<p>我是段落</p>
<p>我是段落</p>
<h1>我是标题</h1>
<p>我是段落</p>
<p>我是段落</p>
<p>我是段落</p>

序选择器

<style>
        p:first-child{
            color: red;
        }

        p:last-child{
            color: blue;
        }

        p:nth-child(2){
            color: yellow;
        }

        p:nth-last-child(3){
            color: green;
        }
</style>
<h1>我是标题</h1>
<p>我是段落1</p>
<p>我是段落2</p>
<p>我是段落3</p>
<p>我是段落4</p>
<div>
    <p>我是段落5</p>
    <p>我是段落6</p>
    <p>我是段落7</p>
    <p>我是段落8</p>
</div>
<style>
        p:first-of-type{
            color: red;
        }

        p:last-of-type{
            color: green;
        }
        p:nth-of-type(2){
            color: red;
        }
</style>
<h1>我是标题</h1>
<p>我是段落1</p>
<p>我是段落2</p>
<a href="#">我是超链接</a>
<div>
    <h1>我是标题</h1>
    <p class="para">我是段落3</p>
    <p class="para">我是段落4</p>
</div>
<style>
        p:nth-child(odd){
            color: red;
        }
        p:nth-child(even){
            color: green;
        }

        p:nth-child(2n+0){
            color: red;
        }
</style>
<p>我是段落</p>
<p>我是段落</p>
<p>我是段落</p>
<p>我是段落</p>
<p>我是段落</p>

属性选择器

格式:
[attribute]
作用:根据指定的属性名称找到对应的标签, 然后设置属性
[attribute=value]
作用: 找到有指定属性, 并且属性的取值等于value的标签, 然后设置属性
最常见的应用场景, 就是用于区分input属性
input[type=password]{}
<input type="text" name="" id="">
<input type="password" name="" id="">
<style>
        /*p[class]{
            color: red;
        }*/

        p[class=para]{
            font-size: 50px;
        }
</style>
<p id="identity1">我是段落1</p>
<p id="identity2" class="cc">我是段落2</p>
<p class="cc">我是段落3</p>
<p id="identity3" class="para">我是段落4</p>
<p>我是段落5</p>

通配符选择器

<style>
        *{
            color: red;
        }
</style>
<h1>我是标题</h1>
<a href="javascript:">我是超链接</a>
<p>我是段落</p>
上一篇 下一篇

猜你喜欢

热点阅读