简体   繁体   English

导航栏 - 如何为活动链接添加下划线或更改颜色?

[英]Navbar - how to underline or change color active link?

i have navbar.我有导航栏。 I would like underline link in menu which is active.我想在活动的菜单中加下划线链接。 How do that?那怎么办?

 .nav a:active { text-decoration: underline; } .nav a:active { background-color: red!important; }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar-header"> <a class="navbar-brand" href="/">info</a> </div> <ul class="nav navbar-nav"> <li><a href="/main">Main site</a> </li> <li><a href="/login">/login</a> </li> <li><a href="/first">/first</a> </li> <li><a href="/second">/second</a> </li> <li><a href="/third">Sport</a> </li> <li><a href="/fourth">fourth</a> </li> </ul> </nav>

But it does not work.但它不起作用。 I search similar topics but i didn't find good solution for it.我搜索了类似的主题,但没有找到好的解决方案。

I think your code will already change the active color.我认为您的代码已经更改了活动颜色。 But I believe you're thinking of the active element of bootstrap.但是我相信您正在考虑引导程序的活动元素。

The CSS :active selector is used when you want to change the feature of an element when you click it (can see the effect when you click and hold). CSS :active选择器用于当您想在单击时更改元素的特性时(单击并按住时可以看到效果)。 In bootstrap by adding,在引导程序中添加,

class="active"

The styles on the element will shift depending on what page your on.元素上的样式将根据您所在的页面而变化。 See how I modified the CSS?看看我是如何修改 CSS 的? This will change the background of the active element for whatever page you are on.这将更改您所在页面的活动元素的背景。

 .nav a:active { text-decoration: underline; } .nav a:active { background-color: red !important; } /* Notice how we added an id? This let's us be more specific and override the bootstrap selector */ .navbar > #my-nav > .active > a { background-color: red; }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar-header"> <a class="navbar-brand" href="#">info</a> </div> <ul id="my-nav" class="nav navbar-nav"> <li class="active"> <a href="#">Main site</a> </li> <li> <a href="#">login</a> </li> <li> <a href="#">first</a> </li> <li> <a href="#">second</a> </li> <li> <a href="#">Sport</a> </li> <li> <a href="#">fourth</a> </li> </ul> </nav> <script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

Side Notes : The background won't change until you have your site up with different pages to go to.旁注:背景不会改变,直到你的网站有不同的页面可供访问。 For instance you would change,例如你会改变,

<li class="active"> <a href="home.html">Main site</a> </li>

Then whenever you clicked on it from another page, it would change the background to red.然后每当您从另一个页面单击它时,它都会将背景更改为红色。

You need to work with php in order to get what you want.你需要使用 php 才能得到你想要的。

first give every page a name:首先给每个页面一个名字:

<?php $page='home'; ?>

then go to your navigation and add the following to your navigation然后转到您的导航并将以下内容添加到您的导航中

 <li><a <?php if($page == 'Home') {echo 'class="active"';} ?> href="index.php">Home</a></li>

last make a class in ur css with the name active and style it最后在你的 css 中创建一个名为 active 的类并设置它的样式

.active{
text-decoration: underline;
}

this should do the trick这应该可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM