css实现聊天对话框的小三角样式(从实现原理上理解)

ps:本人初次研究,仅记录个人学习经验,如果错误还请多多指教!
最近才仔细研究对话框的小三角如何实现,先来了解一下相关的知识—border属性:
先看代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			div {
				width: 200px;
				height: 200px;
				border-top: 25px solid blue;
				border-bottom: 25px solid red;
				border-left: 25px solid yellow;
				border-right: 25px solid skyblue;
				background-color: gray;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

效果如图:
在这里插入图片描述
可以看出来,四边的border都是梯形(我以前一直以为是长方形o(╥﹏╥)o),所以这时候把div的width和height都设置为0,那么就可以得到四个三角形的border:

<style type="text/css">
			div {
				width: 0px;
				height: 0px;
				border-top: 25px solid blue;
				border-bottom: 25px solid red;
				border-left: 25px solid yellow;
				border-right: 25px solid skyblue;
				background-color: gray;
			}
		</style>

在这里插入图片描述
如果不设置其中一边的border的话,比如这样:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			div {
				width: 0px;
				height: 0px;
				border-top: 25px solid red;
				border-bottom: 25px solid yellow;
				border-right: 25px solid skyblue;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

效果图:
在这里插入图片描述
以此类推,还可以只设置两边的border,不够这两条边必须相邻,不然会显示不出来!
然后,可以根据需要,只设置你想要的那个三角形,其他的设置为透明的即可!
比如只要最右边的三角形:

<style type="text/css">
			div {
				width: 0px;
				height: 0px;
				border-top: 25px solid transparent;
				border-bottom: 25px solid transparent;
				border-left: 25px solid transparent;
				border-right: 25px solid skyblue;
			}
		</style>

在这里插入图片描述
最后直接实现一个带有小三角的对话框:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			div {
				width: 200px;
				height:100px;
				border-radius: 5px;
				background-color: #ccc;
				position: relative;
				margin: 0 auto;
			}
			div:before{
				width: 0px;
				height: 0px;
				content: "";
				border-top: 10px solid transparent;
				border-bottom: 10px solid transparent;
				border-right: 10px solid #ccc;
				position: absolute;
				top: 40px;
				left: -10px;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

注意:以上代码千万不要忘记写【content:"";】,不然就显示不出来!
效果图:
在这里插入图片描述
还可以根据需要调节小三角的形状:

<style type="text/css">
			div {
				width: 200px;
				height:100px;
				border-radius: 5px;
				background-color: #ccc;
				position: relative;
				margin: 0 auto;
			}
			div:before{
				width: 0px;
				height: 0px;
				content: "";
				border-top: 30px solid transparent;
				border-bottom: 5px solid transparent;
				border-right: 20px solid #ccc;
				position: absolute;
				top: 30px;
				left: -10px;
			}
		</style>

效果图:
在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值