What is min-height,min-width,max-height,max-width properties of CSS?

What is min-height,min-width,max-height,max-width properties of CSS?

CSS (Cascading Style Sheets) is a powerful language used for styling and formatting web pages. When it comes to controlling the height and width of HTML elements, CSS provides several properties, such as min-height, min-width, max-height, and max-width, that can be used to set minimum and maximum values for an element's height and width.

In this article, we will discuss these four properties in detail and see how they can be used to create responsive and flexible layouts.

1). min-height

The min-height property is used to set the minimum height of an element. If an element's content height is less than the specified min-height value, the element will expand to the minimum height value. On the other hand, if the element's content height is greater than the specified min-height value, the element will expand to fit the content.

Let's take a look at the following example code:

<!DOCTYPE html>
<html>
<head>
    <title>min-height example</title>
    <style>
        .container {
            min-height: 300px;
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <div class="container">
        <p>This is a paragraph.</p>
        <p>This is another paragraph.</p>
        <p>This is a third paragraph.</p>
    </div>
</body>
</html>

In this example, we have set the min-height of the container element to 300px. As you can see, the container element expands to fit the content when the content is greater than 300px, but it does not shrink below the minimum height of 300px when the content is less than 300px.

Output:

2). min-width

Similar to the min-height property, the min-width property is used to set the minimum width of an element. If an element's content width is less than the specified min-width value, the element will expand to the minimum width value. If the element's content width is greater than the specified min-width value, the element will expand to fit the content.

Here's an example code that demonstrates the use of the min-width property:

<!DOCTYPE html>
<html>
<head>
    <title>min-width example</title>
    <style>
        .container {
            min-width: 300px;
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <div class="container">
        <p>This is a paragraph.</p>
        <p>This is another paragraph.</p>
        <p>This is a third paragraph.</p>
    </div>
</body>
</html>

In this example, we have set the min-width of the container element to 300px. As you can see, the container element expands to fit the content when the content is greater than 300px, but it does not shrink below the minimum width of 300px when the content is less than 300px.

Output

3).max-height

The max-height property is used to set the maximum height of an element. If an element's content height is greater than the specified max-height value, the element will be truncated to the maximum height value. If the element's content height is less than the specified max-height value, the element will expand to fit the content.

Here's an example code that demonstrates the use of the max-height property:

<!DOCTYPE html>
<html>
<head>
    <title>max-height example</title>
    <style>
        .container {
             max-height:250px;
             overflow: auto;
             background-color: lightblue;   
            }
    </style>
<head>
<body>
    <div class="container">
            <p>This is a paragraph.</p>
            <p>This is another paragraph.</p>
            <p>This is a third paragraph</p>
            <p>This is a fourth paragraph</p>
            <p>This is a fifth paragraph</p>
            <p>This is a sixth paragraph</p>
            <p>This is a seventh paragraph</p>
            <p>This is a eighth paragraph</p>
            <p>This is a ninth paragraph</p>
            <p>This is a tenth paragraph</p>
        </div>
</body>
</html>

In this example, we have set the max-height of the container element to 250px. As you can see, when the content height is greater than 250px, a vertical scrollbar appears on the container element, and the content is truncated to the maximum height of 250px.

Output:

4). max-width

Finally, the max-width property is used to set the maximum width of an element. If an element's content width is greater than the specified max-width value, the element will be truncated to the maximum width value. If the element's content width is less than the specified max-width value, the element will expand to fit the content.

Here's an example code that demonstrates the use of the max-width property:

<!DOCTYPE html>
<html>
<head>
    <title>max-width example</title>
    <style>
        .container {
            max-width: 500px;
            overflow: auto;
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <div class="container">
        <p>This is a long paragraph that will cause the container to exceed its maximum width and display a horizontal scrollbar.</p>
    </div>
</body>
</html>

In this example, we have set the max-width of the container element to 500px. As you can see, when the content width is greater than 500px, a horizontal scrollbar appears on the container element, and the content is truncated to the maximum width of 500px.

The output :

Conclusion

In conclusion, the min-height, min-width, max-height, and max-width properties are powerful tools that can be used to control the height and width of HTML elements and create flexible and responsive layouts. By using these properties, you can ensure that your website looks great on any screen size or device.

Did you find this article valuable?

Support My Actual Coding Journey by becoming a sponsor. Any amount is appreciated!