在图标上显示通知数量

18

我有一个通知图标(),它显示通知的数量。

我有一个问题,就是无法将数字正确地显示在位置上,如下图所示:

输入图片描述

我需要文本变小并向右移动和向上移动一点...这是代码:

.header .bubble {
  border-radius: 100%;
  height: 14px;
  width: 14px;
  background-color: rgba(226, 32, 91, 0.77);
  color: #ffffff;
  text-align: top;
  position: relative;
  top: 0px;
  float: right;
  right: -3px;
}

 <a href="javascript:;" id="notification-center" class="icon-set globe-fill" data-toggle="dropdown"><span class="bubble">2</span>

有人能帮忙吗,我是新手。


1
为什么标题和气泡具有相同的CSS属性?您应该将此分开,使标题相对,并使您的数字相对于标题绝对。 - EugenSunic
它们没有相同的属性。.header .bubble.. 不是 .header, .bubble.. - cgee
你能否创建一个 JSFiddle?或者至少给我们你的标记代码? - Mehdi Brillaud
https://jsfiddle.net/q84ydjr0/ - Shane
1个回答

33

示例1:使用背景图片

最佳方法是使用position:absolute将子级span定位为父级anchor的子元素。

a.notif {
  position: relative;
  display: block;
  height: 50px;
  width: 50px;
  background: url('http://i.imgur.com/evpC48G.png');
  background-size: contain;
  text-decoration: none;
}
.num {
  position: absolute;
  right: 11px;
  top: 6px;
  color: #fff;
}
<a href="" class="notif"><span class="num">2</span></a>

示例2:使用字体神器图标

如果您想使用图标

这是相应的代码

a.fa-globe {
  position: relative;
  font-size: 2em;
  color: grey;
  cursor: pointer;
}
span.fa-comment {
  position: absolute;
  font-size: 0.6em;
  top: -4px;
  color: red;
  right: -4px;
}
span.num {
  position: absolute;
  font-size: 0.3em;
  top: 1px;
  color: #fff;
  right: 2px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<a class="fa fa-globe">
  <span class="fa fa-comment"></span>
  <span class="num">2</span>
</a>


太完美了!谢谢,我如何将图像更改为图标?(<i class="fa fa-globe"></i> - Shane
你可以将相对定位应用于该图标,并执行相同的操作。 - Suresh Karia
我该如何使红色文本框变宽? - Shane
等待为您的图标制作解决方案。 - Suresh Karia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接