
网站上的文本样式是网页设计和开发的一个重要方面。为此,CSS(层叠样式表)是一个可供您使用的强大工具。 CSS 是一种多功能工具,允许以各种方式操纵文本的外观。最受追捧的技术之一是更改所选文本的颜色。在本文中,我们将学习两种使用 CSS 更改所选文本颜色的技术。
::selection 伪元素是一个强大的功能,使我们能够选择用户当前突出显示的文本并为其设置样式。要更改所选文本的颜色,我们使用 color 属性。例如,如果我们想让所选文本显示为栗色,我们将使用以下 CSS -
::selection {
color: red;
}
这会将整个网页的所选文本颜色更改为红色。
以下示例将所选文本的颜色更改为红色,背景颜色更改为黑色。
立即学习“前端免费学习笔记(深入)”;
<html >
<head>
<title>Change the color of selected text using CSS?</title>
<style>
::selection {
color: red;
background-color:black;
}
</style>
</head>
<body>
<h2>Changing the color of selected text using CSS</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting</p>
</body>
</html>
我们可以更改特定元素或类上选定文本的颜色和背景颜色。例如,我们可以使用以下 CSS 更改特定“h1”标签内选定文本的颜色 -
如果您是新用户,请直接将本程序的所有文件上传在任一文件夹下,Rewrite 目录下放置了伪静态规则和筛选器,可将规则添加进IIS,即可正常使用,不用进行任何设置;(可修改图片等)默认的管理员用户名、密码和验证码都是:yeesen系统默认关闭,请上传后登陆后台点击“核心管理”里操作如下:进入“配置管理”中的&ld
0
h1::selection {
background: red;
color: white;
}
这会将 h1 元素中选定文本的颜色更改为白色,并将选定文本的背景颜色更改为红色。
以下示例将选定的
文本更改为蓝色,背景颜色更改为粉色。
<html>
<title>Change the color of selected text using CSS</title>
<style>
h1::selection {
background: red;
color: white;
}
h2::selection {
background: yellow;
color: red;
}
p::selection {
background: pink;
color: blue;
}
</style>
</head>
<body>
<h1>Welcome to tutorialsPoint</h1>
<h2>Change the color of selected text using CSS</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
printer took a galley of type and scrambled it to make a type specimen book. It has survived
not only five centuries, but also the leap into electronic typesetting</p>
</body>
</html>
使用 CSS 更改所选文本的颜色是一项简单的任务,可以通过利用 ::selection 伪元素来完成。通过使用颜色和背景颜色属性,我们可以更改网站上所选文本的外观。此外,我们可以在特定元素或类上使用 ::selection 伪元素,以更精确地控制所选文本的样式。
以上就是如何使用 CSS 更改选定文本的颜色?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号