Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does width:auto behave differently than height:auto?

Tags:

html

css

I don't get the auto value. If applied to height it will take on the child's height, but if applied to width it will take on the parent's width.

There are no MDN posts on the auto value itself, and Google yields "100% VS auto" hits rather than "width:auto VS height:auto" hits.

For my current needs I would like an element to expand to its child's width, but in general I wish to know what is the deal with auto.

 .divXS {
   width: 100px;
   height: 100px;
   background: green;
 }
 
 .divXXS {
   width: 50px;
   height: 50px;
   background: yellow;
 }
 
 .divSM {
   width: 200px;
   height: 200px;
 }
 
 #Father {
   background: blue;
   border: 3px solid #20295E;
 }
 
 #Mother {
   background: pink;
   border: 3px solid #DB6DBE;
 }
 
 #Daughter {
   width: 100%;
   height: 100%;
 }
 
 #Son {
   width: auto;
   height: auto;
 }
<div class="divSM" id="Mother">
  <div class="divXS" id="Daughter">
    <div class="divXXS" id="grandDaughter"></div>
  </div>
</div>

<div class="divSM" id="Father">
  <div class="divXS" id="Son">
    <div class="divXXS" id="grandSon"></div>
  </div>
</div>

jsFiddle / jsBin

like image 704
Bar Akiva Avatar asked May 01 '16 16:05

Bar Akiva


People also ask

How does width auto work?

Width: auto The initial width of block-level elements like <div> or <p> is auto , which makes them take the full horizontal space of their containing block. When an element has auto as a value for width, it can have margin, padding, and border without becoming bigger than its parent element.

What is the difference between height auto and height 100%?

height: 100% gives the element 100% height of its parent container. height: auto means the element height will depend upon the height of its children.

What is the difference between width auto and width 100%?

width: auto; will try as hard as possible to keep an element the same width as its parent container when additional space is added from margins, padding, or borders. width: 100%; will make the element as wide as the parent container. Extra spacing will be added to the element's size without regards to the parent.

How does height auto work?

If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.


1 Answers

'auto' doesn't even always behave the same way for the width property and the height property respectively. It can have different behaviors for the width property not only depending on the element's display type, but also depending on the value of other properties for the same display type. That's why it's called 'auto' — from my answer here,

The value of said property is adjusted automatically according to the content or the context of the element.

Based on your description I'm assuming your question is in the context of block layout. Block layout consists of a series of block-level boxes stacked vertically in normal flow.

So a block container box, by default, only has to grow tall enough to contain its descendants stacked vertically. And since block-level boxes never stack horizontally in normal flow, there's no reason they can't stretch to the full width of their containing block. (They don't even need to shrink to accommodate floats in the same block formatting context, though the line boxes inside them do, but that's a separate topic altogether.)

And that's why, in block layout, an auto height is based on the total height of descendants and an auto width is based on the containing block width.

like image 115
BoltClock Avatar answered Oct 27 '22 00:10

BoltClock