简体   繁体   English

顶部导航栏 hover 下划线效果

[英]Top navigation bar hover underline effect

For a project, I need to create the following navigation bar:对于一个项目,我需要创建以下导航栏:

导航栏

Like you can see the buttons of this navigation bar have mainly two specificities:就像你可以看到这个导航栏的按钮主要有两个特点:

  1. When we don't hover or active the navigation buttons, there is a 'grey underline' that takes the screen full width.当我们没有hoveractive导航按钮时,会有一个“灰色下划线”占据整个屏幕宽度。
  2. When we hover or active a button, the corresponding button's color and underline become blue.当我们hoveractive一个按钮时,相应按钮的颜色和下划线变为蓝色。

To be honest, I'm completely lost about how to implement these two specificities.老实说,我完全不知道如何实现这两个特性。

At first, I tried to style the by default underline of the .nav-button ( text-decoration-offset ) and also their border-bottom , but I always have the two following problems:起初,我尝试设置.nav-button ( text-decoration-offset ) 的默认underline以及它们的border-bottom ,但我总是遇到以下两个问题:

  1. My underline / border-bottom don't take the full width of the screen.我的underline / border-bottom没有占据屏幕的整个宽度。
  2. I can't limit where the hover / active effect on the line begins and end.我无法限制线路上hover / active效果的开始和结束位置。

Then I tried to style the border-bottom of #nav-menu , but I again faced two problems:然后我尝试设置#nav-menuborder-bottom样式,但我又遇到了两个问题:

  1. Like the first time, I couldn't limit where the hover / active effect on the line begins and end.像第一次一样,我无法限制hover / 线路上的active效果开始和结束的位置。
  2. #nav-menu is the container of my buttons, it means that when I hover or active on one of them, I can't affect their parent, in our case #nav-menu ( Stack Overflow ). #nav-menu是我的按钮的容器,这意味着当我hover或在其中一个上active时,我不能影响它们的父级,在我们的例子中是#nav-menu ( Stack Overflow )。 And since I'm allowed to use only HTML and CSS , this second method can't be used.而且由于我只允许使用 HTML 和 CSS ,因此不能使用第二种方法。

Here is my code:这是我的代码:

 #nav-menu { display: flex; flex-flow: row wrap; justify-content: space-evenly; }.nav-button { color: black; font-size: 18px; padding-top: 16px; text-decoration: none; cursor: pointer; }.nav-button:hover { color: #0065FC; }
 <nav id="nav-menu"> <a class="nav-button">Accommodations</a> <a class="nav-button">Activities</a> </nav>

I really thank in advance anybody who will be kind enough to try to help me:D.我真的提前感谢任何愿意帮助我的人:D。

You have to add flex: 1 to get full width for .nav-button您必须添加flex: 1以获得.nav-button全宽

 #nav-menu { display: flex; flex-flow: row wrap; justify-content: space-evenly; }.nav-button { color: black; font-size: 18px; padding: 16px; text-decoration: none; cursor: pointer; border-bottom: 2px solid #f2f2f2; flex: 1; text-align: center }.nav-button:hover { color: #0065FC; border-bottom: 2px solid #3465fc; }
 <nav id="nav-menu"> <a class="nav-button">Accommodations</a> <a class="nav-button">Activities</a> </nav>

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

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