- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - Clearfix
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 定位
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 转换
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - Justify Items
- CSS - Justify Self
- CSS - 制表符大小
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块尺寸
- CSS - 最小块尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移
- CSS - 口音色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - 重要
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多重背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2d 变换
- CSS - 3d 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - 属性选择器
描述
CSS 属性选择器允许您根据一个或多个属性的存在或值来选择 HTML 元素。它们是定位 HTML 标记中特定元素的强大方法。属性选择器用方括号 [] 括起来,并且可以采用多种形式。
以下部分讨论了一些使用属性选择器的常见方法
CSS [attribute] 选择器
此选择器选择具有指定属性的元素,而不管其值如何。
语法
[attribute]
示例
以下是一个选择所有具有“data-toggle”属性的 HTML 元素的示例
<html>
<head>
<style>
[data-toggle] {
background: yellow;
color: red;
}
</style>
</head>
<body>
<h2>CSS [attribute] Selector</h2>
<div data-toggle="yes">The div with data-toggle attribute</div>
<div>The div without data-toggle attribute</div>
<p data-toggle="no">A paragraph with data-toggle attribute</p>
<p>A paragraph without data-toggle attribute</p>
</body>
</html>
CSS [attribute="value"] 选择器
此选择器选择具有特定属性和特定值的元素。
语法
[attribute="value"]
示例
此选择器选择所有具有 'data-toggle' 属性且其值设置为 'yes' 的元素。
<html>
<head>
<style>
[data-toggle="yes"] {
background: yellow;
color: red;
}
</style>
</head>
<body>
<h2>CSS [attribute="value"] Selector</h2>
<div data-toggle="yes">The div with data-toggle attribute</div>
<div>The div without data-toggle attribute</div>
<p data-toggle="no">A paragraph with data-toggle attribute</p>
<p>A paragraph without data-toggle attribute</p>
</body>
</html>
CSS [attribute*="value"] 选择器
此选择器选择具有特定属性且其值包含特定子字符串的元素。
语法
[attribute*="value"]
示例
此选择器选择所有具有“src”属性且路径中包含“images”的元素
<html>
<head>
<style>
[src*="images"] {
border: 2px dashed red;
}
</style>
</head>
<body>
<h2>CSS [attribute*="value"] Selector</h2>
<img alt="Logo" src = "/css/images/logo.png" />
</body>
</html>
CSS [attribute^="value"] 选择器
此选择器选择具有特定属性且其值以特定字符串开头的元素。
语法
[attribute^="value"]
示例
此选择器选择所有具有“href”属性且以“https://”开头的元素
<html>
<head>
<style>
[href^="https"] {
background: yellow;
text-decoration:none;
color:red;
}
</style>
</head>
<body>
<h2>CSS [attribute^="value"] Selector</h2>
<p><a href="https://tutorialspoint.com">HTTPS Link</a></p>
<p><a href="https://tutorialspoint.com">HTTP Link</a></p>
</body>
</html>
CSS [attribute$="value"] 选择器
此选择器选择具有特定属性且其值以特定字符串结尾的元素。
语法
[attribute$="value"]
示例
此选择器选择所有具有“src”属性且以“.png”结尾的元素
<html>
<head>
<style>
[src$=".png"] {
border: 2px dashed red;
}
</style>
</head>
<body>
<h2>CSS [attribute$="value"] Selector</h2>
<img alt="Logo" src = "/css/images/logo.png" />
</body>
</html>
CSS [attribute|="value"] 选择器
此选择器选择具有特定属性且其值以指定的子字符串开头,后跟一个连字符 (-) 的元素。此选择器通常用于选择具有特定语言属性的元素,例如 lang 属性,这些属性通常使用连字符来表示语言子代码。
语法
[attribute|="value"]
示例
此选择器选择所有具有“lang”属性且以“en”开头,后跟连字符的元素
<html>
<head>
<style>
[lang|="en"] {
border: 2px dashed red;
}
</style>
</head>
<body>
<h2>CSS [attribute|="value"] Selector</h2>
<div lang="en">Hello World in English!</div>
<div lang="fr">Bonjour tout le monde en français!</div>
<div lang="es">Hola Mundo en español!</div>
</body>
</html>
CSS [attribute~="value"] 选择器
此选择器用于选择具有特定属性且其值包含指定单词的元素。该单词应为独立单词,用空格包围或位于属性值的开头或结尾。
语法
[attribute~="value"]
示例
此选择器选择所有具有“class”属性且包含“beautifyme”单词的元素
<html>
<head>
<style>
[class~="beautifyme"] {
background-color:lightblue;
border:2px solid red;
}
</style>
</head>
<body>
<h2>CSS [attribute|="value"] Selector</h2>
<div class="beautifyme">div with <b>beautifyme</b> class</div>
<div class="beautifyme1">div with <b>beautifyme1</b> class</div>
<div class="beautifyme-2">div with <b>beautifyme-2</b> class</div>
</body>
</html>
href 链接的属性选择器
您可以根据其href 属性对元素进行样式设置,href 属性是 <a> 元素上常用的属性,用于指定链接指向的 URL。
这是一个示例 -
<html>
<head>
<style>
ul {
list-style: none;
}
a[href] {
color: rgb(11, 11, 231);
}
a[href="css_backgrounds.htm"] {
color: rgb(224, 152, 18);
}
a[href~="css_colors.htm"] {
color: rgb(51, 255, 0);
}
a[href|="css_padding.htm"] {
color: rgb(0, 255, 255);
}
a[href^="css_margins.htm"] {
color: rgb(255, 0, 55);
}
a[href$="css_lists.htm"] {
color: rgb(255, 230, 0);
}
a[href*="css_borders.htm"] {
color: rgb(112, 108, 108);
}
</style>
</head>
<body>
<ul>
<li> <a href="css_text.htm">CSS Text</a></li>
<li><a href=".htm">CSS Background</a></li>
<li><a href="css_colors.htm">CSS Color</a></li>
<li><a href="css_padding.htm">CSS Padding</a></li>
<li><a href="css_margins.htm">CSS Margin</a></li>
<li><a href="css_lists.htm">CSS List</a></li>
<li><a href="css_borders.htm">CSS Borders</a></li>
</ul>
</body>
</html>
输入的属性选择器
属性选择器可用于根据特定条件(例如其类型、名称、值或其他属性)选择输入元素。
这是一个示例 -
<html>
<head>
<style>
input {
display: block;
margin: 10px;
}
input[type] {
border: 1px solid #e0dd29;
}
input[placeholder="Password"] {
border: 1px solid #f00;
}
input[name|="emailid"] {
background-color: rgb(236, 178, 233);
}
input[type~="submit"] {
background-color: rgb(88, 241, 88);
padding: 10px;
}
input[value^="But"] {
background-color: rgb(236, 149, 68);
padding: 10px;
}
input[type$="box"] {
border-radius: 5px;
height: 50px;
width: 20px;
}
input[type*="adi"] {
height: 50px;
width: 20px;
}
</style>
</head>
<body>
<input type="text" placeholder="Username">
<input type="password" placeholder="Password">
<input type="email" placeholder="Email" name="emailid">
<input type="submit" placeholder="Submit">
<input type="button" value="Button">
<input type="checkbox" placeholder="Checkbox">
<input type="radio" placeholder="Radio">
</body>
</html>
标题的属性选择器
要根据title 属性对元素进行样式设置,您可以使用 CSS 属性选择器 title 来设置具有包含特定值的 title 属性的元素的样式。
这是一个示例 -
<html>
<head>
<style>
span {
display: block;
margin: 10px;
padding: 5px;
}
span[title] {
border: 2px solid rgb(231, 40, 40);
background-color: rgb(109, 177, 236);
}
span[title="CSS Background"] {
border: 2px solid rgb(231, 40, 40);
background-color: rgb(250, 163, 192);
}
span[title|="CSS Border"] {
border: 2px solid rgb(231, 40, 40);
background-color: rgb(245, 248, 79);
}
span[title^="Mar"] {
border: 2px solid rgb(231, 40, 40);
background-color: rgb(255, 147, 23);
}
span[title$="ght"] {
border: 2px solid rgb(231, 40, 40);
background-color: rgb(102, 201, 240);
}
span[title*="CSS Width"] {
border: 2px solid rgb(231, 40, 40);
background-color: rgb(191, 14, 235);
}
</style>
</head>
<body>
<span title="CSS Text">A text refers to a piece of written or printed information in the form of words or characters.</span>
<span title="CSS Background">You can set backgrounds of various HTML elements.</span>
<span title="CSS Border"> border property is used to create a border around an element.</span>
<span title="Margin">Setting up a margin around an HTML element.</span>
<span title="Height">The height property sets the height of an element's content area.</span>
<span title="CSS Width">The width sets the width of an element's content area.</span>
</body>
</html>
语言的属性选择器
您可以使用lang 属性根据其语言选择元素。lang 属性指定元素中包含的文本的语言。
这是一个示例 -
<html>
<head>
<style>
div[lang] {
color: red;
}
div[lang="fr"] {
color: blue;
}
div[lang~="es"] {
color: green;
}
div[lang|="de"] {
color: orange;
}
div[lang^="it"] {
color: purple;
}
div[lang$="ja"] {
color: brown;
}
div[lang*="zh"] {
color: teal;
}
</style>
</head>
<body>
<div lang="en">Hello World in English!</div>
<div lang="fr">Bonjour tout le monde en français!</div>
<div lang="es">Hola Mundo en español!</div>
<div lang="ja">こんにちは、日本語で世界!</div>
<div lang="de">Hallo Welt auf Deutsch!</div>
<div lang="it">Ciao Mondo in italiano!</div>
<div lang="zh">你好世界,中文!</div>
</body>
</html>
CSS 多个属性选择器
CSS 多个属性选择器允许您根据多个属性值选择元素。它用于定位满足多个条件的特定元素。
这是一个示例 -
<html>
<head>
<style>
ul {
list-style: none;
}
a[href] {
color: rgb(231, 11, 194);
}
a[href="css_backgrounds.htm"][href$=".htm"] {
color: rgb(224, 152, 18);
}
a[href="css_colors.htm"] {
color: rgb(51, 255, 0);
}
</style>
</head>
<body>
<ul>
<li><a href="css_text.htm">CSS Text</a></li>
<li><a href="css_backgrounds.htm">CSS Background</a></li>
<li><a href="css_colors.htm">CSS Color</a></li>
</ul>
</body>
</html>
带有同级组合器的 CSS 属性选择器
CSS 属性选择器和同级组合器可以一起使用,根据元素的属性及其与其他元素的关系来选择特定元素。
普通同级组合器 (~) - 此组合器选择在指定元素之后的所有同级元素,但不与其直接相邻。
语法
selector1 ~ selector2 { /* CSS styles */ }相邻同级组合器 (+) - 此组合器选择直接位于指定元素之后的元素。
语法
selector1 + selector2 { /* CSS styles */ }
以下示例演示了相邻同级组合器 (+) 选择标题之后紧随其后的段落,而普通同级组合器 (~) 选择标题之后的 <div> -
<html>
<head>
<style>
h2 + p {
font-weight: bold;
color:blue;
}
h2 ~ div {
color: red;
}
</style>
</head>
<body>
<h2>CSS Background</h2>
<p>You can set backgrounds of various HTML elements.</p>
<div>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text.</p>
</div>
<p>There are many variations of passages of Lorem Ipsum available.</p>
</body>
</html>