CSS max-width Property
Last Updated :
28 Aug, 2024
The max-width property defines the maximum width of an element. It prevents the element from exceeding a specified width, ensuring responsive design. If the content exceeds this width, it will adjust to fit within the defined maximum, enhancing layout control.
Syntax
max-width: none | length | percentage (%) | initial | inherit;
Property Values
All the properties are described well in the example below.
| Value | Description |
|---|
| none | Default value without setting a maximum width. |
| length | Sets the maximum width using a specific length unit like pixels (px), centimeters (cm), etc. |
| percentage | Sets the maximum width as a percentage of the containing element’s width. |
| initial | Sets the property to its default value. |
| inherit | Inherits the property value from its parent element. |
Example 1: Using max-width with none
This example sets the max-width property of paragraphs (<p>) to none, allowing them to expand without limit horizontally within a green background.
HTML
<!DOCTYPE html>
<html>
<head>
<title>max-width property</title>
<!-- max-width CSS property -->
<style>
p {
max-width: none;
color: white;
background-color: green;
}
</style>
</head>
<body>
<p> GeeksforGeeks: A computer science portal </p>
</body>
</html>
Output:

Example 2: Using max-width with length
This example sets the maximum width using a length value in pixels.
HTML
<!DOCTYPE html>
<html>
<head>
<title>max-width property</title>
<!-- max-width CSS property -->
<style>
p {
max-width: 110px;
color: white;
background-color: green;
}
</style>
</head>
<body>
<p>
GeeksforGeeks: A computer science portal
</p>
</body>
</html>
Output:

Example 3: Using max-width with Percentage
This example sets the maximum width as a percentage of the containing element.
HTML
<!DOCTYPE html>
<html>
<head>
<title>max-width property</title>
<!-- max-width CSS property -->
<style>
p {
max-width: 20%;
color: white;
background-color: green;
}
</style>
</head>
<body>
<p>
GeeksforGeeks: A computer science portal
</p>
</body>
</html>
Output:

Example 4: Using max-width with initial
This example sets the max-width property to its default value using initial.
HTML
<!DOCTYPE html>
<html>
<head>
<title>max-width property</title>
<!-- max-width CSS property -->
<style>
p {
max-width: initial;
color: white;
background-color: green;
}
</style>
</head>
<body>
<p>
GeeksforGeeks: A computer science portal
</p>
</body>
</html>
Output:

Example 5: Using max-width with inherit
This example demonstrates how to inherit the max-width property from the parent element.
HTML
<!DOCTYPE html>
<html>
<head>
<title>max-width property</title>
<!-- max-width CSS property -->
<style>
.container {
max-width: inherit;
color: white;
background-color: green;
}
</style>
</head>
<body>
<div class="container">
<p>
GeeksforGeeks: A computer science portal
</p>
</div>
</body>
</html>
Output:

Understanding and using the max-width property in CSS is essential for responsive design. It controls elements’ maximum width, ensuring correct content display across all viewports, enhancing CSS skills, and creating adaptable, user-friendly layouts.
Supported Browsers: The browser supported by the max-width property are listed below:
CSS max-width Property – FAQs
What is the purpose of the max-width property in CSS?
The max-width property in CSS sets the maximum width an element can occupy, preventing it from exceeding a specified width regardless of its content or container size.
How do I use max-width for responsive design?
To make a design responsive, max-width can be set in percentages or viewport units (e.g., max-width: 100%; or max-width: 80vw; ) to ensure elements resize according to the screen size.
Can max-width override width in CSS?
Yes, max-width can override the width property if the width exceeds the specified max-width. For example, if width: 500px; and max-width: 300px; are both applied, the element will be limited to 300 pixels in width.
Is max-width supported in all browsers?
Yes, max-width is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Internet Explorer 8 and above.
What’s the difference between max-width and width?
The width property sets a fixed width for an element, while max-width sets a maximum width the element can grow to. Max-width allows an element to be more flexible and responsive in different viewports.
Please Login to comment...