📅  最后修改于: 2023-12-03 14:39:33.455000             🧑  作者: Mango
In Bootstrap 4.5, you can easily set a maximum height for an HTML element using the .max-height
class. This class can be used on any element, including divs, images, and tables.
To set a maximum height for an element, simply add the .max-height
class, followed by the maximum height value in pixels or any other valid CSS unit.
<div class="max-height-100">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
This will set the maximum height of the div to 100 pixels.
<img class="max-height-50" src="image.jpg" alt="Image">
This will set the maximum height of the image to 50 pixels.
<table class="max-height-200">
<tbody>
<tr>
<td>Row 1 Column 1</td>
<td>Row 1 Column 2</td>
</tr>
<tr>
<td>Row 2 Column 1</td>
<td>Row 2 Column 2</td>
</tr>
</tbody>
</table>
This will set the maximum height of the table to 200 pixels.
You can also use the .mh-
prefix shorthand to achieve the same effect.
<div class="mh-100">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
<img class="mh-50" src="image.jpg" alt="Image">
<table class="mh-200">
<tbody>
<tr>
<td>Row 1 Column 1</td>
<td>Row 1 Column 2</td>
</tr>
<tr>
<td>Row 2 Column 1</td>
<td>Row 2 Column 2</td>
</tr>
</tbody>
</table>
Bootstrap 4.5 also provides responsive classes for setting maximum height based on the screen size. The format for responsive classes is .max-height-{breakpoint}-{value}
or .mh-{breakpoint}-{value}
.
<div class="max-height-md-100">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
This will set the maximum height of the div to 100 pixels on medium screens and above.
<img class="mh-lg-50" src="image.jpg" alt="Image">
This will set the maximum height of the image to 50 pixels on large screens and above.
<table class="max-height-sm-200">
<tbody>
<tr>
<td>Row 1 Column 1</td>
<td>Row 1 Column 2</td>
</tr>
<tr>
<td>Row 2 Column 1</td>
<td>Row 2 Column 2</td>
</tr>
</tbody>
</table>
This will set the maximum height of the table to 200 pixels on small screens and above.
With the .max-height
and .mh-
classes in Bootstrap 4.5, it's easy to set a maximum height for HTML elements. Responsive classes make it even more powerful, allowing for fine-grained control over maximum height based on screen size.