Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an <a> 100% height of <li>?

Tags:

css

ul {
    display: table-row;
}
    
li {
    width: 100px;
    border: 1px solid black;
    display: table-cell;
}

a {
    display: block;
    background-color: yellow;
    height: 100%;
}
<ul>
    <li>
        <a href="#">Test</a>
    </li>
    <li>
        Test  
        Test
        Test
        Test            
    </li>
</ul>

This answer explains how to make <li>s the same height, but now how can I make an <a> fill the entire height of its li (without using a fixed height)?

like image 352
mpen Avatar asked Apr 21 '15 18:04

mpen


People also ask

How can I set my height to 100%?

Try setting the height of the html element to 100% as well. Body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well. However the content of body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.


Video Answer


2 Answers

You are dealing with tables, and table height values are used as a minimal value when computing table and table-cell heights.

The easiest fix is to provide a height value to the CSS table parent block. First, to the ul, apply display: table instead of table-row, and then specify height: 1px and any non-zero value will work. Remember to zero out margins and padding (due to the ul default settings).

Make sure that the CSS table-cell element has height: 100%, and then the a element will take on the height (since it is displayed as a block).

Note: If you set the top level height to 100%, this will work in Chrome but fail in IE.

ul {
  display: table;
  height: 1px; /* any small value will work */
  margin: 0;
  padding: 0;
}
li {
  width: 100px;
  border: 1px solid black;
  display: table-cell;
  height: 100%;
}
a {
  display: block;
  background-color: yellow;
  height: 100%;
}
<ul>
  <li>
    <a href="#">Test</a>
  </li>
  <li>
    Test Test Test Test
  </li>
</ul>
like image 132
Marc Audet Avatar answered Oct 20 '22 00:10

Marc Audet


Use inline-block rather than block. You'll now need to set width, as well.

ul {
  display: table-row;
  height: 0;
}

li {
  width: 100px;
  border: 1px solid black;
  display: table-cell;
  height: 100%;
}

a {
  display: inline-block;
  background-color: yellow;
  height: 100%;
  width: 100%;
}
<ul>
  <li>
    <a href="#">Test</a>
  </li>
  <li>
    Test Test Test Test
  </li>
</ul>
like image 20
Paul Roub Avatar answered Oct 19 '22 22:10

Paul Roub