Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

CSS max-width Property

  • Last Updated : 19 Oct, 2021

The max-width property in CSS is used to define the maximum width of an element. The value of the width cannot be larger than the value by max-width. If the content is larger than the max-width then it will go to the next line and if the content is smaller than max-width then it has no effect.

Syntax:

max-width: none| length| initial| inherit;

Property Values: All the properties are described well with the example below.

none: It is the default value and it does not contain max-width.

Syntax:

max-width: none;

Example: This example illustrates the use of the max-width property whose value is set to none.

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

length: This property is used to set the length of max-width. The length can be set in form of px, cm, etc.

Syntax:

max-width: length;

Example: This example illustrates the use of the max-width property, in order to set the max-width length.

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:

percentage (%): This property is used to set the max-width in form of a percentage.

Syntax:

max-width: %;

Example: This example illustrates the use of the max-width property whose value is assigned in form of a percentage.

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:

initial: It is used to set an element’s CSS property to its default value. The initial keyword can be used for any CSS property, and on any HTML element.

Syntax:

max-width: initial;

Example: This example illustrates the use of the max-width property, in order to set the value to its default value.

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:

inherit: This property is used to inherit a property to an element from its parent element property value. The inherit keyword can be used for inheriting any CSS property, and on any HTML element.

Supported Browsers: The browser supported by the max-width property are listed below: 

  • Google Chrome 1.0
  • Internet Explorer 7.0
  • Microsoft Edge 12.0
  • Firefox 1.0
  • Safari 2.0.2
  • Opera 7.0

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!