CSS의 max-height 속성을 사용하면 요소 콘텐츠 박스의 최대 높이를 고정값으로 지정할 수 있습니다. 이 속성이 적용되면 실제 콘텐츠의 높이가 지정된 값보다 크더라도 요소가 그 이상으로 늘어나지 않습니다.
기본 문법
CSS max-height 속성의 기본 문법은 다음과 같습니다.
Selector {
max-height: /*값*/
}그럼 CSS max-height 속성의 실제 사용 예제를 살펴보겠습니다.
예제 1 – 버튼 클릭으로 maxHeight 변경하기
<!DOCTYPE html>
<html>
<head>
<title>CSS max-height Property</title>
</head>
<style>
* {
padding: 2px;
margin:5px;
}
button {
border-radius: 10px;
}
#containerDiv {
width:70%;
margin: 0 auto;
padding:20px;
background-image: linear-gradient(135deg, #dc3545 0%, #9599E2 100%);
text-align: center;
border-radius: 10px;
}
#contentDiv{
max-height:50px;
overflow: hidden;
}
</style>
<body>
<div id="containerDiv">
<div id="contentDiv">
This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
</div>
<button onclick="add()" class="btn">Set maxHeight</button>
</div>
<script>
function add() {
document.querySelector('#contentDiv').style.maxHeight = "100px";
}
</script>
</body>
</html>실행 결과
위 예제에서는 콘텐츠 영역에 max-height: 50px와 overflow: hidden이 함께 적용되어 있어, 텍스트가 많아도 50px까지만 표시되고 나머지는 잘려서 보입니다.
'Set maxHeight' 버튼을 클릭하기 전 화면입니다.

'Set maxHeight' 버튼을 클릭하면 자바스크립트를 통해 max-height 값이 100px로 변경되면서 더 많은 텍스트가 보이게 됩니다.

이처럼 자바스크립트와 함께 활용하면 아코디언 메뉴나 '더 보기' 기능 등을 손쉽게 구현할 수 있습니다.
이번에는 이미지에 max-height를 적용하는 또 다른 예제를 살펴보겠습니다.
예제 2 – 이미지에 max-height 적용하기
<!DOCTYPE html>
<html>
<head>
<title>CSS max-height</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
#containerDiv {
margin: 0 auto;
max-height: 150px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS max-height</legend>
<div id="containerDiv">
<img id="image" src="https://www.tutorialspoint.com/openshift/images/openshift-mini-logo.jpg">
</div>
</fieldset>
</form>
</body>
</html>실행 결과
max-height가 정의되기 전에는 컨테이너가 이미지 원본 크기만큼 늘어납니다.

max-height가 150px로 정의된 후에는 컨테이너의 높이가 150px로 제한됩니다.

정리
max-height 속성은 반응형 웹 디자인에서 레이아웃이 의도치 않게 무너지는 것을 방지할 때 매우 유용합니다. 특히 overflow: hidden 또는 overflow: auto와 함께 사용하면 긴 콘텐츠를 깔끔하게 처리할 수 있으며, min-height 속성과 조합하면 요소의 높이 범위를 상·하한선 모두에서 제어할 수 있습니다.