gpt4 book ai didi

javascript - 最简单的 "red notification badge"CSS 计数

转载 作者:IT王子 更新时间:2023-10-29 02:47:50 26 4
gpt4 key购买 nike

我需要显示带有计数的流行红色通知指示器,如下所示。跨浏览器获得看起来不错的东西似乎很棘手。例如,不同的浏览器似乎对填充的处理方式不同,导致通知看起来很奇怪。

确保通知正常显示的最佳跨浏览器方式是什么?不反对使用javascript,但纯css当然更可取

enter image description here

最佳答案

实现此目的的最佳方法是使用绝对定位:

/* Create the blue navigation bar */
.navbar {
background-color: #3b5998;
font-size: 22px;
padding: 5px 10px;
}

/* Define what each icon button should look like */
.button {
color: white;
display: inline-block; /* Inline elements with width and height. TL;DR they make the icon buttons stack from left-to-right instead of top-to-bottom */
position: relative; /* All 'absolute'ly positioned elements are relative to this one */
padding: 2px 5px; /* Add some padding so it looks nice */
}

/* Make the badge float in the top right corner of the button */
.button__badge {
background-color: #fa3e3e;
border-radius: 2px;
color: white;

padding: 1px 3px;
font-size: 10px;

position: absolute; /* Position the badge within the relatively positioned button */
top: 0;
right: 0;
}
<!-- Font Awesome is a great free icon font. -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>

<div class="navbar">
<div class="button">
<i class="fa fa-globe"></i>
<span class="button__badge">2</span>
</div>
<div class="button">
<i class="fa fa-comments"></i>
<span class="button__badge">4</span>
</div>
</div>

关于javascript - 最简单的 "red notification badge"CSS 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5747863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com