The max-height
property controls max height for only block-level elements or
elements with absolute or fixed position.
The maxHeight
property sets or gets the maximum height of an element.
maxHeight |
Yes | Yes | Yes | Yes | Yes |
Return the maxHeight property:
var v = object.style.maxHeight
Set the maxHeight property:
object.style.maxHeight='none|length|%|initial|inherit'
Value | Description |
---|---|
none | Default. No controls on the element height. |
length | Set the maximum height in length units |
% | Set the maximum height in percent of the container element |
initial | Set to default value. |
inherit | Inherits from parent element. |
Default Value: | none |
---|---|
Return Value: | A string representing the maximum height |
CSS Version | CSS2 |
The following code shows how to set the maximum height of an element.
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {<!--from www. j a v a2 s .c o m-->
width: 500px;
background-color: black;
overflow: auto;
}
</style>
</head>
<body>
<button onclick="myFunction()">test</button>
<div id="myDIV">
<p>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>
test<br/>test<br/>test<br/>test<br/></p>
</div>
<script>
function myFunction() {
document.getElementById("myDIV").style.maxHeight = "100px";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get the maximum height of an element.
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {<!-- ww w . j a v a2 s . c o m-->
width: 400px;
background-color: black;
overflow: auto;
}
</style>
</head>
<body>
<button onclick="myFunction()">test</button>
<div id="myDIV" style="max-height:100px;">
<p>test<br/>test<br/>test<br/>test<br/>test<br/>test<br/>
test<br/>test<br/>test<br/>test<br/></p>
</div>
<script>
function myFunction() {
console.log(document.getElementById("myDIV").style.maxHeight);
}
</script>
</body>
</html>
The code above is rendered as follows: