The CSS max-width
property sets the maximum width of an element. It ensures that the element does not exceed a specified width, even if the content is larger. This is useful for responsive design and controlling element dimensions.
Setting a fixed maximum width for an element:
.max-width-example {
max-width: 300px;
background-color: #d0f0c0;
padding: 10px;
border: 1px solid #ddd;
}
Using max-width: 100%
for responsive images:
.responsive-image {
max-width: 100%;
height: auto;
}
This image will scale down to fit its container but will not exceed its original dimensions.
Applying max-width
to a container to limit its width:
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}