# How to set the minimum width of an element with JavaScript?

JavascriptWeb DevelopmentFront End Scripts

Use the minWidth property in JavaScript to set the minimum width. You can try to run the following code to set the minimum width of an element with JavaScript −

## Example

Live Demo

<!DOCTYPE html>
<html>
<style>
#box {
width: 50%;
background-color: gray;
}
</style>
<body>
<p>Click below to set Minimum width.</p>
<button type="button" onclick="display()">Min Width</button>
<div id="box">
<p>This is a div. This is a div. 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. This is a div. This is a div.</p>
<p>This is a div. This is a div. 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. This is a div. This is a div.</p>
<p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
</div>
<br>
<script>
function display() {
document.getElementById("box").style.minWidth = "500px";
}
</script>
</body>
</html>
Published on 27-Feb-2018 18:37:33