:hover在元素上不起作用,它位于
  • 元素内部。
  • huangapple go评论50阅读模式
    英文:

    :hover is not working on <a> element. and its inside <li> element

    问题

    在<a>元素上添加悬停效果不起作用,可能是因为其父元素<li>。我不知道。

    我尝试在<a>元素内部的<li>元素上应用悬停效果(translateY),但不起作用。但如果我在<li>上应用悬停(translateY),它就起作用。有人可以告诉我问题在哪里吗?

    a {
      cursor: pointer;
      text-decoration: none;
      font-family: 'Ubuntu', sans-serif;
      font-size: 1.2rem;
      font-weight: bold;
      color: white;
      transition: all 0.3s ease-in;
    }
    
    a:hover {
      color: aquamarine;
      transform: translateY(-10px);
    }
    
    <nav id="navbar">
      <a id="nav-brand" href="">MrinB</a>
      <div id="div-items">
        <ul id="ul-items">
          <li class="li-items"><a id="a" href="">Home</a></li>
          <li class="li-items"><a id="a" href="">Contact</a></li>
          <li class="li-items"><a id="a" href="">About Me</a></li>
        </ul>
      </div>
    </nav>
    
    英文:

    adding hover effect on <a> element is not working. maybe because of its parent element &lt;li&gt;. i dont know

    i was trying to apply hover effect(translateY) on &lt;a&gt; element which is inside a &lt;li&gt; element.but is not working but if i apply hover(trasnlateY) on &lt;li&gt;. its works. can someone tell me whats the problem.

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

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

    a {
      cursor: pointer;
      text-decoration: none;
      font-family: &#39;Ubuntu&#39;, sans-serif;
      font-size: 1.2rem;
      font-weight: bold;
      color: white;
      transition: all 0.3s ease-in;
    }
    
    a:hover {
      color: aquamarine;
      transform: translateY(-10px)
    }
    

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

    &lt;nav id=&quot;navbar&quot;&gt;
      &lt;a id=&quot;nav-brand&quot; href=&quot;&quot;&gt;MrinB&lt;/a&gt;
      &lt;div id=&quot;div-items&quot;&gt;
        &lt;ul id=&quot;ul-items&quot;&gt;
          &lt;li class=&quot;li-items&quot;&gt;&lt;a id=&quot;a&quot; href=&quot;&quot;&gt; Home&lt;/a&gt;&lt;/li&gt;
          &lt;li class=&quot;li-items&quot;&gt;&lt;a id=&quot;a&quot; href=&quot;&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
          &lt;li class=&quot;li-items&quot;&gt;&lt;a id=&quot;a&quot; href=&quot;&quot;&gt;About Me&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;/nav&gt;
    

    <!-- end snippet -->

    答案1

    得分: 1

    a 标签上添加 display: inline-block

    a {
        cursor: pointer;
        text-decoration: none;
        font-family: 'Ubuntu', sans-serif;
        font-size: 1.2rem;
        font-weight: bold;
        color: black;
        display: inline-block;
        transition: all 0.3s ease-in;
    }
    

    这将在 a 标签上设置 display 属性为 inline-block

    英文:

    Add display-inline:block on a tag

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

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

    a{
    cursor: pointer;
    text-decoration: none;
    font-family: &#39;Ubuntu&#39;, sans-serif;
    font-size: 1.2rem;
    font-weight: bold;
    color: black;
    display:inline-block;
    transition: all 0.3s ease-in;}
    
    a:hover{
    color: aquamarine;
    transform: translateY(-10px)}
    

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

    &lt;nav id=&quot;navbar&quot;&gt;
      &lt;a id=&quot;nav-brand&quot; href=&quot;&quot;&gt;MrinB&lt;/a&gt;
      &lt;div id=&quot;div-items&quot;&gt;
        &lt;ul id=&quot;ul-items&quot;&gt;
          &lt;li class=&quot;li-items&quot;&gt;&lt;a id=&quot;a&quot; href=&quot;&quot;&gt; Home&lt;/a&gt;&lt;/li&gt;
          &lt;li class=&quot;li-items&quot;&gt;&lt;a id=&quot;a&quot; href=&quot;&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
          &lt;li class=&quot;li-items&quot;&gt;&lt;a id=&quot;a&quot; href=&quot;&quot;&gt;About Me&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;/nav&gt;
    

    <!-- end snippet -->

    huangapple
    • 本文由 发表于 2023年2月10日 15:08:05
    • 转载请务必保留本文链接:https://go.coder-hub.com/75407922.html
    匿名

    发表评论

    匿名网友

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

    确定