How to set the maximum width of an element with JavaScript?

The maxWidth property in JavaScript allows you to set the maximum width of an element dynamically. This property controls how wide an element can grow, which is particularly useful for responsive layouts and content that needs width constraints.

Syntax

element.style.maxWidth = "value";

The value can be specified in pixels (px), percentages (%), em, rem, or other valid CSS units.

Example: Setting Maximum Width

<!DOCTYPE html>
<html>
<head>
    <style>
        #box {
            background-color: lightblue;
            border: 2px solid blue;
            padding: 10px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <p>Click the button to set maximum width to 200px:</p>
    <button type="button" onclick="setMaxWidth()">Set Max Width</button>
    <button type="button" onclick="resetWidth()">Reset Width</button>
    
    <div id="box">
        <p>This is a div with content that will be constrained by the maximum width property. Notice how the text wraps when the maximum width is applied.</p>
    </div>
    
    <script>
        function setMaxWidth() {
            document.getElementById("box").style.maxWidth = "200px";
        }
        
        function resetWidth() {
            document.getElementById("box").style.maxWidth = "none";
        }
    </script>
</body>
</html>

Multiple Ways to Set Maximum Width

<!DOCTYPE html>
<html>
<head>
    <style>
        .demo-box {
            background-color: lightgreen;
            border: 1px solid green;
            padding: 8px;
            margin: 5px 0;
        }
    </style>
</head>
<body>
    <button onclick="setPixelWidth()">Set 150px</button>
    <button onclick="setPercentageWidth()">Set 50%</button>
    <button onclick="setEmWidth()">Set 20em</button>
    <button onclick="removeMaxWidth()">Remove Max Width</button>
    
    <div id="testBox" class="demo-box">
        <p>This div's maximum width will change based on the button you click.</p>
    </div>
    
    <script>
        function setPixelWidth() {
            document.getElementById("testBox").style.maxWidth = "150px";
        }
        
        function setPercentageWidth() {
            document.getElementById("testBox").style.maxWidth = "50%";
        }
        
        function setEmWidth() {
            document.getElementById("testBox").style.maxWidth = "20em";
        }
        
        function removeMaxWidth() {
            document.getElementById("testBox").style.maxWidth = "";
        }
    </script>
</body>
</html>

Common Use Cases

Value Type Example Use Case
Pixels "300px" Fixed maximum width
Percentage "80%" Responsive to parent container
Em units "25em" Relative to font size
"none" "none" Remove maximum width constraint

Key Points

  • The maxWidth property only sets an upper limit ? elements can be narrower but not wider
  • Use empty string "" or "none" to remove the maximum width constraint
  • This property is commonly used for responsive design and content layout control
  • The property affects the content box, not including padding, border, or margin

Conclusion

The maxWidth property provides flexible control over element sizing in JavaScript. Use it to create responsive layouts and prevent elements from becoming too wide for their containers.

Updated on: 2026-03-15T23:18:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements