What is the meaning of the `auto` value in a CSS property.

What is the meaning of the CSS auto property value. What happens when the CSS property value is set to auto ?

+54
css
Dec 17 '10 at 15:14
source share
3 answers

The value of the specified property is automatically configured according to the content or context of the element.

For example, a block level element with height: auto will grow taller since it contains more text. In another example, a block element with margin: 0 auto will increase the left and right margins until it becomes centered along the y axis of the viewport.

It really depends on the property you give the value to, different properties behave differently depending on the content and context.

+58
Dec 17 '10 at 15:16
source share

automatically means automatic addition. The most common reason I use auto:

 margin: 0 auto; 

to center the element.

Please note: if the size is not announced, it will not work.

Example 1: div is not centered, auto does not work

 <style> .cont { margin: 0 auto; } </style> <div class="cont"></div> 



Example 2: a div is centered on a page

 <style> .cont { width: 1000px; margin: 0 auto; } </style> <div class="cont"></div> 
+9
Mar 21 '13 at 0:19
source share

It really depends on the attribute you are using. For example, the auto block width value will expand the full space of its parent element. But setting the block height automatically expands the required space of its contents.

 <style> #outer{ width: 500px; height: 500px; border: solid 2px black; } #inner{ width: auto; height: auto; background-color: aqua; } </style> <div id="outer"> <div id="inner">content</div> </div> 
0
May 27 '16 at 6:24
source share



All Articles