當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


CSS max-height屬性用法及代碼示例

它設置元素內容框的最大高度。這意味著內容框的高度可以小於 max-height 值,但不能大於。它設置元素高度的上限。

當內容大於最大高度時,就會溢出。如果內容小於 max-height,則該屬性不受影響。該屬性確保 height 屬性的值不能大於 max-height 屬性的值。它不允許負值。

有時將元素的高度限製在某個範圍內很有用。

用法

max-height:none | length | initial | inherit;

此 CSS 屬性的值定義如下。

none:它是不限製內容框大小的默認值。

length:該值以 px、cm、pt 等為單位定義 max-height。

initial:它將屬性設置為其默認值。

inherit:它從其父元素繼承屬性。

現在,讓我們看一個這個 CSS 屬性的例子。

示例

在這個例子中,有四個帶有內容的段落元素。我們使用 max-height 屬性的長度值定義這些段落的 maximum-height。第一段最大高度60px,第二段6em,第三段130pt,第四段5cm。

第一段的內容大於 max-height 屬性的值,所以在輸出中,我們可以看到第一段的內容溢出了內容框。

<!DOCTYPE html>
<html>
<head>
<title>
max-height property
</title>

<style>
p{
border:4px solid blue;
background-color:lightblue;
font-size:20px;
}
#px {
max-height:60px;
}
#em {
max-height:6em;

}
#pt {
max-height:130pt;

}
#cm {
max-height:5cm;

}
</style>
</head>
<body>
<h2> max-height:60px; </h2>
<p id = "px">
Hi, Welcome to the javaTpoint.com. This site is developed so that students may learn computer science related technologies easily. The javaTpoint.com is always providing an easy and in-depth tutorial on various technologies. No one is perfect in this world, and nothing is eternally best. But we can try to be better.
</p>
<br>
<h2> max-height:6em; </h2>
<p id = "em">
Hi, Welcome to the javaTpoint.com. This site is developed so that students may learn computer science related technologies easily. The javaTpoint.com is always providing an easy and in-depth tutorial on various technologies. No one is perfect in this world, and nothing is eternally best. But we can try to be better.
</p>
<h2> max-height:130pt; </h2>
<p id = "pt">
Hi, Welcome to the javaTpoint.com. This site is developed so that students may learn computer science related technologies easily. The javaTpoint.com is always providing an easy and in-depth tutorial on various technologies. No one is perfect in this world, and nothing is eternally best. But we can try to be better.
</p>
<h2> max-height:5cm; </h2>
<p id = "cm">
Hi, Welcome to the javaTpoint.com. This site is developed so that students may learn computer science related technologies easily. The javaTpoint.com is always providing an easy and in-depth tutorial on various technologies. No one is perfect in this world, and nothing is eternally best. But we can try to be better.
</p>

</body>
</html>

輸出

CSS max-height property



相關用法


注:本文由純淨天空篩選整理自 CSS max-height property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。