/

HTML / CSS / JavaScript Tutorial

CSS Property: max-width

[this page | pdf | back links]

The CSS (CSS2) max-width property sets the maximum width an element can become. It overrides the width property.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

length

A CSS length

%

Defined as a percentage of that of the containing block

none

(default value). No limit

 

Default Value:

none

JavaScript syntax:

e.g. object.style.maxWidth="300px"

Inherited:

No

Animatable:

Yes

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
p {height: 60px; width: 300px; overflow: auto; background-color: yellow;}
p.x1 {max-width: 75px;}
</style>
</head>
<body>
<p>Element with default property</p>
<p class="x1">Property set using in-file HTML style</p>
<p id="x2">Property Element set using JavaScript</p>

<script>
document.getElementById("x2").style.maxWidth = "75px";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyMaxWidth() {
  var x = document.createElement("DIV"); x.style.maxWidth = "70px"; return (window.getComputedStyle(x, null).maxWidth == "70px");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile