如何在图标顶部对齐徽章

How to align badge on top of icon

我正在尝试在 fontawesome 图标上添加徽章,但它没有正确对齐。无论我尝试什么,它要么出现在顶部,要么出现在下方。但我正在尝试将其添加到图标上。

请告诉我我错过了什么。

谢谢!

@import url('//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css');
.menutoggle {
    float: left;
    width: 50px;
    height: 52px;
    font-size: 22px;
    cursor: pointer;
    color: #1d2939;
    border-right: 1px solid #eee;
    border-left: 1px solid #eee;
    -moz-transition: all 0.2s ease-out 0s;
    -webkit-transition: all 0.2s ease-out 0s;
    transition: all 0.2s ease-out 0s;
}

.menutoggle i {
    padding:15px;
  padding-bottom:0px;
}

.menutoggle:hover {
    color: #1d2939;
    background-color: #f7f7f7;
}

.badge{
  display:inline-block;
  min-width:10px;
  padding:3px 7px;
  font-size:12px;
  font-weight:700;
  line-height:1;
  color:#fff;
  text-align:center;
  white-space:nowrap;
  vertical-align:baseline;
  background-color:#777;
  border-radius:10px
}
<ul>
        <li>
           <div class="menutoggle"> 
               <i class="fa fa-cog"></i>
             <span class="badge">1</span>
           </div>
        </li>
</ul>

您需要相对于其父 menutoggle 容器定位徽章元素。对于此设置 position: relative;.menutoggleposition: absolute; 用于具有所需顶部和 left/right 值的徽章。

@import url('//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css');
.menutoggle {
    position: relative;
    float: left;
    width: 50px;
    height: 52px;
    font-size: 22px;
    cursor: pointer;
    color: #1d2939;
    border-right: 1px solid #eee;
    border-left: 1px solid #eee;
    -moz-transition: all 0.2s ease-out 0s;
    -webkit-transition: all 0.2s ease-out 0s;
    transition: all 0.2s ease-out 0s;
}
.menutoggle i {
    padding:15px;
    padding-bottom:0px;
}
.menutoggle:hover {
    color: #1d2939;
    background-color: #f7f7f7;
}
.badge {
    position: absolute;
    top: 0;
    right: 0;
    display:inline-block;
    min-width:10px;
    padding:3px 7px;
    font-size:12px;
    font-weight:700;
    line-height:1;
    color:#fff;
    text-align:center;
    white-space:nowrap;
    vertical-align:baseline;
    background-color:#777;
    border-radius:10px;
}
<ul>
    <li>
        <div class="menutoggle"> 
            <i class="fa fa-cog"></i>
            <span class="badge">1</span>
        </div>
    </li>
</ul>