Day04-HTML5 CSS3

一.相邻兄弟选择器

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 相邻兄弟选择器  +  */
        span+div {
            color: red;
        }
    </style>
</head>

<body>

    <div class="content">
        <span>span1</span>
        <div>div1</div>
        <span>span2</span>
        <span>span3</span>
        <span>span4</span>
        <span>span5</span>
        <span>span6</span>
    </div>

</body>

</html>

二.匹配选择器

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 匹配选择器  ~  */
        /* 位于div 后面的所有span元素都会被选中 */
       .content div ~ span{
            color: red;
        }
    </style>
</head>

<body>

    <div class="content">
        <span>span1</span>
        <span>span2</span>
        <span>span3</span>
        <span>span4</span>
        <div>div1</div>
        <span>span5</span>
        <span>span6</span>
    </div>
    <div class="content2">
        <span>span1</span>
        <span>span2</span>
        <span>span3</span>
        <span>span4</span>
        <div>div1</div>
        <span>span5</span>
        <span>span6</span>
    </div>

</body>

</html>

三.属性选择器

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* [attribute]用于选取带有指定属性的元素。*/
        /* [title] {
            color: red;
        } */

        /* [attribute=value] 用于选取带有指定属性和值的元素。 */
        /* [data-index="1"]{
            color: red;
        } */

        /* [attribute^=value]匹配属性值以指定值开头的每个元素。 */
        /* [class^="box"]{
            color: red;
        } */

        /* [attribute$=value]匹配属性值以指定值开头的每个元素。 */
        /* [class$='x']{
            color: red;
        } */

        /* [attribute*=value]匹配属性值中包含指定值的每个元素。 */
        /* [class*='2'] {
            color: red;
        } */

        /* [attribute~=value]用于选取属性值中(包含)指定词汇的元素,还可以空格为分隔符。 */
        /* [class~="box2"] {
            color: red;
        } */

        /* [attribute|=value]用于选取带有以指定值(开头)的属性值的元素,还可以连字符为分隔符。 */
        /* [class|='box1']{
            font-family: "华文彩云";
        } */

        /* ::selection {
            background-color: pink;
            color: white;
        } */
    </style>
</head>

<body>

    <div class="box" data-index="1">box</div>
    <div class="box2" data-index="2">box2</div>
    <div class="box box3">box box3</div>
    <div class="box1-box2-box3" title="">box1 box2 box3</div>
    <div class="box-4box5box_4">box-4box5box_4</div>
    <div class="box6" title="">box6</div>
    <div class="box-1 box-2 box-3">box-1 box-2 box-3</div>

</body>

</html>

四.整体结构类型选择器

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 整体结构性伪类选择器 */
        /* :last-child */
        /* :first-child */

        /* 选中当前 content里面的第一个h1 */
        .content h1:first-child {
            color: springgreen;
        }

        /* 选中当前 content里面的最后一个h1 */
        /* 当h1不再是整体结构中的最后一个元素 那么此句不会生效 */
        .content h1:last-child {
            color: orange;
        }

        /* 标签结构类型 */
        /* :last-of-type */
        /* :first-of-type */
        .content2 h1:first-of-type{
            color: red;
        }
        
        .content2 h1:last-of-type{
            font-family: "华文彩云";
        }
    </style>
</head>

<body>

    <div class="content">
        <h1>张学友演唱会上突然头晕跌倒1</h1>
        <h1>张学友演唱会上突然头晕跌倒2</h1>
        <h1>张学友演唱会上突然头晕跌倒3</h1>
        <h1>张学友演唱会上突然头晕跌倒4</h1>
        <h1>张学友演唱会上突然头晕跌倒5</h1>
        <h1>张学友演唱会上突然头晕跌倒6</h1>
    </div>

    <div class="content2">
        <p>张学友演唱会上突然头晕跌倒0</p>
        <h1>张学友演唱会上突然头晕跌倒1</h1>
        <h1>张学友演唱会上突然头晕跌倒2</h1>
        <h1>张学友演唱会上突然头晕跌倒3</h1>
        <h1>张学友演唱会上突然头晕跌倒4</h1>
        <h1>张学友演唱会上突然头晕跌倒5</h1>
        <h1>张学友演唱会上突然头晕跌倒6</h1>
        <p>张学友演唱会上突然头晕跌倒7</p>
    </div>

