css怎么做悬浮文字
导读:#hover-text { background-color: yellow; display: inline-block; position: relative; z-index: 1; }...
#hover-text {
background-color: yellow;
display: inline-block;
position: relative;
z-index: 1;
}
#hover-text:before {
content: "";
position: absolute;
z-index: -1;
top: 5px;
left: 5px;
right: 5px;
bottom: 5px;
background-color: black;
transform: scaleX(0);
transform-origin: left center;
transition: transform 0.3s ease;
}
#hover-text:hover:before {
transform: scaleX(1);
}
在html中,我们可以使用css来制作悬浮文字效果。具体来说,在文字的包裹元素中,我们加入一个伪元素before,用来实现悬浮效果。示例代码如下:
p>
span id="hover-text">
悬浮文字/span>
/p>
#hover-text {
background-color: yellow;
display: inline-block;
position: relative;
z-index: 1;
}
#hover-text:before {
content: "";
position: absolute;
z-index: -1;
top: 5px;
left: 5px;
right: 5px;
bottom: 5px;
background-color: black;
transform: scaleX(0);
transform-origin: left center;
transition: transform 0.3s ease;
}
#hover-text:hover:before {
transform: scaleX(1);
}
在以上代码中,我们给文字包裹元素设置了背景色、inline-block布局和相对定位等属性,然后给伪元素before设置了绝对定位和z-index为-1,从而达到让before在文字之下的效果。在before上设置了黑色背景色,并将其初始的transform属性设置成了scaleX(0),即在X轴上缩小到0,从而隐藏before。在鼠标悬浮在文字上方时,我们使用:hover选择器将before的transform属性变为scaleX(1),即在X轴上放大到原尺寸,从而展示before,从而达到了悬浮效果。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: css怎么做悬浮文字
本文地址: https://pptw.com/jishu/534672.html
