How to align bootstrap navbar to bottom of div

2019-07-29 18:34发布

问题:

I have a basic bootstrap based layout I'm working on. I placed a logo image in the navbar brand element. The logo is roughly 140px high. I then have a navbar-right menu element that I would like to anchor to the bottom of the navbar itself. I've experimented with vertical-align on the navbar-right but it doesn't have any affect. Can anyone explain how to achieve this? This is the basic layout code:

    <!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"><img src="images/logo.png" width="432" height="143" ></a>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
         <li><a href="#">Services</a></li>
       <li><a href="#">Contact</a></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>


<div class="container" style="padding-top:200px;">


<div class="row">
    <div class="col-md-4">
      <h2>Heading</h2>
      <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
      <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
    </div>
    <div class="col-md-4">
      <h2>Heading</h2>
      <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
      <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
   </div>
    <div class="col-md-4">
      <h2>Heading</h2>
      <p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
      <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
    </div>
  </div>

</div> <!-- /container -->

回答1:

The problem I had was the right most menu item was butting up against the right side of the page. Instead of right:0, I changed it to right:10%

.nav {
    position:absolute;
    bottom:0;
    right:10%;
}


回答2:

The easiest way to achieve what you're trying is to use position absolute for your ul class.

.nav {position:absolute;
bottom:0;
right:0;}

This will position your menu items at the the bottom of your navbar. Also, if you want to have your image contained inside your navbar then your going to have to change your html a little bit. When you use navbar-brand your adding a whole mess of css properties to your navbar that you probably don't want with an image this size. So if you just change your navbar-brand class to pull-left, you'll achieve a better layout and be able to contain your image inside your navbar. Here's a fiddle with the layout I'm describing. Let me know if you needed something different and I can change it up. https://jsfiddle.net/xbxat9mq/