clusterBuddy clusterBuddy - 3 months ago 6
CSS Question

Setting <li> background to full width + centering vertical height, possible?

to cut to the chase:

I have a vertical menu-bar and I want to have an

<li>
element with full background color + vertical align.
I've seen here someone recommending adding the
vertical-align: middle;
won't suffice and I would need to add a
display: table-cell;
but that doesn't really help vertical menus.

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;
}


So my question is how to have the
<li>
vertical aligned with a background color covering the
<li>
without changing the padding/margin of the
<li>

Answer
  • HTML: Wrap icon and all inside the A element
  • Don't style the LI
  • Focus styling the Anchor element instead
  • For vertical alignment you can use line-height to match the actual 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;
}
<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>

Here's an example without using line-height and height but instead playing with A's padding:

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

Comments