Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<li> will not take 100% height of the parent <ul>

Tags:

html

css

height

I have a <ul> with several <li> items in it all in a single row. The <li> has a nested <span> and an <img>. The <img> height is all the same across all items. But the <span> items contain text which can span on a single line or two lines (depends on text).

I have tried appying display:block, float:left, height:100%, and few other suggestions I found, but the shorter <li> items will not take the same height as the taller <li> items. So I am left with a space below the shorter <li> items. Anyone has any suggestions on this?

like image 838
KingsInnerSoul Avatar asked Jun 26 '13 13:06

KingsInnerSoul


People also ask

How do you set height to 100% of a parent?

Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.

What is height fit content in CSS?

The fit-content behaves as fit-content(stretch) . In practice this means that the box will use the available space, but never more than max-content . When used as laid out box size for width , height , min-width , min-height , max-width and max-height the maximum and minimum sizes refer to the content size.

How height works in CSS?

The height CSS property specifies the height of an element. By default, the property defines the height of the content area. If box-sizing is set to border-box , however, it instead determines the height of the border area.


2 Answers

An easy fix is to add display:table on your ul element. The hieght will align with the content.

like image 38
Claude Ricke Avatar answered Sep 20 '22 10:09

Claude Ricke


In this case, when you set height:100% it isn't inheriting any height from its parent. If you want the list items to have 100% height of the div #wrapper, then you should set the ul's height to 100% and set a height on the div #wrapper in pixels or em's:

http://jsfiddle.net/SF9Za/1/

#wrapper {
    background: transparent;
    width: 350px;
    color: white;
    height:250px;
}
#wrapper ul {
    list-style-type: none;
    display: block;
    float: left;
    background: green;
    height:100%;
}

If you'd rather have it stretch to the full height of the browser window, then you need to set the height of html, body in your css to 100%, and then all of the elements down to the li (html, body, div#wrapper, ul.list, and li) must have 100% height:

http://jsfiddle.net/YdGra/

html, body{
    height:100%;
    margin:0;
    padding:0;
}
#wrapper {
    background: transparent;
    width: 350px;
    color: white;
    height:100%;
}
#wrapper ul {
    list-style-type: none;
    display: block;
    float: left;
    background: green;
    height:100%;
}

Here's some other links that you might want to check out that talk about this:

  • CSS 100% height layout
  • Setting height: 100% on my label element doesn't work
  • http://webdesign.about.com/od/csstutorials/f/set-css-height-100-percent.htm
like image 124
Joe_G Avatar answered Sep 19 '22 10:09

Joe_G