JavaScript – Change the Width of Paragraph

To change the width of a paragraph using JavaScript, get reference to the paragraph element, and assign required width value to the element.style.width property.

Example

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head> 
<body>
    <p id="myPara">Change the Width of Paragraph in JavaScript</p>
    <button type="button" onclick="changeStyle()">Click Me</button>
    <script>
    function changeStyle(){
        var element = document.getElementById("myPara");
        element.style.width = "200px";
    }
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to change the width of a paragraph using JavaScript.