</body>

</html>

五.指定子元素的序号

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- :nth-child(n) 对指定序号的子元素设置样式(从前往后数) -->
    <style>
        /* 2n偶数 2n+1奇数 odd奇数 even偶数 */
        .content p:nth-child(4n) {
            font-family: "华文彩云";
        }

        /* .content p:nth-child(2n+1) {
            color: red;
        } */


        /* :nth-last-child(n) 对指定序号的子元素设置样式(从后往前数)。参数同上。 */
        /* .content h3:nth-last-child(2n){
            color: aqua;
        } */
        /* .content h3:nth-last-child(4n){
            color: aqua;
        } */


        /* :nth-of-type(n) 匹配指定序号的同一种类型的子元素(从前往后数)。参数同上。 */
        /* .content h3:nth-of-type(4n){
            color: aqua;
        } */

        /* :nth-last-of-type(n) 匹配 指定序号的同一种类型的子元素(从后往前数)。参数同上。 */
        .content h3:nth-last-of-type(4n){
            color: aqua;
        }
</style>

</head>

<body>

    <div class="content">
        <!-- <p>中秋国庆连休8天1</p>
        <p>中秋国庆连休8天2</p>
        <p>中秋国庆连休8天3</p>
        <p>中秋国庆连休8天4</p>
        <p>中秋国庆连休8天5</p>
        <p>中秋国庆连休8天6</p> -->
        <h3>中秋国庆连休8天1</h3>
        <h3>中秋国庆连休8天2</h3>
        <h3>中秋国庆连休8天3</h3>
        <h3>中秋国庆连休8天4</h3>
        <h3>中秋国庆连休8天5</h3>
        <h3>中秋国庆连休8天6</h3>
        <strong>丈夫割草归来采一大把野花送妻子1</strong>
        <strong>丈夫割草归来采一大把野花送妻子2</strong>
        <strong>丈夫割草归来采一大把野花送妻子3</strong>
        <strong>丈夫割草归来采一大把野花送妻子4</strong>
        <strong>丈夫割草归来采一大把野花送妻子5</strong>
        <strong>丈夫割草归来采一大把野花送妻子6</strong>
    </div>

</body>

</html>

六.其他伪类选择器

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /*  - :root:将样式应用到页面的根元素中。root选择器就是将样式应用到根元素中。在整个HTML页面中,指的就是整个“html”部分。
            - :empty 指定当元素内容为空白时使用的样式。
            - :not(selector)排除selector选择器选中的元素,不对它们应用样式解析。
            - :target 对页面中某个target元素指定样式,该样式只在用户单击了页面中的链接,并且跳转到target元素后生效。 
        */
        /* :root{
            color: purple;
        } */

        /* :empty {
            width: 200px;
            height: 200px;
            background-color: pink;
        } */

        h3:not(.item) {
            color: orange;
        }

        .detail {
            width: 1200px;
            height: 500px;
            margin: 20px auto 0px;
            background-color: skyblue;
        }

        :target {
            color: springgreen;
        }
    </style>
</head>

