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

JavascriptWeb DevelopmentFront End Scripts

Use the maxWidth property in JavaScript to set the maximum width.

Example

You can try to run the following code to set the maximum width of an element with JavaScript −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <p>Click below to set Maximum width.</p>
      <button type="button" onclick="display()">Max Width</button>
      <div id="box">
         <p>This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div.</p>
      </div>
      <br>
      <script>
         function display() {
            document.getElementById("box").style.maxWidth = "80px";
         }
      </script>
   </body>
</html>
raja
Published on 27-Feb-2018 18:19:20
Advertisements