如何在鼠标悬停时突出显示
  • 元素?
  • huangapple go评论90阅读模式
    英文:

    Need help personalizing my CSS navbar: how do I highlight the <li> element on mouseover?

    问题

    我正在尝试制作一个简单的导航栏,我已经创建了基础部分,但当我尝试个性化时遇到了困难。

    我的目标是当鼠标移到每个元素(home等)上时,它会突出显示像一个框,但目前只突出显示&lt;a&gt;,而不是包含它的&lt;li&gt;

    我正在尝试使&lt;li&gt;成为可以单击并突出显示的锚点,类似于stack的导航栏。

    .nav-top {
      background: rgb(151, 138, 63);
      margin: 0;
      padding: 1rem 0;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
    
    .nav-item {
      list-style: none;
      margin-right: 1.2rem;
      padding: 5px 10px;
    }
    
    .nav-top ul {
      margin: 0 auto;
      padding: 0;
      list-style: none
    }
    
    .nav-top li:hover {
      display: block
    }
    
    .nav-top a:hover {
      background: #F2F2F2;
      color: #444444;
    }
    
    .nav-item a {
      text-decoration: none;
      color: white;
      width: 100px;
    }
    
    .nav-item:first-child {
      margin-right: auto;
      margin-left: 2rem;
    }
    
    .nav-item a:hover {
      color: aqua;
    }
    
    <nav>
      <ul class="nav-top">
        <li class="nav-item"><label class="logo">LoremX</label></li>
        <li class="nav-item"><a href="index.html">Home</a></li>
        <li class="nav-item"><a href="#">About</a></li>
        <li class="nav-item" id="contact"><a href="#">Contact</a></li>
      </ul>
    </nav>
    
    英文:

    I am trying to make a simple navbar I managed to make the base but when I try to personalize I am getting stuck.

    My objective is that when the mouse goes over each element(home, etc) it highlights like a box, but currently it only highlights the &lt;a&gt; I tried and not the &lt;li&gt; holding it.

    I'm trying to make the &lt;li&gt; an anchor that can be clicked and highlighted similar to stack's navbar.

    <!-- begin snippet: js hide: false console: true babel: false -->

    <!-- language: lang-css -->

    .nav-top {
      background: rgb(151, 138, 63);
      margin: 0;
      padding: 1rem 0;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
    
    .nav-item {
      list-style: none;
      margin-right: 1.2rem;
      padding: 5px 10px;
    }
    
    .nav-top ul {
      margin: 0 auto;
      padding: 0;
      list-style: none
    }
    
    .nav-top li:hover {
      display: block
    }
    
    .nav-top a:hover {
      background: #F2F2F2;
      color: #444444;
    }
    
    .nav-item a {
      text-decoration: none;
      color: white;
      width: 100px;
    }
    
    .nav-item:first-child {
      margin-right: auto;
      margin-left: 2rem;
    }
    
    .nav-item a:hover {
      color: aqua;
    }
    

    <!-- language: lang-html -->

    &lt;navbar&gt;
      &lt;ul class=&quot;nav-top&quot;&gt;
        &lt;li class=&quot;nav-item&quot;&gt;&lt;label class=&quot;logo&quot;&gt;LoremX&lt;/label&gt;&lt;/li&gt;
        &lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;index.html&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;nav-item&quot; id=&quot;contact&quot;&gt;&lt;a href=&quot;#&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/navbar&gt;
    

    <!-- end snippet -->

    答案1

    得分: 2

    你对 li 的唯一 :hover 声明是 display: block 的默认值,而颜色变化声明仅适用于 a。然而,我认为你试图实现的效果更好地通过将锚点设置为块级元素并添加填充来实现。

    与悬停效果无关,只是更正你的标记:

    • 你有 .nav-top ul 选择器,但示例中没有嵌套的 ul
    • 我怀疑你误用了 label 元素。
    • navbar 不是 HTML 元素,我怀疑你想要使用 nav
    .nav-top {
      display: flex;
      margin: 0;
      padding: 0;
      list-style-type: none;
      background: rgb(151, 138, 63);
    }
    
    .logo {
      margin-right: auto;
      margin-left: 2rem;
      padding: 5px 10px;
      align-self: center;
    }
    
    .nav-item {
      margin-right: 1.2rem;
      padding: 5px 10px;
    }
    
    .nav-item a {
      display: block;
      padding: .5em;
      text-decoration: none;
      color: white;
    }
    
    .nav-item a:hover {
      color: aqua;
      background: #F2F2F2;
    }
    
    <nav>
      <ul class="nav-top">
        <li class="logo">LoremX</li>
        <li class="nav-item"><a href="#">Home</a></li>
        <li class="nav-item"><a href="#">About</a></li>
        <li class="nav-item" id="contact"><a href="#">Contact</a></li>
      </ul> 
    </nav>
    
    英文:

    The only :hover declaration you have for li is the default value of display: block while the color change declarations are made only for a. However, the effect that I believe you are trying to achieve is better accomplished by making the anchors block-level with padding.

    Not related to the hover effect, just correcting your markup:

    • You have .nav-top ul selector but the example doesn't include a nested ul
    • I suspect that you are misusing the label element.
    • navbar is not an HTML element and I suspect you want nav

    <!-- begin snippet: js hide: false console: true babel: false -->

    <!-- language: lang-css -->

    .nav-top {
      display: flex;
      margin: 0;
      padding: 0;
      list-style-type: none;
      background: rgb(151, 138, 63);
    }
    
    .logo {
      margin-right: auto;
      margin-left: 2rem;
      padding: 5px 10px;
      align-self: center;
    }
    
    .nav-item {
      margin-right: 1.2rem;
      padding: 5px 10px;
    }
    
    .nav-item a {
      display: block;
      padding: .5em;
      text-decoration: none;
      color: white;
    }
    
    .nav-item a:hover {
      color: aqua;
      background: #F2F2F2;
    }
    

    <!-- language: lang-html -->

    &lt;nav&gt;
      &lt;ul class=&quot;nav-top&quot;&gt;
        &lt;li class=&quot;logo&quot;&gt;LoremX&lt;/li&gt;
        &lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;nav-item&quot;&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;nav-item&quot; id=&quot;contact&quot;&gt;&lt;a href=&quot;#&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt; 
    &lt;/nav&gt;
    

    <!-- end snippet -->

    答案2

    得分: 1

    在锚元素中添加填充和边框:

    .nav-item a {
      text-decoration: none;
      color: white;
      width: 100px;
      padding: 10px;
      border-radius: 10px;
    }
    
    英文:

    just add padding and a border in an anchor element

    .nav-item a {
      text-decoration: none;
      color: white;
      width: 100px;
      padding: 10px;
      border-radius: 10px;
    }
    

    huangapple
    • 本文由 发表于 2023年6月2日 12:45:06
    • 转载请务必保留本文链接:https://go.coder-hub.com/76387183.html
    匿名

    发表评论

    匿名网友

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定