Open In App

CSS max-width Property

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 the max-width then it has no effect.

Syntax:

max-width: none | length | percentage (%) | initial | inherit;

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

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

<!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: This example illustrates the use of the max-width property, in order to set the max-width length.

<!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: This example illustrates the use of the max-width property whose value is assigned in the form of a percentage.

<!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: This example illustrates the use of the max-width property, in order to set the value to its initial or default value.

<!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: This example illustrates the use of the max-width property, with the value set to inherit.

<!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

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

Article Tags :