如何创建 - 聊天消息
了解如何使用 CSS 创建聊天消息。
Chat Messages
Hello. How are you today?
11:00Hey! I'm fine. Thanks for asking!
11:01Sweet! So, what do you wanna do today?
11:02Nah, I dunno. Play soccer.. or learn more coding perhaps?
11:05如何创建聊天消息
步骤 1) 添加 HTML:
实例
<div class="container">
<img src="/w3images/bandmember.jpg"
alt="Avatar">
<p>Hello. How are you today?</p>
<span
class="time-right">11:00</span>
</div>
<div class="container darker">
<img src="/w3images/avatar_g2.jpg"
alt="Avatar" class="right">
<p>Hey! I'm fine. Thanks for asking!</p>
<span class="time-left">11:01</span>
</div>
<div class="container">
<img src="/w3images/bandmember.jpg" alt="Avatar">
<p>Sweet! So, what
do you wanna do today?</p>
<span class="time-right">11:02</span>
</div>
<div class="container darker">
<img src="/w3images/avatar_g2.jpg"
alt="Avatar" class="right">
<p>Nah, I dunno. Play soccer.. or learn
more coding perhaps?</p>
<span class="time-left">11:05</span>
</div>
步骤 2) 添加 CSS:
实例
/* 聊天容器 */
.container {
border: 2px solid #dedede;
background-color: #f1f1f1;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
}
/* 更暗的聊天容器 */
.darker {
border-color: #ccc;
background-color: #ddd;
}
/* 清除浮动 */
.container::after {
content: "";
clear: both;
display: table;
}
/* 图像样式 */
.container img {
float: left;
max-width: 60px;
width: 100%;
margin-right: 20px;
border-radius: 50%;
}
/* Style the right image */
.container img.right
{
float: right;
margin-left: 20px;
margin-right:0;
}
/* 时间文本样式 */
.time-right {
float: right;
color:
#aaa;
}
/* 时间文本样式 */
.time-left {
float: left;
color: #999;
}
亲自试一试 »