css 如何更改活动导航项的背景颜色

70gysomp  于 2023-05-08  发布在  其他
关注(0)|答案(3)|浏览(211)

我在页面中使用引导导航选项卡,我想更改活动选项卡的背景颜色和文本颜色

<div class="tab-content">
            <div class="os-tabs-controls">
                <ul class="nav nav-pills text-upper pl-2 pr-2">
                    <li class="nav-item tabs p-2">
                        <a class="nav-link" data-toggle="tab" href="#leavelist" aria-expanded="true">
                            LeaveList
                        </a>
                    </li>
                    <li class="nav-item tabs p-2">
                        <a class="nav-link" data-toggle="tab" href="#procurementlist" aria-expanded="false">
                            Procurement List
                        </a>
                    </li>
                </ul>
            </div>
        </div>

我的CSS如下

<Style>
.nav-item a:active{
        color: white;
        background-color: red;
    }
</Style>

上面的代码在我单击tab时工作正常,但它立即变回默认的颜色样式。请任何人看看这个问题,并帮助我让它工作。

htrmnn0y

htrmnn0y1#

它的工作现在,实际上我覆盖了默认的样式与我的自定义样式添加以下css

<style>
.nav-pills .nav-link.active, .nav-pills .show > .nav-link {
        color: #F0F8FF;
        background-color: #CD0B0B;
    }
</style>
8qgya5xd

8qgya5xd2#

你可以使用jQuery:

$('.nav-item a').click(function(e) {
    $('.nav-item a').removeClass('current_page_item');
    $(this).addClass('current_page_item');
});

CSS:

.current_page_item {
  background-color: red;
  color: white;
}

JSFiddle在这里:Link

kadbb459

kadbb4593#

很简单,在页面上的引用链接处添加类

.active{
        color: white;
        background-color: red;
    }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"><script
  src="https://code.jquery.com/jquery-3.5.1.js"
  integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
  crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<div class="tab-content">
            <div class="os-tabs-controls">
                <ul class="nav nav-pills text-upper pl-2 pr-2">
                    <li class="nav-item tabs p-2">
                        <a class="nav-link" data-toggle="tab" href="#leavelist" aria-expanded="true">
                            LeaveList
                        </a>
                    </li>
                    <li class="nav-item tabs p-2">
                        <a class="nav-link active" data-toggle="tab" href="#procurementlist" aria-expanded="false">
                            Procurement List
                        </a>
                    </li>
                </ul>
            </div>
        </div>

如果你想在悬停时有同样的效果,你可以简单地把它添加到导航链接类中。
对于一页网站:

$('body').scrollspy({
  target: '#myNavbar',
  offset: 50
});
.navbar-nav li>a:hover,.navbar-nav li>a:focus,.navbar-nav li.active {
        color: white;
        background-color: red;
    }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"><script
  src="https://code.jquery.com/jquery-3.5.1.js"
  integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
  crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<div class="tab-content">
            <div class="os-tabs-controls">
                <ul class="nav nav-pills text-upper pl-2 pr-2">
                    <li class="nav-item tabs p-2">
                        <a class="nav-link" data-toggle="tab" href="#leavelist" aria-expanded="true">
                            LeaveList
                        </a>
                    </li>
                    <li class="nav-item tabs p-2">
                        <a class="nav-link" data-toggle="tab" href="#procurementlist" aria-expanded="false">
                            Procurement List
                        </a>
                    </li>
                </ul>
            </div>
        </div>
        
        
<div id="leavelist">Section One</div>
 <div id="procurementlist">Section Two</div>

相关问题