如何将引导按钮定位在导航选项卡的右侧?

时间:2019-01-25 16:11:08

标签: twitter-bootstrap bootstrap-4

我正在使用Bootstrap的选项卡组件,无法将蓝色的登录按钮刷新到nav-tabs的右侧。我尝试了各种方法,但显然我不明白为什么它无法定位在我想要的位置。

这是我要做什么的图片:

enter image description here

HTML:

<section class="container">
  <ul class="nav nav-tabs pt-5">
    <li class="nav-item"><a class="nav-link active" href="#">Home</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Data</a></li>
    <li class="nav-item">
      <button type="button" class="btn btn-primary ml-2">Log In</button>
    </li>
  </ul>
</section>

JSFIDDLE: https://jsfiddle.net/auov8ety/

3 个答案:

答案 0 :(得分:1)

您可以在包装按钮的ml-auto上使用类nav-item(有关更多信息,请阅读Bootstrap Spacing

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>

<section class="container">
  <ul class="nav nav-tabs pt-5">
    <li class="nav-item">
      <a class="nav-link active" href="#">Home</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Data</a>
    </li>
    <li class="nav-item ml-auto">
      <button type="button" class="btn btn-primary ml-2">Log In</button>
    </li>
  </ul>
</section>

答案 1 :(得分:0)

只需将ml-auto赋予按钮

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

<section class="container">
  <ul class="nav nav-tabs pt-5">
    <li class="nav-item"><a class="nav-link active" href="#">Home</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Data</a></li>
    <li class="nav-item ml-auto">
      <button type="button" class="btn btn-primary ml-2">Log In</button>
    </li>
  </ul>
</section>

提琴:https://jsfiddle.net/0fep392h/

答案 2 :(得分:0)

我建议您将按钮与列表分开,并将flexboxjustify-content: space-between;一起使用

代码示例:

section.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

   <section class="container pt-5">
      <ul class="nav nav-tabs">
        <li class="nav-item"><a class="nav-link active" href="#">Home</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Data</a></li>
      </ul>
      
      <button type="button" class="btn btn-primary ml-2">Log In</button>
    </section>