使用css实现气泡框的效果
.bubble-container {
position: relative;
display: inline-block; /* or block, depending on your layout needs */
}
.bubble {
position: absolute;
background-color: #fff;
border-radius: 5px;
padding: 10px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15); /* Optional: Add a subtle shadow */
z-index: 1; /* Ensure the bubble is on top */
/* Positioning adjustments - change these based on where you want the bubble to appear */
top: -20px; /* Adjust vertical position */
left: 50%;
transform: translateX(-50%); /* Center horizontally */
}
.bubble::before {
content: "";
position: absolute;
border-style: solid;
border-width: 0 10px 10px 10px; /* Adjust triangle size */
border-color: transparent transparent #fff transparent; /* Match bubble background */
bottom: -10px; /* Position the triangle at the bottom of the bubble */
left: 50%;
transform: translateX(-50%); /* Center horizontally */
}
/* Example usage with different positioning */
.bubble-top {
top: -100%; /* Position above */
left: 50%;
transform: translateX(-50%);
}
.bubble-top::before {
top: 100%; /* Pointing upwards */
bottom: auto;
border-color: #fff transparent transparent transparent;
}
.bubble-left {
top: 50%;
left: -100%; /* Position to the left */
transform: translateY(-50%);
}
.bubble-left::before {
left: 100%; /* Pointing right */
top: 50%;
transform: translateY(-50%);
border-width: 10px 10px 10px 0;
border-color: transparent #fff transparent transparent;
}
.bubble-right {
top: 50%;
right: -100%; /* Position to the right */
transform: translateY(-50%);
left: auto;
}
.bubble-right::before {
right: 100%; /* Pointing left */
top: 50%;
transform: translateY(-50%);
border-width: 10px 0 10px 10px;
border-color: transparent transparent transparent #fff;
}
Explanation and How to Use:
-
bubble-container
: This is the parent container. It's crucial for positioning the bubble relative to its content. Usedisplay: inline-block
if you want the container to fit the content's width, ordisplay: block
if you want it to take up the full width. -
bubble
: This is the actual bubble element.background-color
,border-radius
,padding
, andbox-shadow
control the bubble's appearance.position: absolute
allows us to position it relative to thebubble-container
.z-index: 1
ensures the bubble appears above other content.
-
bubble::before
: This creates the triangle "pointer."content: "";
is necessary for pseudo-elements.border-style: solid
andborder-width
create the triangle shape using transparent borders. The non-transparent border creates the visible triangle. The values here control the size of the triangle.border-color
should match thebackground-color
of the bubble.- Positioning properties (
top
,left
,transform
) control where the triangle appears relative to the bubble.
-
Positioning Variations: The examples
.bubble-top
,.bubble-left
, and.bubble-right
demonstrate how to position the bubble and its pointer in different locations. Adjust thetop
,left
,right
,bottom
, andtransform
properties as needed. The key is to change theborder-color
in the::before
pseudo-element so the solid color corresponds to the direction the triangle should point.
Example HTML (using the bubble-top
variation):
<div class="bubble-container">
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现