切入追逐:
我有一个垂直菜单栏,我希望<li>
元素具有完整的背景色+垂直对齐。
我在这里看到有人建议添加vertical-align: middle;
赢得足够的奖金,我需要添加一个display: table-cell;
,但这并不能真正帮助垂直菜单。
HTML
<div class="sidebar">
<img src="img/Logo.png">
<ul>
<li><span class="glyphicon glyphicon-home" style="color: white;"></span><a href="#">Home</a></li>
</ul>
</div>
CSS:
.sidebar{
width:150px;
background-color: #0e1a35;
height: 100%;
}
.sidebar > img:nth-child(1){
width: 70%;
display:block;
margin:auto;
padding: 5px ;
}
.sidebar > ul {
list-style: none;
padding:0;
margin:0;
width: 100%;
}
.sidebar > ul > li{
text-decoration: none;
background-color: #42105d;
height:50px;
vertical-align: middle;
float: none;
width: 100%;
text-align: left;
border-left: #5584ff 4px solid;
}
.sidebar > ul > li > a{
color: white;
text-decoration: none;
width: 100%;
height: 100%;
position: relative;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
display: inline-block;
}
.sidebar .glyphicon{
margin-left: 0px;
margin-right: 10px;
}
所以我的问题是如何让<li>
垂直与覆盖<li>
的背景颜色对齐,而不更改<li>
<的填充/边距/ p>
答案 0 :(得分:1)
A
元素LI
A
nchor元素的样式line-height
来匹配实际
height
@import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");
*{box-sizing: border-box;}
body{margin:0;}
.sidebar{
width:150px;
}
.sidebar ul{
margin:0;
padding:0;
}
.sidebar a{
text-decoration:none;
display:block;
background: #42105d;
color:#fff;
border-left: #5584ff 4px solid;
height:50px;
line-height:50px;
padding: 0 8px;
}
&#13;
<div class="sidebar">
<ul>
<li>
<a href="#"> <!-- WRAP ALL INSIDE THE ANCHOR! -->
<span class="glyphicon glyphicon-home" style="color: white;"></span> Home
</a>
</li>
</ul>
</div>
&#13;
以下是没有使用line-height
和height
,而是使用A
填充的示例
@import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");
*{box-sizing: border-box;}
body{margin:0;}
.sidebar ul{
width:150px;
margin:0;
padding:0;
}
.sidebar a{
text-decoration:none;
display:block;
background: #42105d;
color:#fff;
border-left: #5584ff 4px solid;
padding: 16px 8px;
}
&#13;
<div class="sidebar">
<ul>
<li>
<a href="#">
<span class="glyphicon glyphicon-home" style="color: white;"></span> Home
</a>
</li>
<li>
<a href="#">
<span class="glyphicon glyphicon-user" style="color: white;"></span> About
</a>
</li>
<li>
<a href="#">
<span class="glyphicon glyphicon-envelope" style="color: white;"></span> Contact
</a>
</li>
</ul>
</div>
&#13;