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:插入的内容;...;...;}

相关推荐
知识分享小能手2 小时前
Vue3 学习教程,从入门到精通,Axios 在 Vue 3 中的使用指南(37)
前端·javascript·vue.js·学习·typescript·vue·vue3
程序员码歌4 小时前
【零代码AI编程实战】AI灯塔导航-总结篇
android·前端·后端
用户21411832636025 小时前
免费玩转 AI 编程!Claude Code Router + Qwen3-Code 实战教程
前端
小小愿望7 小时前
前端无法获取响应头(如 Content-Disposition)的原因与解决方案
前端·后端
小小愿望7 小时前
项目启功需要添加SKIP_PREFLIGHT_CHECK=true该怎么办?
前端
烛阴7 小时前
精简之道:TypeScript 参数属性 (Parameter Properties) 详解
前端·javascript·typescript
海上彼尚7 小时前
使用 npm-run-all2 简化你的 npm 脚本工作流
前端·npm·node.js
开发者小天8 小时前
为什么 /deep/ 现在不推荐使用?
前端·javascript·node.js
如白驹过隙9 小时前
cloudflare缓存配置
前端·缓存
excel9 小时前
JavaScript 异步编程全解析:Promise、Async/Await 与进阶技巧
前端