Computer >> 컴퓨터 >  >> 프로그램 작성 >> CSS

CSS의 max-height 속성

<시간/>

높이가 max-height보다 크더라도 요소의 콘텐츠 상자가 더 커지도록 허용하지 않는 CSS max-height 속성을 사용하여 요소의 콘텐츠 상자에 대해 고정된 최대 높이를 정의할 수 있습니다.

구문

CSS max-height 속성의 구문은 다음과 같습니다 -

Selector {
   max-height: /*value*/
}

CSS max-height 속성의 예를 살펴보겠습니다 -

예시

<!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>

출력

'최대 높이 설정'을 클릭하기 전에 버튼 -

CSS의 max-height 속성

'최대 높이 설정'을 클릭한 후 버튼 -

CSS의 max-height 속성

CSS 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>

출력

최대 높이가 정의되기 전 -

CSS의 max-height 속성

최대 높이가 정의된 후 -

CSS의 max-height 속성