Definition
The max-width
CSS property is used to set the maximum width that an element can have. It restricts the width of the element to a specified value, preventing it from exceeding that limit.
The max-width
property accepts various length units, such as pixels (px
), percentages (%
), viewport width (vw
), or the none
keyword.
Here’s an example:
.container {
max-width: 600px;
}
In this example, the .container
class sets a maximum width of 600px
for the element. If the content inside the element expands beyond this width, it will be automatically truncated or wrapped based on the overflow
property.
You can also use other length units or percentage values to set a relative maximum width:
.container {
max-width: 80%;
}
In this case, the .container
class sets a maximum width of 80%
of its containing element’s width. This allows the element to adjust its width based on the available space.
The max-width
property is useful when you want to limit the width of an element, preventing it from growing beyond a certain point. It is commonly used to control the size and responsiveness of elements, ensuring they fit within their intended layout and viewport.