1

I am trying to understand the following

  1. Height: auto; What does this do?
  2. Height: 100%; What does this do?
  3. What is the difference betweem 1 and 2 ? Any examples to explain the difference ?

Thanks in advance!

Spiderweb
  • 85
  • 2
  • 6

3 Answers3

6

height: auto; means, the height of the element will increase according to the content it holds, if you assign fixed height, the content overflows, so when you don't know that that your element will contain how much, you set it to auto, so the height will auto increase.

When you set height: 100%; so it will take entire vertical space of the container element, so say for example, when the container element is 200px in height, and you use height: 100%; on the child element, it will be height: 100%; of the container element = 200px.

By default, the element's height is always set to auto unless and until you specify the height using px % or any other unit.

Demo (height: auto;) Keep adding content and your element will expand vertically.

Demo 2 (height: 100%;), this will behave just like you are setting some fixed height to your element, if the content increases, it will overflow. This method only comes handy where you want your child element to take 100% vertical space of the parent container.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
2
  1. height:100%: implies the element is going to have the 100% height of its parent container.

  2. height:auto: means, the element will have flexible height i.e. its height will depend upon the height of children elements of it

  3. Click here for difference with code

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • As an example, if I were to add `margin:5px` to a div with `height:100px`, it would stretch out of its bounds and do 105px. With `height:auto`, it would take the 5px of margin out of the height instead, keeping it at 100px. – Scott Rowell Oct 18 '13 at 05:34
0

reference:http://www.w3.org/TR/CSS2/visudet.html#the-height-property

Content height: the 'height' property

  <percentage>

Specifies a percentage height. The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'. A percentage height on the root element is relative to the initial containing block. Note: For absolutely positioned elements whose containing block is based on a block-level element, the percentage is calculated with respect to the height of the padding box of that element. This is a change from CSS1, where the percentage was always calculated with respect to the content box of the parent element.

auto

The height depends on the values of other properties. See the prose below. Note that the height of the containing block of an absolutely positioned element is independent of the size of the element itself, and thus a percentage height on such an element can always be resolved. However, it may be that the height is not known until elements that come later in the document have been processed.

Negative values for 'height' are illegal.

For example, the following rule sets the content height of paragraphs to 100 pixels:

p { height: 100px }

Paragraphs of which the height of the contents exceeds 100 pixels will overflow according to the 'overflow' property.

Sobin Augustine
  • 3,639
  • 2
  • 25
  • 43