hover伪类
- 用法一:悬于自身
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style>
#box1 {width: 200px; height: 200px; background: red}
#box2 {width: 100px; height: 100px; background: blue;}
#box1:hover {background: #CB5EC6}
</style>
</head>
<body>
<div id="box1">
<!-- <a href="#">连接</a>-->
<!-- <div id="box2"></div>-->
</div>
</body>
</html>


- 用法二:悬于父类作用于子类
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style>
#box1 {width: 200px; height: 200px; background: red}
#box2 {width: 100px; height: 100px; background: blue;}
#box1:hover #box2 {background: #CB5EC6}
</style>
</head>
<body>
<div id="box1">
<!-- <a href="#">连接</a>-->
<div id="box2"></div>
</div>
</body>
</html>


注意事项:
- 只可悬浮于父类作用于子类,不可悬浮于子类作用于父类;
- 悬浮于同一级,不能作用于同一级;
总结:只可以悬于自身或者悬于父类作用于子类,其他的都不行!!!

本文详细介绍了CSS中hover伪类的两种使用方法:直接应用于元素自身改变状态,以及通过父元素悬停影响子元素样式。文章强调了hover伪类的作用范围限制,并提供了代码示例。
2100

被折叠的 条评论
为什么被折叠?



