TimH
TimH

Reputation: 1032

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>

Upvotes: 2

Views: 1581

Answers (2)

DaniP
DaniP

Reputation: 38252

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>

Upvotes: 1

Johannes
Johannes

Reputation: 67748

You can use flexbox for the container (.nav) and assign height: 100% to all a tags in there:

.nav {
  display: flex;
  }
.nav li {
  background: #fa0;
  }
.nav li a {
  height: 100%;
  }
<!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>

Upvotes: 0

Related Questions