如何在引导程序中更改活动选项卡的颜色

时间:2018-08-06 06:13:57

标签: html css bootstrap-4

我想创建一个带有选项卡的搜索框,但问题是当我在选项卡上按时,活动选项卡不显示任何文本,因为我将颜色设置为白色。我要那个;当我按下选项卡时,活动选项卡将显示蓝色文本,而所有其他选项卡将在蓝色背景上显示白色文本。我为此使用了引导程序。

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="nav nav-tabs" style="margin-left: 20px;" id="myTab" role="tablist" >
  <li class="nav-item" style="background-color:#0080FF; ">
    <a class="nav-link active " id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true" style="color: white; ">Home</a>
  </li>
  <li class="nav-item" style="background-color:#0080FF; ">
    <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false" style="color:white ;">Profile</a>
  </li>
  <li class="nav-item" style="background-color:#0080FF; ">
    <a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false" style="color:white ;">Contact</a>
  </li>
</ul>

<div class="tab-content" id="myTabContent" >
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab" >...</div>
  <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
  <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">...</div>
</div>

这是我的代码:

3 个答案:

答案 0 :(得分:3)

当这不是最后的选择时,您不应使用内联CSS。 我重构您的代码并按如下所示编辑CSS。

这只是特异性值的问题。您可以参考此https://css-tricks.com/specifics-on-css-specificity/

.nav-tabs .nav-item .nav-link {
  background-color: #0080FF;
  color: #FFF;
}

.nav-tabs .nav-item .nav-link.active {
  color: #0080FF;
}

.tab-content {
  border: 1px solid #dee2e6;
  border-top: transparent;
  padding: 15px;
}

.tab-content .tab-pane {
  background-color: #FFF;
  color: #0080FF;
  min-height: 200px;
  height: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

  <div class="col-md-11">
    <ul class="nav nav-tabs" id="myTab" role="tablist">
      <li class="nav-item">
        <a class="nav-link active " id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Home</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Profile</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">Contact</a>
      </li>
    </ul>

    <div class="tab-content" id="myTabContent">
      <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">The standard Lorem Ipsum passage, used since the 1500s "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
        nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
        laborum." Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis
        et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem
        ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam,
        nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"</div>
      <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
      <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">...</div>
    </div>
  </div>

答案 1 :(得分:0)

使用此样式可以更改Bootstrap中活动标签的颜色

ul.nav li.active a {color:red;} 

您正在使用活动类进行标记。但是bootsrap给

  • 标记赋予了活动类。

      

    这完全取决于您的引导程序版本。

  • 答案 2 :(得分:0)

    您始终可以通过CSS修改导航列表。请改用此CSS代码,以这种方式使用您使用标签来更改颜色及其背景颜色。使用li标签仅设置背景,使用a标签设置文本的颜色是不合适的。您始终可以将bg颜色和文本颜色组合到一个标签中。而且也不建议在html中使用内联CSS。

    CSS代码

    ul.nav-tabs li.nav-item {display:block;vertical-align:top;width:100%;}
    ul.nav-tabs li.nav-item a.nav-link {width:100%;display:block;background-color:#0080FF;color:#fff;text-decoration:none;}
    ul.nav-tabs li.nav-item a.nav-link:active,.nav-item a.nav-link:hover {color:red;background-color:red;}