<body>

    <a href="#like">点击定位到猜你喜欢</a>
    <a href="#goodproduct">点击定位到品质好货</a>
    <div class="content">
        <p></p>
        <p>中秋国庆连休8天2</p>
        <p>中秋国庆连休8天3</p>
        <p>中秋国庆连休8天4</p>
        <p>中秋国庆连休8天5</p>
        <p>中秋国庆连休8天6</p>
        <h3></h3>
        <h3>中秋国庆连休8天2</h3>
        <h3>中秋国庆连休8天3</h3>
        <h3>中秋国庆连休8天4</h3>
        <h3>中秋国庆连休8天5</h3>
        <h3>中秋国庆连休8天6</h3>
        <strong></strong>
        <strong>丈夫割草归来采一大把野花送妻子2</strong>
        <strong>丈夫割草归来采一大把野花送妻子3</strong>
        <strong>丈夫割草归来采一大把野花送妻子4</strong>
        <strong>丈夫割草归来采一大把野花送妻子5</strong>
        <strong>丈夫割草归来采一大把野花送妻子6</strong>
    </div>
    <div class="detail"></div>
    <div class="detail"></div>
    <h1 id="goodproduct">品质好货</h1>
    <div class="detail"></div>
    <div class="detail"></div>
    <h1 id="like">猜你喜欢</h1>
    <div class="detail"></div>
    <div class="detail"></div>

    <script>

        var h3 = document.getElementsByTagName("h3");

        function siblings(ele) {
            for (var i = 0; i < ele.length; i++) {
                ele[i].className = ""
            }
        }
        for (var i = 0; i < h3.length; i++) {
            h3[i].onclick = function () {
                siblings(h3)
                this.className = "item";
            }
        }

    </script>

</body>

</html>

七.表单状态伪类选择器

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /*  - :disabled 指定当前元素处于不可用状态时的样式。
            - :enabled 指定当前元素处于可用状态时的样式。
            - :checked 指定表单中单选框或复选框处于选中状态时的样式。 */
        :disabled{
            width: 20px;
            height: 20px;
        }
        [type="email"]:enabled{
            width: 200px;
            height: 30px;
        }

        :checked{
            width: 20px;
            height: 20px;
        }
    </style>
</head>

<body>

    <form>
        <div>
            邮箱: <input type="email" disabled>
        </div>
        <div>
            手机号: <input type="tel">
        </div>
        <div>
            性别:
            男<input type="radio" name="gender">
            女<input type="radio" name="gender">
        </div>
        <div>
            爱好 :
            <input type="checkbox">敲代码
            <input type="checkbox">听音乐
            <input type="checkbox">看电影
            <input type="checkbox">美食
        </div>
    </form>

</body>

</html>

八.内容选中伪类

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> 
        .content{
            width: 800px;
            height: 800px;
            border: 1px solid red;
            margin: 100px auto;
        }
        ::selection{
            color: purple;
            background-color: aqua;
        }
    </style>
</head>

<body>

    <div class="content">
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111122222222222222222222222222222222222222222222222222354444444444444488888888888888888888888888888888888888888888888888666666666666
    </div>

</body>

</html>

九.内容追加伪元素

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /*  - ::before:向当前的元素前面追加内容(创建一个伪元素,其将成为匹配选中的元素的第一个子元素)
            - ::after:向当前的元素内容后面追加内容(创建一个伪元素,作为已选中元素的最后一个子元素) */
            /* 
            :before是css2的写法
            ::before是css3的写法
            */
        .content:before {
            content: "这是追加到前面的文字";
            position: absolute;
            display: block;
            width: 200px;
            height: 200px;
            background-color: pink;
        }

        .content:after{
            content: "这是追加到后面的文字";
            background-color: purple;
        }
    </style>
</head>

<body>

    <div class="content">
        原本内容
    </div>

</body>

</html>

十.JS新增选择器

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <div class="box">
        <div class="box2"></div>
        <input type="radio">
    </div>
    <div class="box2">
        <h3>12345678</h3>
        <h3>12345678</h3>
        <h3>12345678</h3>
        <h3>12345678</h3>
        <h3>12345678</h3>
        <h3>12345678</h3>
    </div>

    <script>

        // 根据id获取元素
        // getElementById(id);
        // 根据类名获取元素
        // getElementsByClassName(classname)[0];
        // 根据标签名获取元素
        // getElementsByTagName(tagname);

        // JS新增一个选择器
        // 获取一个元素
        // document.querySelector('selector');
        // 获取多个元素
        // document.querySelectorAll()
        var box = document.querySelector('.box');
        console.log(box);

        var input = document.querySelector('[type="radio"]');
        console.log(input);

        var eleh3 = document.querySelectorAll('h3');
        console.log(eleh3);

    </script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值