用css实现三角形、聊天对话框


前言

常见的聊天对话框,如微信,都是由一个长方形加一个三角形组合而成,重点就在于三角形的制作

在线体验:codepen


一、css制作三角形的实现原理?

三角形的实现原理是元素宽高设置为0,再设置边框宽度、样式和颜色。
例如:
在这里插入图片描述

二、单个三角形

1.原理

只设置一条边的颜色,其他三边颜色设置为透明
例如:

  • 👇 向下三角
    在这里插入图片描述

  • 👉 向右三角
    在这里插入图片描述

  • 👆 向上三角
    在这里插入图片描述

  • 👈 向左三角
    在这里插入图片描述

2.空心三角形

只能看见边线,内部透明的三角形该如何用css实现呢

实现思路:使用css伪元素定位,用两个不同颜色、不同大小的实心三角形叠加,以达到“空心”的效果

<div class="box"><div>

<style>
.box {
  width: 200px;
  height: 50px;
  background-color: #fff;
  border-radius: 6px;
  position: relative;
  border: 1px solid red;
}
.box::before{
  position: absolute;
  top: 15px;
  left: 200px;
  content: '';
  border-width: 10px;
  border-style: solid;
  border-top-color: transparent;
  border-right-color: transparent;
  border-bottom-color: transparent;
  border-left-color: red;
}
.box::after {
  position: absolute;
  top: 15px;
  left: 198.5px;
  content: '';
  border-width: 10px;
  border-style: solid;
  border-top-color: transparent;
  border-right-color: transparent;
  border-bottom-color: transparent;
  border-left-color: #fff;
}
</style>

在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的HTMLCSS代码示例,用于设计聊天对话框的外观和布局: ```html <!DOCTYPE html> <html> <head> <title>Chat Dialog</title> <style type="text/css"> body { font-family: Arial, sans-serif; background-color: #f5f5f5; margin: 0; padding: 0; } .chat-box { width: 400px; height: 500px; margin: 50px auto; background-color: #fff; border-radius: 5px; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); overflow: hidden; } .chat-header { height: 50px; background-color: #4caf50; color: #fff; display: flex; align-items: center; padding: 0 15px; } .chat-header h2 { margin: 0; font-size: 20px; font-weight: normal; } .chat-body { height: 400px; overflow-y: scroll; padding: 15px; } .chat-message { margin: 10px 0; display: flex; flex-direction: column; max-width: 60%; align-self: flex-end; padding: 10px; background-color: #e1ffc7; border-radius: 5px; } .chat-message p { margin: 0; font-size: 14px; line-height: 1.4; } .chat-message .time { font-size: 12px; color: #999; text-align: right; margin-top: 5px; } .chat-input { display: flex; align-items: center; padding: 10px; background-color: #f0f0f0; } .chat-input input[type="text"] { flex: 1; border: none; padding: 8px; border-radius: 5px; font-size: 14px; } .chat-input input[type="submit"] { background-color: #4caf50; color: #fff; border: none; padding: 8px 15px; border-radius: 5px; margin-left: 10px; font-size: 14px; cursor: pointer; } </style> </head> <body> <div class="chat-box"> <div class="chat-header"> <h2>Chat Dialog</h2> </div> <div class="chat-body"> <div class="chat-message"> <p>Hello!</p> <div class="time">11:30 AM</div> </div> <div class="chat-message"> <p>How are you?</p> <div class="time">11:31 AM</div> </div> <div class="chat-message"> <p>I'm fine, thanks.</p> <div class="time">11:32 AM</div> </div> </div> <div class="chat-input"> <input type="text" placeholder="Type your message here..."> <input type="submit" value="Send"> </div> </div> </body> </html> ``` 这个代码会生成一个类似于聊天对话框的界面,包括一个标题栏、消息列表和输入框。您可以根据需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值