1.利用 CSS 的伪类中的content属性获取attr中的信息
效果图:鼠标放上去出现提示
css代码:
.box{position:relative;display:inline-block;margin:100px;} .box:hover:after{content: attr(data-msg);position:absolute;top:100%;left:0;display:inline-block;background:rgba(0,0,0,0.8);color:#fff;border-radius:5px;line-height:25px;padding:0 10px;} .box:hover:before{content:'';border:3px solid transparent;border-bottom-color:rgba(0,0,0,0.8);display:block;position:absolute;top:100%;margin-top:-6px;left:50%;tansform:translateX(-50%);}
html代码:
<div class="box" data-msg="hahaha">43343</div>
2.实现带边框的三角形,利用伪类实现
比如弹框中的这种效果:
css代码:
.triangle{ position:relative; 0px; height:0px; border: 11px solid transparent; border-top: 10px solid #ddd; border-bottom-0px; } .triangle:after{ content: ''; position: absolute; display: block; z-index: 4; left: -10px; top: -10px; 0; height: 0; border: 10px solid transparent; border-top: 10px solid #fdf8f8; border-bottom: 0; }
html代码
<div class="triangle"></div>