TimH TimH - 28 days ago 12
CSS Question

Bootstrap menu: stretch a vertically to fill li

There are subtle variants to this question, but I cannot find anything that has worked for me.

I have a bootstrap ul where I need the anchor elements to completely fill the

li
. The snag is that some menu text is wrapped onto a second line and I need the
a
elements in the rest of the menu to stretch to fill the new space.

Here's the HTML I'm working with.

Notice how Menu 1 doesn't fill the entire height of the menu bar because Menu 2 has a second line of text. I want Menu 1 to expand to fill the height of the menu bar.



<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Case</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container-fluid">
<ul class="nav nav-pills">
<li class="active"><a data-toggle="pill" href="#menu1">Menu 1</a></li>
<li><a data-toggle="pill" href="#menu2">Menu 2<br> test</a></li>
</ul>

<div class="tab-content">
<div id="menu1" class="tab-pane fade in active">
<h3>MENU 1</h3>
<p>Menu 1 content</p>
</div>
<div id="menu2" class="tab-pane fade">
<h3>Menu 2</h3>
<p>Menu 2 content</p>
</div>
</div>
</div>

</body>
</html>




Answer

That's how float works, in order to get your desired output you can add your own css to make it like this:

Note: I've used !important here due to the way the snippet get the resources, on a real case if you have loaded your CSS after bootstrap you don't need that.

.nav-pills {
  display: flex;
}
.nav-pills>li>a {
  height: 100%;
  display:flex !important;
  align-items:center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container-fluid">
  <ul class="nav nav-pills">
    <li class="active"><a data-toggle="pill" href="#menu1">Menu 1</a>
    </li>
    <li><a data-toggle="pill" href="#menu2">Menu 2<br> test</a>
    </li>
  </ul>
  <div class="tab-content">
    <div id="menu1" class="tab-pane fade in active">
      <h3>MENU 1</h3>
      <p>Menu 1 content</p>
    </div>
    <div id="menu2" class="tab-pane fade">
      <h3>Menu 2</h3>
      <p>Menu 2 content</p>
    </div>
  </div>
</div>