如何使用 CSS 创建聊天消息?
要使用 CSS 创建聊天消息,代码如下:-
例子
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0 auto;
max-width: 800px;
padding: 0 20px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.message {
font-size: 18px;
font-weight:500;
border: 2px solid #dedede;
background-color: #55e4a8;
color:rgb(0, 0, 0);
border-radius: 12px;
padding: 10px;
margin: 10px 0;
}
.darker {
border-color: #ccc;
background-color: rgb(111, 25, 182);
color:white;
}
.message::after {
content: "";
clear: both;
display: table;
}
.profilePic{
width: 100%;
}
.message img {
float: left;
max-width: 60px;
width: 100%;
margin-right: 20px;
border-radius: 50%;
}
.message img.right {
float: right;
margin-left: 20px;
margin-right:0;
}
.timeStampRight {
float: right;
color: rgb(0, 0, 0);
font-weight: bold;
}
.timeStampLeft {
float: left;
color: rgb(255, 255, 255);
font-weight: bold;
}
</style>
</head>
<body>
<h1>Chat Messages Example</h1>
<div class="message">
<img class="profilePic" src="https://i.picsum.photos/id/295/200/200.jpg">
<p>Hello. How are your holidays going?</p>
<span class="timeStampRight">9:00</span>
</div>
<div class="message darker">
<img class="profilePic" src="https://i.picsum.photos/id/255/200/200.jpg">
<p>They are going pretty good.Thanks for asking!</p>
<span class="timeStampLeft">9:05</span>
</div>
<div class="message">
<img class="profilePic" src="https://i.picsum.photos/id/295/200/200.jpg">
<p>Are you visiting somewhere in holidays?</p>
<span class="timeStampRight">9:07</span>
</div>
<div class="message darker">
<img class="profilePic" src="https://i.picsum.photos/id/255/200/200.jpg">
<p>Yes! Thinking of going to Disneyland.</p>
<span class="timeStampLeft">9:10</span>
</div>
</body>
</html>输出
以上代码将产生以下输出:-

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
JavaScript
PHP