How to align tabs to top/right in bootstrap 3?

删除回忆录丶 提交于 2019-12-09 05:02:23

问题


I need to align bootstrap 3 tabs to right instead of left.

Is it possible with usage of bootstrap css or do I need to use custom css ?


回答1:


Use the class .navbar-right to push an element to the right within a navbar, or .pull-right to do the same when not in a navbar.

<ul class="nav nav-tabs navbar-right">
    <li class="active"><a href="#">Home</a></li>
    <li><a href="#">Tab1</a></li>
    <li><a href="#">Tab2</a></li>
</ul>

Good to know the other helper-classes here too: http://getbootstrap.com/css/#helper-classes




回答2:


use pull-right class:

<ul class="nav nav-tabs">
    <li class="active pull-right"><a href="#">Home</a></li>
    <li class="pull-right"><a href="#">Tab1</a></li>
    <li class="pull-right"><a href="#">Tab2</a></li>
</ul>



回答3:


Using .pull-right does this, it's undesirable:

What you want is this:

You need to adjust some stuff:

http://jsbin.com/zuxol/1/

HTML

  <div class="right-tabs clearfix">
<ul class="nav nav-tabs">
  <li class="active"><a href="#tab_a" data-toggle="tab">Tab A</a></li>
  <li><a href="#tab_b" data-toggle="tab">Tab B</a></li>
  <li><a href="#tab_c" data-toggle="tab">Tab C</a></li>
  <li><a href="#tab_d" data-toggle="tab">Tab D</a></li>
</ul>
<div class="tab-content">
        <div class="tab-pane active" id="tab_a">
            <h4>Pane A</h4>
            <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                ac turpis egestas.</p>
        </div>
        <div class="tab-pane" id="tab_b">
            <h4>Pane B</h4>
            <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                ac turpis egestas.</p>
        </div>
        <div class="tab-pane" id="tab_c">
            <h4>Pane C</h4>
            <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                ac turpis egestas.</p>
        </div>
        <div class="tab-pane" id="tab_d">
            <h4>Pane D</h4>
            <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames
                ac turpis egestas.</p>
        </div>
</div><!-- tab content -->

</div><!-- end right-tabs -->  

CSS

.right-tabs .nav {
    float: right;
    border-bottom: 0px;
}
.right-tabs .nav li { float: left }
.right-tabs .tab-content {
    float: left;
    border-top: 1px solid #ddd;
    margin-top: -1px;
}



回答4:


I know it's an old post, how ever the soultion is simple :

CSS

.nav-rtl {
    padding-left:40px;
    padding-right:0px;
}

.nav-rtl li {
    float:right;
}

HTML

<ul class="nav nav-tabs nav-rtl" role="tablist">
    <li ..... ></li>
    <li ..... ></li>  // an so on
</ul>



回答5:


In case someone here is looking for an answer using bootstrap 4:

You can add ml-auto to the tab you want to push to the right

<ul class="nav nav-tabs small" role="tablist">
    <li class="nav-item">
        <a class="nav-link active" data-toggle="tab" href="#first" role="tab">First</a>
    </li>
    <!-- .ml-auto: adds margin to the left of this tab, all tabs behind this one, are pushed to the right -->
    <li class="nav-item ml-auto"> 
        <a class="nav-link" data-toggle="tab" href="#second" role="tab">Second</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-toggle="tab" href="#third" role="tab">Third</a>
    </li>
</ul>

bootstrap docs: auto-margins




回答6:


Create a new class for Right alignment and use below css.

/* overwrite boostrap */

.nav>li {
  float: none!important;
  display: inline-block!important;
}
/* new class */

.nav-tabs-right {
  text-align: right;
  font-size: 0;
  /* prevent floated child bug */
}
.nav-tabs-right>li {
  display: inline-block;
  clear: left;
  float: none;
  text-align: right;
  font-size: 12px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="container jumbotron">

  <ul class="nav nav-tabs nav-tabs-right">
    <li class="active">
      <a data-toggle="tabs" href="#tab-1">Tab 1</a>
    </li>
    <li>
      <a data-toggle="tabs" href="#tab-2">Tab 2</a>
    </li>
  </ul>

</div>



回答7:


To use right aligned tabs, just add .text-right to the .nav-tabs class.

<ul class="nav nav-tabs text-right nav-tabs-highlight ">



回答8:


A very simple and good solution is described already here by balexandre:

Align a bootstrap navbar to the right

.right-to-left li {
  float: right !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>


<h2>Bootstrap tab aligned right</h2>
<ul class="nav nav-tabs right-to-left">
  <li role="3" class="active"><a href="#">Tab 3</a></li>
  <li role="2"><a href="#">Tab 2</a></li>
  <li role="1"><a href="#">Tab 1</a></li>
</ul>

(not that the tabs are ordered reverse)




回答9:


You can also override by your container

<div class="someclassintabscontainer">
<!-- Here the tabs code writed normaly -->
</div>

Write this in some css file.

.someclassintabscontainer .nav-tabs > li {
    float: right;
}



回答10:


Add class justify-content-end

<ul class="nav nav-tabs justify-content-end" role="tablist">
</ul>


来源:https://stackoverflow.com/questions/24942637/how-to-align-tabs-to-top-right-in-bootstrap-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!