Web

CSS中常用的选择器

2019-07-10  本文已影响0人  追逐_chase
web.jpeg

1.标签选择器

  <style>
        p{
            background-color: beige;
            color: red;
            /* 文字的修饰样式 */
            text-decoration: underline;
            font-size: 30px;
        }
    </style>

<body>
    <p>这个一个段楼标签,一个选择</p>
</body>
image.png

2.类选择器

 <style>
        .demo{
         
         color: red;  
           
        }
        .big{
            font-size: 30px;
        }
        .fx1 {
           /* 文字的修饰样式 */
           text-decoration: underline;
        }

    </style>

<body>
    <p class="demo big">这个一个段楼标签,一个选择</p>
    <p class="fx1 " >这是一个测试</p>
    <p class="big fx1">哈哈 你好</p>
</body>

image.png

3.id选择器

  <style type="text/css">
        #hezi{
            background: aqua;
            width: 100px;
            height: 100px;
            
        }


    </style>

<body>
        <div id="hezi"></div>
</body>


id选择器.jpg

4. 后代选择器


 .houdai p{


            background-color: bisque;
            font-size: 50px;
            color: red;
            

        }

 <div class="houdai">
        <ul>
            <li>
                <p>后台选择器的测试</p>
            </li>
        </ul>
    </div>
后代.png

5.交集选择器

交集.png
 h3.special {
            background-color: bisque;
            color: red;
        }


    <div>
        <h3 class="special">交集选择器</h3>
        <h3 class="special">交集选择器</h3>
        <h3>交集选择器</h3>
        <h3>交集选择器</h3>
        <h3>交集选择器</h3>
    </div>

6.并集选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS之选择器</title>
    <!-- 样式-->
    <style type="text/css">

        h3,li{
            color: red;
        }

    </style>
 
   
</head>
<body>

    <div>
        <h3>这是一个标题</h3>
        <p>是一个段落</p>
        <ul>
            <li>这是个列表</li>
        </ul>
    </div>
</body>
</html>

7.通配符选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS之选择器</title>
    <!-- 样式-->
    <style type="text/css">

       *{
           color: red;
       }

    </style>
 
   
</head>
<body>

    <div>
        <h3>这是一个标题</h3>
        <p>是一个段落</p>
        <ul>
            <li>这是个列表</li>
        </ul>
    </div>



</body>
</ht

8.伪类选择器

8.1 链接伪类选择器 主要针对于 a标签(其中 link 和 visited只有在a上才有效果)

注意:伪类显示是有顺序的,必须是按照上面的顺序类 lv-ha
link - visited - hover - active

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>伪类</title>
    <style type="text/css">
        .pp{
            text-decoration: none;
            font-size: 30px;
        }

        a:link {
            color: red;

        }
        a:visited{
            color: purple;

        }

        a:hover {
            color: yellow;

        }
        a:active{
            color: green;

        }


    </style>

</head>
<body>

    <!--伪类-->

    <div>
        <a class="pp" href="#">这是不是伪类</a>
    </div>

</body>
</html>


Untitled.gif

8.2 结构伪类选择器(CSS3 有兼容性)

<html>
<head>
<style type="text/css">
p:first-child I {
  color:blue;
  } 
</style>
</head>

<body>
<p>some <i>text</i>. some <i>text</i>.</p>
<p>some <i>text</i>. some <i>text</i>.</p>
</body>
</html>

9.属性选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>颜色格式</title>
    <style>
        a[title]{
            color: red;
        }

    </style>
</head>
<body>

    <a href="#" title="baidu">百度</a>
    <a href="#" title="xinlang">新浪</a>
    <a href="#">搜狐</a>
    <a href="#">掠食龙</a>
    <a href="#">土豆</a>

    
</body>
</html>
image.png
 <style>
 
     /* 属性等于某个值 */
     input[type=text]{
         color: aqua;
     }
   
    </style>
<input type="text" value="文本输入框">
    <input type="text" value="测试框">
属性选择指定属性的值
 <style>
        /* 属性选择器  选中带有title属性的标签 */
     a[title]{
         color: red;
     }
     /* 属性等于某个值 */
     input[type=text]{
         color: aqua;
     }
     /* 表示font开头的 选择器 */
     div[class^=font]{
        color: yellowgreen;
     }
     /* 以sub结尾的 选择器 */
     div[class$=sub]{
        color: rebeccapurple;
     }
     /* 包含sub的选择器 */
     div[class*=sub]{
        color: green;
     }

    </style>

<body>
    <!-- 属性选择器 -->
    <a href="#" title="这是百度">百度</a>
    <a href="#" title="这是新浪">新浪</a>
    <a href="#">网易</a>
    <a href="#">优酷</a>
    <a href="#">土豆</a>
    <input type="text" value="文本输入框">
    <input type="text" value="测试框">

    <div class="font12">font12</div>
    <div class="font23">font23</div>
    <div class="font34">font34</div>
    <div class="aafont">aafont</div>
    <div class="aasub">aasub</div>
    <div class="bsub">bsub</div>
    <div class="csub">csub</div>
</body>
image.png

10. 伪元素选择器


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

        p::first-line{
            font-size: 17px;
            font-weight: bold;
        }

        p::selection{

            background-color: pink;
        }
    
    </style>


 <p>新华社香港11月13日电(记者丁梓懿 郭鑫 朱宇轩)国家主席习近平12日会见香港澳门各界庆祝国家改革开放40周年访问团并发表重要讲话,在港澳引起热烈反响。两地各界人士及媒体舆论普遍认为,讲话将对港澳本地未来发展和更好融入国家发展大局产生重大引领和促进作用,两地应切实发挥所长,贡献国家所需,从国家发展中获得广阔空间和源源不断的动力。</p>

image.png
<style>
    
        div {

            height: 200px;
            width: 200px;
            background-color: gray;
        }

        div::before {
            content: "开始";
            color: red;
            

        }

        div::after {
            content:"结束";
            width: 100px;
            height: 100px;

            background-color: blue;

        }
    
    </style>


<div> 伪元素 </div>
image.png

兄弟选择器

image.png
上一篇 下一篇

猜你喜欢

热点阅读