Javascript DOM CSS Style minWidth Property set

Introduction

Set the minimum width of a <div> element:

document.getElementById("myDIV").style.minWidth = "400px";

Click the button to make sure that the DIV element's width never is below 400 pixels:

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {//  ww w  . j  av  a  2s .  c  om
  width: 50%;
  background-color: lightblue;
}
</style>
</head>
<body>
<button onclick="myFunction()">Test</button>

<div id="myDIV">
  <p>This DIV element has a defined width of 50%.</p>
  <p>This DIV element has a defined width of 50%.</p>
  <p>This DIV element has a defined width of 50%.</p>
  <p>The width of this DIV element is 50% of its parent element.</p>

  <p>Try resizing the browser window to see that width of this DIV element will grow/shrink along width the resizing.</p>

  <p>But if you click the "Test" button, you will make sure that this DIV element does not get below 400 pixels in width.</p>
</div>

<script>
function myFunction() {
  document.getElementById("myDIV").style.minWidth = "400px";
}
</script>

</body>
</html>

The minWidth property sets or gets the minimum width of an element.

The minWidth property has effect only on block-level elements or on elements with absolute or fixed position.

Property Values

Value Description
length Sets the minimum width in length units. Default is 0
% Sets the minimum width in % of the parent element
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

The minWidth property Default Value: 0

The minWidth property returns a String representing the minimum width of an element.




PreviousNext

Related