方案一、使用 box-shadow 添加阴影,用一个块将多余的部分盖住
<div class="bubble margin">
<div class="bubble-word">我是一个气泡我是一个气泡我是一个气泡我是一个气泡我是一个气泡我是一个气泡</div>
</div>
.bubble {
position: relative;
width: 200px;
background-color: #fafafa;
box-shadow: 0 0 5px #444;
border-radius: 4px;
padding: 8px;
box-sizing: border-box;
}
.bubble::before {
content: "";
position: absolute;
top: 5px;
right: 0;
width: 100rpx;
bottom: 5px;
background: #fafafa;
z-index: 2;
}
.bubble::after {
content: "";
position: absolute;
top: 25%;
right: -8px;
width: 0;
height: 0;
border-top: 8px solid transparent;
border-bottom: 8px solid #fafafa;
border-left: 8px solid #fafafa;
border-right: 8px solid transparent;
transform: rotate(-135deg);
box-shadow: 0px 0px 5px #444;
border-radius: 0 0 0 4px;
z-index: 1;
}
.bubble-word {
position: relative;
z-index: 3;
}
效果:
注意:设置层级z-index
方案2:利用 filter: drop-shadow() 添加阴影(推荐)
<div class="bubble">我是一个气泡我是一个气泡我是一个气泡我是一个气泡我是一个气泡我是一个气泡</div>
.bubble {
position: relative;
width: 200px;
background-color: #fff;
border-radius: 4px;
padding: 8px;
box-sizing: border-box;
filter: drop-shadow(1px 1px 5px #000);
}
.bubble::after {
content: "";
position: absolute;
top: 25%;
right: -16px;
width: 0;
height: 0;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 8px solid #fafafa;
border-right: 8px solid transparent;
z-index: 1;
}
效果图:
方案3:实现一个带圆角的气泡(推荐)
<div class="bubble">我是一个气泡我是一个气泡我是一个气泡我是一个气泡我是一个气泡我是一个气泡</div>
.bubble {
position: relative;
width: 200px;
background-color: #fff;
border-radius: 4px;
padding: 8px;
box-sizing: border-box;
filter: drop-shadow(1px 1px 5px #000);
}
.bubble::after {
content: "";
position: absolute;
top: 25%;
right: -6px;
width: 15px;
height: 15px;
background: #fff;
transform: rotate(30deg) skewX(-30deg) scale(1, 0.866);
z-index: 2;
border-top-right-radius: 40%;
z-index: 1;
}
参考:
css如何画一个三角形:
https://blog.csdn.net/qq_36206799/article/details/129324588?spm=1001.2014.3001.5501
CSS 实现不规则元素的阴影:
https://blog.csdn.net/Vue2018/article/details/129078693
CSS边框外的小三角形+阴影效果的实现:
https://www.bbsmax.com/A/l1dyPqMx5e/
CSS 奇思妙想 | 巧妙的实现带圆角的三角形:
https://cloud.tencent.com/developer/article/1847542?from=15425&areaSource=102001.1&traceId=Hc6iSfpBwFhFa2e6YanSp