CSS学习06之属性选择器

回顾

结构伪类选择器有很多种伪类,列举常用的几种方式:

  1. first-child 伪类
  2. hover悬停伪类
  3. nth-of-type() 伪类
  4. nth-child()伪类

属性选择器(重要)

为带有属性的 HTML 元素设置样。
顾名思义可以设置带有特定属性或属性值的 HTML 元素的样式。

使用方式

属性选择器:元素类型[属性名=属性值(正则)]

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>属性选择器</title>
    <style>
        .demo a{
            float: left;
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px;
            background: blue;
            text-align: center;
            text-decoration: none;
            color: red;
            margin-right: 10px;
            font: bold 50px/50px Arial;
            /*line-height: 50px; 行高*/
        }
        /*存在id属性的元素*/
        a[id]{
            background: yellow;
        }
        /*id为second的元素*/
        a[id=second]{
            background: deeppink;
        }
        /*class中有links的元素
        =是绝对等于
        *=是包含这个元素
        */
        a[class*=links]{
            background: aqua;
        }
        /*class绝对等于item*/
        a[class=item]{
            background: darkgreen;
        }
        /*选中href中以http开头的元素*/
        a[href^=https]{
            background: black;
        }
        /*选中href中以last结尾的元素*/
        a[href$=last]{
            background: tomato;
        }
    </style>
</head>
<body>

<p class="demo">
    <a href="http://www.baidu.com" class="item first" id="first">1</a>&emsp;&emsp;
    <a href="" class="item active" target="_blank" title="test" id="second">2</a>&emsp;&emsp;
    <a href="" class="links item">3</a>&emsp;&emsp;
    <a href="" class="links item">4</a>&emsp;&emsp;
    <a href="" class="item">5</a>&emsp;&emsp;
    <a href="" class="item">6</a>&emsp;&emsp;
    <a href="https://www.hao123.com" class="links item">7</a>&emsp;&emsp;
    <a href="abcde//last" class="item last">8</a>&emsp;&emsp;
</p>

</body>
</html>

效果

每一个数字都是超链接并且拥有一些不同属性,但是我们能给其渲染不同的样式
在这里插入图片描述

小结

属性选择器:元素类型[属性名=属性值(正则)]
=是绝对等于
*=是包含这个元素

作者有话说

博客创作不易,希望看到这里的读者动动你的小手点个赞,如果喜欢的小伙伴可以一键三连,作者大大在这里给大家谢谢了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值