HTML | DOM Style maxWidth Property

The maxWidth property set/return the maximum width of an element.
The maxWidth property affects only on block-level elements, absolute or fixed position elements.

Syntax:

  • It returns the maxWidth property.



    object.style.maxWidth
    
  • It sets the maxWidth Property.

    object.style.maxWidth = "none|length|%|initial|inherit"
    

Values:

Value Description
none Default value without any limit on width of the element
length Define width’s maximum value in length unit
% Define width’s maximum value in % of the parent element
initial Set property to its default value
inherit Inherit from its parent element

Return value:It return the maximum width of element.

Example-1: Set width in length unit.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Style maxWidth Property </title>
</head>
<style>
    #Geek1 {
        color: white;
        width: 300px;
        background: green;
    }
</style>
  
<body>
    <center>
        <h1 id="Geek1"
                GeeksForGeeks 
            </h1>
  
        <h2>DOM Style maxWidth Property </h2>
        <br>
        <button type="button" onclick="mygeeks()">
            Click to change
        </button>
  
        <script>
            function mygeeks() {
                
                //  set width using length unit.
                document.getElementById(
                  "Geek1").style.maxWidth = "220px";
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before click on button:
  • After click on button:

Example-2: Set width in ‘%’.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Style maxWidth Property </title>
</head>
<style>
    #Geek1 {
        color: white;
        width: 50%;
        background: green;
    }
      
    #Geek_Center {
        background: yellow;
        width: 400px;
        margin-left: 150px;
    }
</style>
  
<body>
    <center id="Geek_Center">
        <h3 id="Geek1"
                GeeksForGeeks 
            </h3>
  
        <h2>DOM Style maxWidth Property </h2>
        <br>
  
        <button type="button" onclick="mygeeks()">
            Click to change
        </button>
  
        <script>
            function mygeeks() {
                
                // Set width using % .
                document.getElementById(
                    "Geek1").style.maxWidth = "35%";
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before click on button:
  • After click on button:

Supported Browsers: The browser supported by HTML | DOM Style maxWidth Property are listed below:

  • Google Chrome
  • Internet Explorer
  • Mozilla firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.