Computer >> 컴퓨터 >  >> 프로그래밍 >> HTML

HTML DOM style minHeight 속성 – 요소 최소 높이 반환 및 설정 방법

DOM의 style.minHeight 속성은 HTML 문서에서 요소의 최소 높이(minimum height)를 반환하거나 수정하는 데 사용됩니다. 이 속성을 활용하면 콘텐츠 양에 관계없이 요소가 일정 높이 이상으로 축소되지 않도록 보장할 수 있습니다.

구문(Syntax)

기본 문법은 다음과 같습니다.

minHeight 값 반환하기

object.style.minHeight

minHeight 값 설정하기

object.style.minHeight = "value"

속성 값(Values)

value 자리에 들어갈 수 있는 값은 다음과 같습니다.

설명
inherit부모 요소로부터 해당 속성 값을 상속받습니다.
initial해당 속성 값을 기본(default) 값으로 되돌립니다.
lengthpx, em, rem 등 길이 단위로 최소 높이를 지정합니다.
percentage(%)부모 요소 높이를 기준으로 한 백분율(%) 값으로 지정합니다.

예제(Example)

style.minHeight 속성의 실제 사용 예를 살펴보겠습니다. 버튼을 클릭하면 단락(p) 요소의 최소 높이가 300px로 설정되는 코드입니다.

<!DOCTYPE html>
<html>
<head>
<style>
    body {
        color: #000;
        background-image: linear-gradient(to top, #a18cd1 0%, #fbc2eb 100%);
        height: 100vh;
    }
    p {
        border: 2px solid #fff;
        width: 300px;
    }
    .btn {
        background: coral;
        border: none;
        height: 2rem;
        border-radius: 2px;
        width: 40%;
        display: block;
        color: #fff;
        outline: none;
        cursor: pointer;
    }
</style>
</head>
<body>
<h1>DOM Style minHeight Property Example</h1>
<p>
    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.
</p>
<button onclick="add()" class="btn">Set minHeight</button>
<script>
    function add() {
        document.querySelector('p').style.minHeight = "300px";
    }
</script>
</body>
</html>

출력(Output)

위 코드를 실행하면 다음과 같은 화면이 출력됩니다.

HTML DOM style minHeight 속성 – 요소 최소 높이 반환 및 설정 방법

여기서 “Set minHeight” 버튼을 클릭하면, 단락(p) 요소의 최소 높이가 300px로 지정되어 아래와 같이 변경된 결과를 확인할 수 있습니다.

HTML DOM style minHeight 속성 – 요소 최소 높이 반환 및 설정 방법