Skip to content Skip to sidebar Skip to footer

Links Inside List Items Have A Height Greater Than The List Element - What's Going On?

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 preceeding list item, making it hard to c

Solution 1:

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;*/
}

navli {
  margin: 10px;
  background-color: green;
}

nava {
  background-color: pink;
}
<nav><ul><li><ahref="projects.html">Projects</a></li><li><ahref="about.html">About</a></li><li><ahref="services.html">Services</a></li><li><ahref="ethical-design.html">Ethics</a></li><li><ahref="contact.html">Contact</a></li></ul></nav>

Solution 2:

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.

Solution 3:

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:

navli {
    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";
}

navli {
    background-color: green;
    overflow: hidden;
}

nava { 
    background-color: pink;  
}

navli:hovera{ 
    background-color: yellow;  
}
<nav><ul><li><ahref="projects.html">Projects</a></li><li><ahref="about.html">About</a></li><li><ahref="services.html">Services</a></li><li><ahref="ethical-design.html">Ethics</a></li><li><ahref="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:

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

Answer : 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";
}

navli {
    background-color: green;
    overflow: hidden;
}

nava {
  background-color: pink;
  display: block;
  margin-top: -20px;
  padding-bottom: 20px;
  border:1px solid blue;
}

navli:hovera{ 
    background-color: yellow;  
}
<nav><ul><li><ahref="projects.html">Projects</a></li><li><ahref="about.html">About</a></li><li><ahref="services.html">Services</a></li><li><ahref="ethical-design.html">Ethics</a></li><li><ahref="contact.html">Contact</a></li></ul></nav>

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

Post a Comment for "Links Inside List Items Have A Height Greater Than The List Element - What's Going On?"