Links inside list items have a height greater than the list element - what's going on?

后端 未结 3 390
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 08:33

I have a nav containing a list of links. The list has a line-height: 1em. However the links have a height greater than 1em and overlap the

相关标签:
3条回答
  • 2021-01-27 08:48

    Just add display: inline-block to your a elements.

    Anchor tags are naturally inlined by user agent stylesheets which is what's causing your overflow.

    0 讨论(0)
  • 2021-01-27 09:06

    The problem is with the line-height in your nav, its not giving any space between the lines ()line-height: 1em is only allocating the same as the font-size (50px) so there is no room for the default space around the letters). You can make line-height larger (1.1em will works with your code above):

    nav { line-height: 1.1em; } 
    

    Or just remove it altogether so it uses the default.

    UPDATE:

    If you cannot change the line-height from 1em, There are 2 fundamental problems that are causing issues to achieve this:

    1. a tags are inline by default which makes it harder to work with margins & padding etc.
    2. most fonts have extra space above and below so that the ascenders and descenders don't touch - this is down to the font glyphs themselves. Some fonts are "worse" than others.

    You could force the link not to overflow outside the li using the following, and it will prevent the effect you see where the mouse looks like its over one link but actually activates another:

    nav li {
        background-color: green;
        overflow: hidden;        /* this will crop off anything outside the element */
    }
    

    However depending on the font, this could crop a tiny part off the descenders of the letters.

    Working snippet:

    ul {
      margin: 0;
        padding: 0;
        border: 0;
        vertical-align: top;
        list-style: none;
    }
    
    nav {
        height: 100%;
        overflow: hidden;
        position: absolute;
        left: 0;
        right: 0;
        line-height: 1em;
        font-size: 3rem;
        font-family: "Times New Roman";
    }
    
    nav li {
        background-color: green;
        overflow: hidden;
    }
    
    nav a { 
        background-color: pink;  
    }
    
    nav li:hover a{ 
        background-color: yellow;  
    }
    <nav>
        <ul>
            <li><a href="projects.html">Projects</a></li>
            <li><a href="about.html">About</a></li>
            <li><a href="services.html">Services</a></li>
            <li><a href="ethical-design.html">Ethics</a></li>
            <li><a href="contact.html">Contact</a></li>
        </ul>   
    </nav>

    There isn't an easy way around this without changing the line-height (even slightly), but I tried various hacks to see if we could move the link text up a couple of pixels without moving the active link.

    If it is possible for you to make the a to be display: block, then this seems to work:

    nav li {
        background-color: green;
        overflow: hidden;
    }
    nav a {
      background-color: pink;
      display: block;
      /* tweak the values below to suit */
      margin-top: -2px;
      padding-bottom: 2px;
    }
    

    Solution: Use overflow:hidden, negative margin and padding as workaround this

    The negative margin moves up the top of the link (which has the extra space) and the padding adds a little space for the descender. The òverflow:hidden on the li crops off the extra.

    You can see it working below - Note I have greatly exaggerated the margin and padding to ensure that it works with no overlap, and I added a border around the links to make it clear where the link was:

    ul {
      margin: 0;
        padding: 0;
        border: 0;
        vertical-align: top;
        list-style: none;
    }
    
    nav {
        height: 100%;
        overflow: hidden;
        position: absolute;
        left: 0;
        right: 0;
        line-height: 1em;
        font-size: 3rem;
        font-family: "Times New Roman";
    }
    
    nav li {
        background-color: green;
        overflow: hidden;
    }
    
    nav a {
      background-color: pink;
      display: block;
      margin-top: -20px;
      padding-bottom: 20px;
      border:1px solid blue;
    }
    
    nav li:hover a{ 
        background-color: yellow;  
    }
    <nav>
        <ul>
            <li><a href="projects.html">Projects</a></li>
            <li><a href="about.html">About</a></li>
            <li><a href="services.html">Services</a></li>
            <li><a href="ethical-design.html">Ethics</a></li>
            <li><a href="contact.html">Contact</a></li>
        </ul>   
    </nav>

    That's as good as I can come up with, hope one of those options is suitable!

    0 讨论(0)
  • 2021-01-27 09:15

    the weird thing you were doing is to set the font-size of nav which is parent of ul li to 10rem that had made them bigger and also line-height is different from the actual height just se here line-height

    example

    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    
    nav {
      height: 100%;
      overflow: hidden;
      position: absolute;
      top: 7.2rem;
      left: 0;
      right: 0;
      /* font-size: 10rem;*/
    }
    
    nav li {
      margin: 10px;
      background-color: green;
    }
    
    nav a {
      background-color: pink;
    }
    <nav>
      <ul>
        <li><a href="projects.html">Projects</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="services.html">Services</a></li>
        <li><a href="ethical-design.html">Ethics</a></li>
        <li><a href="contact.html">Contact</a></li>
      </ul>
    </nav>

    0 讨论(0)
提交回复
热议问题