CSS高级选择器

一、属性选择器

以value开头的att属性的E元素:E[att^="value"]{ ;}

a[href^=http]{background-color="red";}

css

css 复制代码
a[href^=http]{
    background-color="red";
}

html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a[href^=http]{
            background-color: red;
        }
    </style>
</head>
<body>
    <p class="demo">
        <a href="http://www.baidu.com" class="links item first" >1</a>
        <!-- 1背景变红 -->
        <a href class="active item" title="test website" target=" blank">2</a>
        <a href="sites/file/test.html" class="links item">3</a>
        <a href="sites/file/test.png" class="links item"> 4</a>
        <a href="sites/file/image,ipg" class="links item">5</a>
    </p>
</body>
</html>

结果,1背景变红

以value结尾的att属性的E元素:E[att$="value"]{ ;}

a[href$=png]{background-color: red;}

css

css 复制代码
        a[href$=png]{
            background-color: red;
        }

html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a[href$=png]{
            background-color: red;
        }
    </style>
</head>
<body>
    <p class="demo">
        <a href="http://www.baidu.com" class="links item first" >1</a>
        <a href class="active item" title="test website" target=" blank">2</a>
        <a href="sites/file/test.html" class="links item">3</a>
        <a href="sites/file/test.png" class="links item"> 4</a>
        <!-- 4背景变红 -->
        <a href="sites/file/image,ipg" class="links item">5</a>
    </p>
</body>
</html>

结果,4背景变红

含有value的att属性的E元素:E[att*="value"] { ;}

a[class*=links] { background: red; }

css

css 复制代码
        a[class*=links]{
            background-color: red;
        }

html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a[class*=links]{
            background-color: red;
        }
    </style>
</head>
<body>
    <p class="demo">
        <a href="http://www.baidu.com" class="links item first" >1</a>
        <a href class="active item" title="test website" target=" blank">2</a>
        <!-- 除了2背景都变红 -->
        <a href="sites/file/test.html" class="links item">3</a>
        <a href="sites/file/test.png" class="links item"> 4</a>  
        <a href="sites/file/image,ipg" class="links item">5</a>
    </p>
</body>
</html>

除了2背景都变红

二、关系选择器

子代选择器:E元素的直接下一级的所有子元素F,对下下级无效:E>F{ ;}

body>p{ background: pink; }

相邻兄弟选择器:E元素的下一个平级F元素,紧贴着E:E+F{ ;}

.active+p { background: green; }

普通兄弟选择器:E元素之后的所有平级元素F:E~F{ ;}

.active~p{ background: yellow; }

三:结构伪类选择器

四:链接伪类选择器

五:伪元素选择器

在被选元素E的内容的前面插入内容,必须配合content属性:E::before{content:插入的内容;...;...;}

在被选元素E的内容的后面插入内容,必须配合content属性:E::after{content:插入的内容;...;...;}

相关推荐
ai小鬼头13 分钟前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
一只叫煤球的猫1 小时前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈
vvilkim1 小时前
Electron 自动更新机制详解:实现无缝应用升级
前端·javascript·electron
vvilkim1 小时前
Electron 应用中的内容安全策略 (CSP) 全面指南
前端·javascript·electron
aha-凯心1 小时前
vben 之 axios 封装
前端·javascript·学习
遗憾随她而去.1 小时前
uniapp 中使用路由导航守卫,进行登录鉴权
前端·uni-app
xjt_09012 小时前
浅析Web存储系统
前端
foxhuli2292 小时前
禁止ifrmare标签上的文件,实现自动下载功能,并且隐藏工具栏
前端
青皮桔3 小时前
CSS实现百分比水柱图
前端·css
失落的多巴胺3 小时前
使用deepseek制作“喝什么奶茶”随机抽签小网页
javascript·css·css3·html5