![CSS Selectors](https://codetofun.com/wp-content/themes/codetofun/images/large/css-selectors.webp)
CSS Properties
CSS max-height Property
![CSS max-height Property](https://codetofun.com/wp-content/themes/codetofun/images/large/css-max-height.webp)
Photo Credit to CodeToFun
π Introduction
The max-height
property in CSS is used to specify the maximum height of an element.
This property is particularly useful when you want to restrict the height of an element but still allow it to grow if necessary, depending on its content. It's often used in conjunction with other properties such as overflow to handle content that exceeds the maximum height.
π‘ Syntax
The syntax for the max-height
property is as follows:
element {
max-height: value;
}
Here, value can be a length (such as px, em, rem, vh, etc.), a percentage, or the keyword none.
ποΈ Default Value
The default value of the max-height
property is none, which means there is no maximum height restriction on the element.
π Property Values
Value | Description |
---|---|
length | Specifies a fixed height, such as 200px, 10em, 50vh. |
percentage | Specifies a height relative to the height of the containing element, such as 50%. |
none | No maximum height is applied, meaning the element can grow indefinitely. |
π Example
In this example, weβll restrict the maximum height of a div element to 200 pixels. If the content exceeds this height, it will be clipped or scrollable depending on the overflow property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS max-height Example</title>
<style>
.container {
max-height: 200px;
overflow: auto; /* Adds a scrollbar if content exceeds max-height */
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<h1>Container with Max Height</h1>
<div class="container">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
</div>
</body>
</html>
π₯οΈ Browser Compatibility
The max-height
property is well-supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. As with any CSS feature, itβs always a good practice to test across different browsers and devices to ensure consistent behavior.
π Conclusion
The max-height
property is a versatile tool for controlling the height of elements on your web page. By setting a maximum height, you can manage the layout of your content more effectively, especially in dynamic or responsive designs.
Use this property to ensure your web elements behave as expected and contribute to a better user experience.
π¨βπ» Join our Community:
Author
![author](https://codetofun.com/wp-content/themes/codetofun/images/mari-selvan-400.webp)
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS max-height Property), please comment here. I will help you immediately.