Skip to content Skip to sidebar Skip to footer

Vertical Align Middle Not Working With Ul Li

I am trying to vertical align middle li content but not able to do it.

Solution 1:

An option is to use a flexbox for the hyperlink inside the li element

#leftPanel {
  width: 25%;
}

ul {
  width: 100%;
}

#leftPanelli {
  list-style: none;
  margin-bottom: 10px;
  width: 100%;
}

#leftPanellia {
  display: flex;
  align-items: center;
}

#leftPanelliimg {
  margin-right: 10px;
}

.activeBtn,
.button5 {
  width: 100%;
  background: #ccc;
  height: 100%;
}
<divid="leftPanel"><ul><li><ahref="#"class="activeBtn"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Location &amp; Sub Location"align="left">Location: Sub Location</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Issued To"align="left">Issued To</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Non Conformance Standard Items"align="left">Non Conformance Standard items</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Attendee &amp; Company"align="left">Attendee : Company</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Construction Calendar"align="left">Construction Calendar</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Associate Users &amp; Permission"align="left">Associate Users : Permission</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Cost Code Form"align="left">Cost Code</a></li></ul></div>

Solution 2:

The problem of vertical alignment is because you have used the align left attribute on your image (you should not use this as align has been deprecated).

This in effect floats your image to the left destroying the natural flow of the cell which is why your text is not vertically aligned, to get around this, you can put your text and images into separate cells:

#leftPanel {
  width: 25%;
}

#leftPanelul {
  display: table;  /* moved this to the ul */width: 100%;
}

#leftPanelli {
  display: table-row;  /* added this (just makes it a little more browser proof) */list-style: none;
  margin-bottom: 10px;
  width: 100%;
}

#leftPanelliimg {
  margin-right: 10px;
  display:block;      /* added this to get rid of the space under the image */
}

#leftPanelli>a {        /* make the anchors table-cell */display: table-cell;
  background: #ccc;
  height: 100%;
  vertical-align: middle;
}

#leftPanelli>a:last-child {
  width: 100%;               /* this just makes the text field expand to take the remaining space */
}
<divid="leftPanel"><ul><li><ahref="#"class="activeBtn"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Location &amp; Sub Location"></a><ahref="#"class="activeBtn">Location: Sub Location</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Issued To"align="left"></a><ahref="#"class="button5">Issued To</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Non Conformance Standard Items"></a><ahref="#"class="button5">Non Conformance Standard items</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Attendee &amp; Company"></a><ahref="#"class="button5">Attendee : Company</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Construction Calendar"></a><ahref="#"class="button5">Construction Calendar</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Associate Users &amp; Permission"></a><ahref="#"class="button5">Associate Users : Permission</a></li><li><ahref="#"class="button5"><imgsrc="https://i.stack.imgur.com/yEshb.gif"width="49"height="45"alt="Cost Code Form"></a><ahref="#"class="button5">Cost Code</a></li></ul></div>

Post a Comment for "Vertical Align Middle Not Working With Ul Li"