如何在图标顶部对齐徽章

时间:2015-02-01 15:30:07

标签: html css twitter-bootstrap-3 badge

我正在尝试在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>

1 个答案:

答案 0 :(得分:2)

您需要将徽章元素相对于其父menutoggle容器放置。对于具有所需顶部和左/右值的徽章,请设置position: relative;.menutoggleposition: absolute;

@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>