要在悬停上居中显示边框,可以使用CSS伪类:hover
结合transform
属性来实现。以下是一个详细的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hover Border Center</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="box">
Hover over me!
</div>
</body>
</html>
.box {
width: 200px;
height: 200px;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
position: relative;
transition: transform 0.3s ease;
}
.box:hover {
border: 2px solid #007bff;
transform: scale(1.1);
}
.box::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 2px solid transparent;
transition: border-color 0.3s ease;
}
.box:hover::before {
border-color: #007bff;
}
div
元素,并为其添加一个类名box
。.box
:设置盒子的基本样式,包括宽度、高度、背景颜色,并使用flex
布局使其内容居中。.box:hover
:在悬停时添加边框,并通过transform: scale(1.1)
使盒子稍微放大,以突出显示效果。.box::before
:使用伪元素::before
创建一个透明的边框,初始状态下不可见。.box:hover::before
:在悬停时改变伪元素的边框颜色,使其可见。flex
布局或position: absolute
结合transform
属性来居中内容。通过上述方法,可以有效地在悬停时居中显示边框,并提供良好的用户体验。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云