The Wayback Machine - https://web.archive.org/web/20240829162911/https://www.geeksforgeeks.org/css-max-width-property/
Open In App

CSS max-width Property

Last Updated : 28 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

ValueDescription
noneDefault value without setting a maximum width.
lengthSets the maximum width using a specific length unit like pixels (px), centimeters (cm), etc.
percentageSets the maximum width as a percentage of the containing element’s width.
initialSets the property to its default value.
inheritInherits 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:

max-width property

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:

Screenshot-2023-07-05-160738

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:

Screenshot-2023-07-05-170626

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.



Previous Article
Next Article

Similar Reads

CSS Media Queries - max-width or max-height
CSS media queries can help you apply various styles depending on the user's device or viewport properties. You may make responsive designs with them, which change to accommodate various screen sizes, orientations, and device capabilities. The @media rule in CSS is used to write media queries, which can target a wide range of characteristics, includ
3 min read
HTML width/height Attribute vs CSS width/height Property
In HTML 5, few elements follow the width and height attributes and maximum elements do not follow this attribute. Like img, iframe, canvas, and svg kind of elements follow the width and height attributes but div, span, article and section type of elements don't follow them.The width and height attributes are affected in img, svg tags, those are wea
3 min read
Difference Between css(‘width’) and width() methods In jQuery
In jQuery, we have two ways to change the width of any HTML element. The jQuery css() method and width() method, below both the methods are described properly with the example. jQuery CSS('width') Method: It is a method present in jQuery which is used to get or set the property on the matched element. It is a property used to get or set the width o
3 min read
How to add Background-Color for Text Width Instead of Entire Element Width using CSS ?
In this article, we will see how to add background color for the text width instead of the entire element width using CSS. The display: inline or display: inline-block property is used to set the background color of the text width. Syntax: display: inline; display: inline-block; Approach: We will create an element and then apply the display: inline
2 min read
How to make a grid with two columns where 1st column has 20% of width and 2nd one 80% width in Tailwind CSS ?
Tailwind CSS simplifies the process of making complex UI designs. It has a lot of predefined styles in form of classes, which can be applied to any HTML element in the code. Also, one advantage of Tailwind CSS is that during the production build, it analyzes the HTML components, and removes any unused CSS, hence decreasing the overall size of the C
4 min read
What is the difference between 'width: 100%' and 'width: 100vw' in CSS ?
The CSS width Property is used to set the width of the text and images. The width can be assigned to the text and images in the form of pixels(px), percentages (%), centimeters (cm), etc. The width: 100% sets an element's width relative to its containing block, making it responsive within its parent. The width: 100% responds to the container's size
1 min read
Explain the purpose of the max-width Property
The max-width property in CSS is used to set the maximum width of an element. It is commonly applied to ensure that an element does not exceed a specified width, providing control over the layout and responsiveness of a web page. The max-width property can be applied to various elements, such as images or containers, limiting their width. Syntax:/*
1 min read
Tailwind CSS Max-Width
This class accepts lots of values in tailwind CSS in which all the properties are covered in class form.  It is the alternative to the CSS max-width property. This class is used to define the maximum width of an element. The value of the width cannot be greater than the value of the max-width. If the content specified within the element is bigger,
3 min read
Bootstrap 5 Breakpoints Max-width
For Max-width, CSS is executed for the size smaller than mentioned in the media query. There are media queries and mixins for targeting segments of screen sizes using the minimum and maximum breakpoint widths. We use xs, md, lg, xl, xxl for various viewport sizes. We often utilize media queries that travel in the opposite way that is the given size
3 min read
Which table property specifies the width that should appear between table cells in CSS ?
In this article, we will see how to specify the width that should appear between table cells. CSS provides the border-spacing property that can be used to set the distance between the borders of neighboring cells in the Table. This property works only when the border-collapse property is set to no-collapse separate. Syntax: border-spacing: length|i
2 min read
CSS outline-width Property
The outline is the line that is created around the specified element outside the border of the element to make the particular distinct and easier to distinguish. The outline-width Property is used to specify the width of this outline for a specific element. The outline-style property must be declared or used before the outline-width property is use
3 min read
CSS min-width Property
The min-width property in CSS is used to define the minimum width of an element. The value of the width cannot be less than the value of min-width. If the content specified within the element is smaller, min-width maintains the specified minimum width. Syntax: min-width: length|initial|inherit; Property Values: length: This property is used to set
2 min read
CSS | stroke-width Property
The stroke-width property is used to set the width of a border in a SVG shape. This property can only be applied to elements that have a shape or are text content elements. Syntax: stroke-width: &lt;length&gt; | &lt;percentage&gt; Property Values: length: It is used to set the stroke width in measurement units. It can take values in whole numbers o
2 min read
CSS | border-block-start-width Property
The border-block-start-width property in CSS is used to define the individual logical block-start border property values in a single place in the style sheet. This property sets the border-width at the top of the defining element border. Syntax: border-block-start-width: border-width; Property values: border-width: This property holds the width of
1 min read
CSS | border-block-end-width Property
The border-block-end-width property in CSS is used to define the individual logical block-end border property values in a single place in the style sheet. This property sets the border-width at the bottom of the defining element border. Syntax: border-block-end-width: border-width; Property values: border-width: This property holds the width of the
1 min read
CSS | border-inline-start-width Property
The border-inline-start-width property in CSS is used to define the individual logical inline-end border property values in a single place in the style sheet. This property sets the border-width at the left of the defining element border. Syntax: border-inline-start-width: border-width; Property values: border-width: This property holds the width o
1 min read
CSS | border-inline-end-width Property
The border-inline-end-width property in CSS is used to define the individual logical inline-end border property values in a single place in the style sheet. This property sets the border-width at the bottom of the defining element border. Syntax: border-inline-end-width: border-width; Property values: border-width: This property holds the width of
1 min read
Which property specifies the width of a border in CSS ?
In this article, we will learn to set the border's size of any element using the CSS border-width property. It can take up to 4 values. Users can consider the below values for the border-width property and use them according to their requirements. border-width property values: Thin: It specifies the thin border on the element.Medium: It specifies t
4 min read
CSS border-inline-width Property
The border-inline-width property sets the width of an element's borders in the inline direction. It specifies the thickness of the inline-start and inline-end borders, adapting to the text direction. This property ensures consistent border styling in both left-to-right and right-to-left layouts. Syntax: border-inline-width: width;Property values: w
2 min read
CSS border-block-width Property
The border-block-width property sets the width of an element's borders in the block direction. It defines the thickness of the block-start and block-end borders, adapting to the writing mode. This property ensures consistent border styling for elements, regardless of text flow direction. Syntax: border-block-width: width;Property values: width: Thi
2 min read
CSS width Property
The width property in CSS is used to set the width of elements such as text and images. It can be specified in various units, including pixels (px), percentages (%), and centimeters (cm). This property defines the width of the content area, excluding padding, borders, and margins. The width property can be overridden by the min-width and max-width
3 min read
CSS scrollbar-width Property
The scrollbar-width property allows developers to set the width or thickness of an element's scrollbar. This feature can be especially useful on pages where a more prominent element display is required, as reducing the scrollbar width provides more space for content. Syntax:  scrollbar-width: auto | thin | none | initial | inheritProperty Values:
4 min read
CSS border-bottom-width Property
The border-bottom-width property in CSS is used to set a specific width to the bottom border of an element. The border-bottom-style or border-style property is used for the element before using the border-bottom-width property. Syntax: border-bottom-width: length|thin|medium|thick|initial|inherit;Property Values: The border-bottom-width property va
2 min read
CSS border-image-width Property
The CSS border-image-width Property is used to set the width of the border image. It can be set by providing multiple values. If only one value is provided, it is applied to all four sides.When two values are specified, The first value is applied to 'the top and bottom', and The second value is applied to the 'left and right'.When three values are
3 min read
CSS border-left-width Property
The border-left-width property in CSS specifies the width of an element's left border. You can set this width using various units like pixels, ems, or percentages. This property allows for precise control over the left border's thickness, contributing to the element's overall design and layout. Syntax: border-left-width: medium|thin|thick|length|in
4 min read
CSS border-top-width Property
The border-top-width property in CSS is used to set a specific width to the top border of an element. The border-top-style or border-style property is used for the element before using the border-top-width property. Default Value: medium Syntax: border-top-width: length|thin|medium|thick|initial|inherit;Property Values: The border-top-width propert
2 min read
CSS border-width Property
The border-width property in CSS sets the width of an element's border. You can specify a single width for all four sides or define different widths for each side (top, right, bottom, and left). This property is crucial for creating visually appealing borders and enhancing the layout of your web pages. Default Value: medium Syntax of the border-wid
7 min read
CSS column-rule-width Property
The column-rule-width property in CSS is used to change the width of the column rule i.e., the vertical lines between the columns. Syntax: column-rule-width: length|thin|medium|thick|initial|inherit;Default Value: medium Property Values: thin: It is used to set a thin rule between the columns.medium: It is used to create a medium width rule between
4 min read
CSS column-width Property
The columns-width property in CSS is used to define the width of the columns. The minimum number of columns are required to show the content across the element. It is a flexible property. If the browser cannot fit at least two-columns at a given column-width then the two columns will be put into a single column. Syntax: column-width: auto|length|in
4 min read
CSS border-right-width Property
The border-right-width property in CSS sets the width of the right border of an element. It can be defined using various units such as pixels, ems, or percentages. This property helps control the thickness of the right border, enhancing element styling and layout. Syntax: border-right-width: medium|thin|thick|length|initial|inherit; Property Values
4 min read