CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



max-height

Max-height is a CSS property that allows you to set the maximum height of an element. This property is useful when you want to limit the height of an element, but still allow it to expand if necessary. In this article, we will discuss the max-height property in detail and provide some code examples to help you understand how it works.

How Max-Height Works

The max-height property sets the maximum height of an element. If the content inside the element is taller than the max-height value, the overflow property will come into play. The overflow property determines what happens when the content overflows the element's boundaries. By default, the overflow property is set to "visible", which means that the content will overflow the element and be visible outside of it. However, you can set the overflow property to "hidden", "scroll", or "auto" to control how the content is displayed when it overflows the element.

Here is an example of how to use the max-height property:

<div style="max-height: 200px; overflow: auto;">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, velit eget bibendum bibendum, velit elit bibendum elit, vel bibendum elit elit vel elit.</p>
  <p>Sed euismod, velit eget bibendum bibendum, velit elit bibendum elit, vel bibendum elit elit vel elit.</p>
  <p>Velit elit bibendum elit, vel bibendum elit elit vel elit.</p>
</div>

In this example, we have set the max-height property to 200 pixels and the overflow property to "auto". This means that if the content inside the div is taller than 200 pixels, a scrollbar will appear so that the user can scroll through the content.

Using Max-Height with Images

One common use of the max-height property is to limit the height of images. This is useful when you have images of different sizes and you want them all to be the same height. Here is an example:

<img src="example.jpg" style="max-height: 200px;">

In this example, we have set the max-height property to 200 pixels. This means that the image will be no taller than 200 pixels, but it can be any width. If the image is taller than 200 pixels, it will be scaled down to fit within the 200-pixel height limit.

Using Max-Height with Tables

The max-height property can also be used with tables to limit the height of a table. Here is an example:

<table style="max-height: 200px; overflow: auto;">
  <tr>
    <td>Lorem ipsum dolor sit amet</td>
    <td>consectetur adipiscing elit</td>
  </tr>
  <tr>
    <td>Sed euismod</td>
    <td>velit eget bibendum bibendum</td>
  </tr>
  <tr>
    <td>Velit elit bibendum elit</td>
    <td>vel bibendum elit elit vel elit</td>
  </tr>
</table>

In this example, we have set the max-height property to 200 pixels and the overflow property to "auto". This means that if the content inside the table is taller than 200 pixels, a scrollbar will appear so that the user can scroll through the content.

Conclusion

The max-height property is a useful CSS property that allows you to set the maximum height of an element. This property is particularly useful when you want to limit the height of an element, but still allow it to expand if necessary. By using the max-height property, you can ensure that your content is displayed in a consistent and visually appealing way.

References

Activity