问题
I am trying to add badge over a fontawesome icon but it's not getting aligned properly. Whatever I try, it either comes in the top or on the below. But I am trying to add it over the icon.
Please tell me what am I missing.
Thank You!
@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:
You need to position badge elements relatively to their parent menutoggle
containers. For this set position: relative;
to .menutoggle
and position: absolute;
for badges with desired top and left/right values.
@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>
来源:https://stackoverflow.com/questions/28264375/how-to-align-badge-on-top-of-icon