简体   繁体   English

Bootstrap 4 Custom NavBar for item active state

[英]Bootstrap 4 Custom NavBar for item active state

I know how to add a nav bar to the page.我知道如何在页面上添加导航栏。 However I am struggling with the active state.但是,我正在努力使用活跃的 state。 When an item is active (I am on that page) or hovered over the nav item should go from item to [item] also underlined.当一个项目处于活动状态(我在那个页面上)或悬停在导航项目上时,go 从项目到 [项目] 也应该加下划线。 So I need to know how to add an active & hover state that underlines the item and adds those brackets around it.所以我需要知道如何添加一个活动的 & hover state 来强调该项目并在其周围添加这些括号。

Thanks.谢谢。

although your description is quite vague I think I understand what you are trying to do.尽管您的描述很模糊,但我想我了解您要做什么。 I am assuming that when you click a navbar button you move to a different page.我假设当您单击导航栏按钮时,您将移动到不同的页面。

If you want a "nav-item" to appear active you simply have to give it the class "active"如果您希望“导航项”显示为活动状态,您只需为其指定 class “活动”

<li class="nav-item active">
    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>

In the case of changing item to [item], you can do this with CSS:在将 item 更改为 [item] 的情况下,您可以使用 CSS 执行此操作:

.example:before {
    content: "item";
}

.example:hover:before {
    content: "[item]";
}

Though using this method does require there to be no text in the link otherwise you'll end up with "item[item]" being displayed.尽管使用此方法确实需要链接中没有文本,否则您最终会显示“item [item]”。

I hope this helped.我希望这会有所帮助。 To learn more about bootstraps navbars, check here: https://getbootstrap.com/docs/4.0/components/navbar/要了解有关引导导航栏的更多信息,请查看此处: https://getbootstrap.com/docs/4.0/components/navbar/

Thanks for your question and welcome to the community.感谢您的提问,欢迎来到社区。 Going forward please share some code that you have come up with so others can use it to learn:).展望未来,请分享您提出的一些代码,以便其他人可以使用它来学习:)。

In regard to your problem, simply use an active class in CSS to change the state of your nav item.关于您的问题,只需在 CSS 中使用活动的 class 即可更改导航项目的 state。

To actually detect what page you are on will require a language.要实际检测您所在的页面将需要一种语言。 You could use PHP, see this existing post How to set current page "active" in php您可以使用 PHP,请参阅此现有帖子如何在 php 中设置当前页面“活动”

Hope this helps希望这可以帮助

HTML HTML

<ul class="nav">
  <li class="nav-item">
    <a class="nav-link active" href="#">Active</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#">Disabled</a>
  </li>
</ul>

CSS CSS

.nav-item {
  border: none;
  outline: none;
  padding: 10px 16px;
  background-color: #f1f1f1;
  cursor: pointer;
}

/* Style the active class (and on mouse-over) */
.active, .nav-item:hover {
  background-color: #666;
  color: white;
}

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

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