如何在Font Awesome符号顶部添加徽章?

LA_*_*LA_ 56 css badge font-awesome

我想在Font Awesome符号(fa-envelope)的顶部添加带有一些数字(5,10,100)的徽章.例如:

图片

但是,我无法理解如何将徽章放在符号的顶部.我的尝试可以在这里找到:jsFiddle.

我想支持Twitter Bootstrap 2.3.2.

Pau*_*e_D 70

这可以在没有额外标记的情况下完成,只需要一个新类(无论如何都会使用)和一个伪元素.

JSFiddle演示

HTML

<i class="fa fa-envelope fa-5x fa-border icon-grey badge"></i>
Run Code Online (Sandbox Code Playgroud)

CSS

*.icon-blue {color: #0088cc}
*.icon-grey {color: grey}
i {   
    width:100px;
    text-align:center;
    vertical-align:middle;
    position: relative;
}
.badge:after{
    content:"100";
    position: absolute;
    background: rgba(0,0,255,1);
    height:2rem;
    top:1rem;
    right:1.5rem;
    width:2rem;
    text-align: center;
    line-height: 2rem;;
    font-size: 1rem;
    border-radius: 50%;
    color:white;
    border:1px solid blue;
}
Run Code Online (Sandbox Code Playgroud)

  • 除非你像'<span>那样沿着额外的元素路线走下去,否则你可以使用这样的`data-attribute`:http://jsfiddle.net/zxVhL/18/ (22认同)
  • 扩展Paulie_D的注释和jsfiddle:当使用数据属性(例如数据计数)从html传入一个参数时,可以使用.badge [data-count ="0"]隐藏徽章:{display:none; } (9认同)
  • 谢谢!有没有办法将`content`从css移到html? (5认同)

Ger*_*Ger 28

虽然@Paulie_D的答案很好,但是当你有一个可变宽度的容器时,它不能很好地工作.

这个解决方案的效果更好:http://codepen.io/johnstuif/pen/pvLgYp

HTML:

<span class="fa-stack fa-5x has-badge" data-count="8,888,888">
  <i class="fa fa-circle fa-stack-2x"></i>
  <i class="fa fa-bell fa-stack-1x fa-inverse"></i>
</span>
Run Code Online (Sandbox Code Playgroud)

CSS:

.fa-stack[data-count]:after{
  position:absolute;
  right:0%;
  top:1%;
  content: attr(data-count);
  font-size:30%;
  padding:.6em;
  border-radius:999px;
  line-height:.75em;
  color: white;
  background:rgba(255,0,0,.85);
  text-align:center;
  min-width:2em;
  font-weight:bold;
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 19

将包含数字fa-envelopespan包含数字div包装在一起position:relative并制作包装div 和span position:absolute.

检查这个小提琴

使用HTML

<div class="icon-wrapper">
   <i class="fa fa-envelope fa-5x fa-border icon-grey"></i>
   <span class="badge">100</span>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.icon-wrapper{
    position:relative;
    float:left;
}

*.icon-blue {color: #0088cc}
*.icon-grey {color: grey}
i {   
    width:100px;
    text-align:center;
    vertical-align:middle;
}
.badge{
    background: rgba(0,0,0,0.5);
    width: auto;
    height: auto;
    margin: 0;
    border-radius: 50%;
    position:absolute;
    top:-13px;
    right:-8px;
    padding:5px;
}
Run Code Online (Sandbox Code Playgroud)

希望这可能对你有所帮助


MSt*_*utt 10

http://jsfiddle.net/zxVhL/16/

通过将徽章放置在图标元素内,我能够相对于图标容器的边界定位徽章.我使用ems 完成了所有大小调整,以便徽章的大小相对于图标的大小,只需更改字体大小即可更改.badge


Dem*_*tic 9

这似乎有用,并且它有一个非常小的代码可以添加.

.badge{
  position: relative;
  margin-left: 60%;
  margin-top: -60%;
}
Run Code Online (Sandbox Code Playgroud)
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<span class="fa-stack fa-3x">
  <i class="fa fa-circle fa-stack-2x"></i>
  <i class="fa fa-bell fa-stack-1x fa-inverse"></i>
  <span class="badge">17</span>
</span>
<span class="fa-stack fa-2x">
  <i class="fa fa-circle fa-stack-2x"></i>
  <i class="fa fa-bell fa-stack-1x fa-inverse"></i>
  <span class="badge">17</span>
</span>
<span class="fa-stack fa-1x">
  <i class="fa fa-circle fa-stack-2x"></i>
  <i class="fa fa-bell fa-stack-1x fa-inverse"></i>
  <span class="badge">17</span>
</span>
Run Code Online (Sandbox Code Playgroud)