The community is working on translating this tutorial into Turkish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
Minimum and maximum width/height
So far, we have seen how we can give an element relative and absolute sizes, by using the width and height properties. However, another possibility exist: Specifying minimum and/or maximum sizes. For this purpose, four properties exist: min-height, max-height, min-width and max-width. Their purpose should be easy to defer from their names, but how and when to use them might not be equally clear.
Specifying minimum and/or maximum sizes for an element allows you to take advantage of the fluid nature of elements on a webpage, allowing a certain element to expand and subtract within a limited set of dimensions, instead of defining a constant and absolute size like we did with the height and width properties. Just like width and height, the min-* and max-* properties allows you to specify an absolute or relative length unit or a percentage based size.
Minimum width and height
Using the min-width and min-height properties, you can define the smallest possible size that an element can have. If the element doesn't have width and/or height defined (auto), or if these values are smaller than the defined minimum height and width, the minimum height and width values will overrule it. This can easily be illustrated by an example:
<style type="text/css">
.box {
width: 50px;
height: 50px;
background-color: DarkSeaGreen;
padding: 10px;
margin: 20px;
float: left;
}
</style>
<div class="box">
Box 1 - Default
</div>
<div class="box" style="min-height: 80px; min-width: 80px;">
Box 2 - Minimum height and width
</div>
<div class="box" style="min-height: 80px; min-width: 80px;">
Box 3 - Minimum height and width. Only expands to 80px.
</div>
<div class="box" style="min-height: 80px; min-width: 80px; height: auto;">
Box 4 - Minimum height and width. Only expands to 80px, unless no max-height/height have been specified (auto)
</div>
If you test the example, notice how the second box is bigger by default, even though width and height is originally set to 50px - in this case, the min-width and min-height values take precedence.
Now look at the third box. It has more content than the second, and while the minimum values would normally allow it to grow, the fact that it has now outgrown both its height and min-height values caps it at the biggest value of the two (80px). If we want to allow it to grow beyond this, the height and/or width should not be defined. We illustrated this with the fourth box, where we reset the height value to auto, which is the default value, if it wasn't set by the .box selector - with that in place, the element is now allowed to grow vertically to make room for the content.
Maximum width and height
Sometimes you want to restrain an element to a specific width and/or height. Obviously this can be done with the width and height properties, but this will force the element to stay at this size all of the time - no matter if the content requires it or not. By using the max-width and/or max-height properties instead, you can allow an element to be whatever size it requires to fit the content and then grow along with the content up until a certain point.
Here's an example where we use max-height and max-width:
<style type="text/css">
.box {
background-color: DarkSeaGreen;
padding: 10px;
margin: 20px;
float: left;
}
</style>
<div class="box">
Box 1 - Default
</div>
<div class="box" style="max-height: 100px; max-width: 100px;">
Box 2 - Maximum height and width
</div>
<div class="box" style="max-height: 100px; max-width: 100px;">
Box 3 - Maximum height and width, this time with more content
</div>
Notice how each of the three boxes act differently, depending on whether or not max values are specified and how much space the content requires.
Summary
The max and min width/height properties gives you an extra layer of control in addition to the regular width and height properties. Please be aware that if both min-height, max-height and height are specified, min-height takes precedence over both, while max-height only takes precedence over height. The exact same is true for the width-related properties.