The Wayback Machine - https://web.archive.org/web/20250323041449/https://www.geeksforgeeks.org/css-height-property/
Open In App

CSS height Property

Last Updated : 27 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The height property is used to set the height of an element. The height property does not contain padding and margin and border of the element. 

Syntax:  

height: auto|length|initial|inherit;

Default Value: auto 

Property Values: 

  • auto: It is used to set the height property to its default value. If the height property is set to auto then the browser calculates the height of the element.
  • length: It is used to set the height of the element in the form of px, cm, etc. The length can not be negative.
  • initial: It is used to set the height property to its default value. 
  • inherit: It is used to set the height property from its parent element.

Example: In this example, we are using height: auto;

html
<!DOCTYPE html>
<html>
<head>
    <title>
        CSS height Property
    </title>

    <style>
        .Geeks {
            height: auto;
            color: white;
            font-size: 30px;
            background-color: green;
        }
    </style>
</head>

<body>
    <h2>CSS height Property</h2>

    <div class="Geeks">
        GeeksforGeeks: A computer science portal
    </div>
</body>
</html>

Output: 

CSS_height property numeric value

Example: In this example, we are using height: length; property.

html
<!DOCTYPE html>
<html>
<head>
    <title>
        CSS height Property
    </title>

    <style>
        .Geeks {
            height: 100px;
            color: white;
            font-size: 30px;
            background-color: green;
        }
    </style>
</head>

<body>
    <h2>CSS height Property</h2>

    <div class="Geeks">
        GeeksforGeeks: A computer science portal
    </div>
</body>
</html>

Output: 

CSS height property

Example: In this example, we are using height: initial; property.

html
<!DOCTYPE html>
<html>
<head>
    <title>
        CSS height Property
    </title>

    <style>
        .Geeks {
            height: initial;
            color: white;
            font-size: 30px;
            background-color: green;
        }
    </style>
</head>

<body>
    <h2>CSS height Property</h2>

    <div class="Geeks">
        GeeksforGeeks: A computer science portal
    </div>
</body>
</html>

Output: 

CSS height property auto

Supported Browsers: The browser supported by height property are listed below:  

  • Google Chrome 1.0 and above
  • Edge 12.0 and above
  • Internet Explorer 4.0 and above
  • Firefox 1.0 and above
  • Safari 1.0 and above
  • Opera 7.0 and above

CSS height Property – FAQs

What does the height property do in CSS?

The height property sets the height of an element’s content area. You can define it in various units like pixels, percentages, viewport units (vh), or using flexible methods like auto.

How do I set an element’s height using CSS?

You can set height like this: height: 300px;, which sets the element’s height to 300px. For responsive designs, you might use percentages or viewport units, like height: 50vh; .

What happens if the content overflows the height set?

If the content exceeds the specified height, it will overflow the container unless you manage it with properties like overflow, which can control scrolling or clipping behavior.

Can I use min-height and max-height with the height property?

Yes, min-height and max-height are useful for creating flexible layouts. You can ensure that an element has a minimum or maximum height while still allowing the height to adjust dynamically within those bounds.

How does the height property interact with flexbox or grid layouts?

In flexbox and grid layouts, the height property can be influenced by the container’s behavior. For example, a flex container might stretch items to fill available space, affecting their height.



Next Article

Similar Reads

three90RightbarBannerImg