CSS实现带三角的对话框

CSS实现带三角的对话框即对话气泡框

作者:爱编程的小金毛球球
日期:2020年3月22日

话不多说,上图展示:
在这里插入图片描述
首先,肯定是要先添加一个框了
HTML

<div class = "div"></div>

CSS

.div{
  border: 2px blue solid;
  width: 200px;
  height: 100px;
}

这样就会得到一个框,如图所示:
在这里插入图片描述
接着就是要实现上边的三角形部分。首先,让我们看看三角形是怎么来的。
我们先做个实验,做个边长为100的正方形div。

<div class = "div"></div>
.div{
  width: 100px;
  height: 100px;
  border: 100px solid;
  border-color: red blue green yellow;
}

在这里插入图片描述
接下来,把长和宽都设置为0px,这样就会出先四个三角形。
在这里插入图片描述
现在将上、左、右border-color都设置为继承父类颜色。
在这里插入图片描述
这样就只剩下一个三角形了,这就是上方三角形的由来,现在将这两个组合起来。用CSS在div之前插入此样式,用::before 就可以啦, 对其绝对定位!为了和之前的边框同颜色,把下边框三角形颜色设为:blue,左右边框为透明色,上边框为透明色。

.div::before{
  content: '';
  width: 0;
  height: 0;
  border: 20px solid;
  position: absolute;
  top: -40px;
  left: 20px;
  border-color:  transparent transparent blue;
}

就会得到这样的图:
在这里插入图片描述
但是现在和想要的目标还差一点点,原来的三角形处应该是空心的,现在来实现它,只需要在三角形处的上方设置同样大小的三角形向下移动边框的宽度个距离,将颜色设置为和最底层相同即可了。

.div::after{
  content: '';
  width: 0;
  height: 0;
  border: 20px solid;
  position: absolute;
  top: -38px;
  left: 20px;
  border-color:  transparent transparent white;
}

在这里插入图片描述
这样就大功告成了,如果想过于边框圆润一些,只要设置border-radius就可以了,将border-radius设置为5px。
在这里插入图片描述
在里边加入文本区域标签textarea就可以啦
在这里插入图片描述
全部代码:
HTML

 <div class="div">
      <textarea>请输入处理信息</textarea>
    </div>

CSS

.div{
  border: 2px blue solid;
  width: 200px;
  height: 100px;
  position: relative;
  border-radius: 5px;
}
.div::before{
  content: '';
  width: 0;
  height: 0;
  border: 20px solid;
  position: absolute;
  top: -40px;
  left: 20px;
  border-color:  transparent transparent blue;
}
.div::after{
  content: '';
  width: 0;
  height: 0;
  border: 20px solid;
  position: absolute;
  top: -38px;
  left: 20px;
  border-color:  transparent transparent white;
}
  textarea{
    border: 0px;
    height: 90px;
    width: 190px;
  }
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现对话框小尾巴的关键是使用 CSS 的 `::before` 和 `::after` 伪元素来画出三角形。以下是一个简单的示例代码: HTML 代码: ```html <div class="dialogue"> <div class="bubble">这是一条对话</div> </div> ``` CSS 代码: ```css .dialogue { position: relative; } .bubble { position: relative; padding: 10px; background-color: #ffffff; border-radius: 5px; box-shadow: 0 0 5px #888888; font-size: 16px; color: #333333; } .bubble::before { content: ""; position: absolute; bottom: -10px; left: 50%; border: 10px solid transparent; border-top-color: #ffffff; transform: translateX(-50%); } .bubble::after { content: ""; position: absolute; bottom: -8px; left: 50%; border: 10px solid transparent; border-top-color: #888888; transform: translateX(-50%); } ``` 解释一下上述代码: - `.dialogue` 元素用来定位对话框三角形。 - `.bubble` 元素是对话框的内容。注意,它的 `position` 属性必须是 `relative`,因为三角形是相对于它进行定位的。 - `.bubble::before` 伪元素用来画出白色的三角形。`border` 属性用来画出三角形,`border-top-color` 属性用来设置上边的边框颜色为白色。 - `.bubble::after` 伪元素用来画出阴影效果。同样使用 `border` 属性画出三角形,但是 `border-top-color` 属性设置为灰色,形成阴影效果。 这样就实现了一个简单的对话框小尾巴效果。你可以根据需要调整三角形的大小和位置,以及对话框的样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值