css怎么实现鼠标选中文字后改变背景色-LMLPHP

本教程操作环境:windows7系统、css3版本、Dell G3电脑。

教程推荐:css视频教程

css实现鼠标选中文字后改变背景色

css鼠标选中文字后改变背景色可以使用::selection选择器。

::selection选择器匹配元素中被用户选中或处于高亮状态的部分。::selection只可以应用于少数的CSS属性:color、background、cursor和outline。

代码示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<style type="text/css">
			p {
				border: 1px solid red;
			}
			
			.color::-moz-selection {
				background: #93c;
				color: #fcf;
			}
			
			.color::selection {
				background: #93c;
				color: #fcf;
			}
		</style>
	</head>

	<body>
		<p>默认文本。测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!</p>
		<p class="color">鼠标选中文字后改变背景色。测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!</p>
	</body>

</html>
登录后复制

效果图:

css怎么实现鼠标选中文字后改变背景色-LMLPHP

更多编程相关知识,请访问:编程教学!!

以上就是css怎么实现鼠标选中文字后改变背景色的详细内容,更多请关注Work网其它相关文章!

09-16 18:55