如何使用 CSS 创建警报消息?
警报消息可以在网页上看到。例如,在社交媒体网站上删除帐户时,可能会看到一条消息。甚至一些网站也会以警报消息的形式提供优惠券。其他例子包括“您的订单已确认”或“您的密码即将过期。请立即更新。”让我们看看如何使用 HTML 和 CSS 创建警报消息。
创建警报的容器
为警报消息设置一个 div。在其中,警报消息与关闭按钮符号一起设置 -
<div class="alertMessage"> <span class="close" onclick="this.parentElement.style.display='none';">×</span> <strong>This cannot be revered!</strong> Your account will be deleted. </div>
设置警报样式
我们使用 background-color 属性为警报消息设置了背景颜色 -
.alertMessage {
padding: 20px;
background-color: #f44336;
color: white;
font-size: 20px;
}
警报上的关闭按钮
警报消息上有一个可见的关闭按钮,以便用户阅读后可以将其移除。关闭的符号是 x -
<span class="close" onclick="this.parentElement.style.display='none';">×</span>
定位关闭按钮
看起来像关闭按钮的 x 的样式如下。关闭按钮使用 float 属性(值为 right)进行定位。使用 transition 属性设置过渡 -
.close{
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}
示例
要使用 CSS 创建警报消息,代码如下 -
<!DOCTYPE html>
<html>
<head>
<style>
body{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.alertMessage {
padding: 20px;
background-color: #f44336;
color: white;
font-size: 20px;
}
.close{
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}
.close:hover {
color: black;
}
</style>
</head>
<body>
<h1>Alert Message Example</h1>
<div class="alertMessage">
<span class="close" onclick="this.parentElement.style.display='none';">×</span>
<strong>This cannot be revered!</strong> Your account will be deleted.
</div>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP