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)?
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With