What does height:auto do in CSS?

  • Context: HTML/CSS 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Css
Click For Summary
SUMMARY

The discussion clarifies that the CSS property height: auto automatically adjusts the height of a block element based on its content. It is the default value for all block elements, including forms, making explicit declarations of height: auto unnecessary in most cases. However, if a different height value is set, reverting to height: auto may require JavaScript or more specific CSS selectors. The trend in 2022 is to minimize JavaScript usage for styling purposes.

PREREQUISITES
  • Understanding of CSS box model concepts
  • Familiarity with block and inline elements in HTML
  • Basic knowledge of CSS properties and their default values
  • Awareness of JavaScript for DOM manipulation
NEXT STEPS
  • Research CSS box model and its impact on layout
  • Learn about CSS specificity and how it affects style application
  • Explore media queries for responsive design techniques
  • Investigate best practices for minimizing JavaScript in styling
USEFUL FOR

Web developers, front-end designers, and anyone looking to optimize CSS for responsive layouts and efficient styling practices.

shivajikobardan
Messages
637
Reaction score
54
TL;DR
height:auto
https://stackoverflow.com/questions/15943009/difference-between-css-height-100-vs-height-auto
I read the accepted answer and it doesn’t make much sense to me in practical applications.

Here’s my codepen.
What does height:auto does here?

As per my research, height:auto fits content. Ie As much content, that much height.
Also height:auto is default for every block element and form is a block element, why is it necessary to add height:auto here?
 
Technology news on Phys.org
You don't have to set height to auto because it is the default value for that property.

But if you set it to something else, it is possible that you would need to change it back to its default value with Javascript.
 
jack action said:
change it back to its default value with Javascript.
Or a selector with more specificity, or a media query. In 2022 we tend not to use JS for look-and-feel.
 
  • Like
Likes   Reactions: jack action

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K