CSS 文字行高、文字縮排
透過網頁顯示具有大量文字的文章時,通常都會設定文字的「行高」或「縮排」,這篇教學會介紹「什麼是行高」,以及運用 line-height 和 text-indent 這兩個 CSS 樣式屬性,設定文字的行高和縮排。
快速導覽:
認識行高
網頁中的每個文字,除了本身的字型設定的尺寸,也會根據字型定義產生「內容區域 content area」,預設的行高和內容區域的高度相同,大部分情況為字型尺寸的 1.2 倍,如果有設定行高數值,則會以「中心線」向上下擴展至設定的數值,下圖呈現文字尺寸 ( 深藍色 )、內容區域 ( 淺藍色 ) 和行高 ( 藍色虛線 ) 的關係。
如果要在同一行裡面,使用不同的文字字型或尺寸的元素,這些元素會被擺放在「行框」裡,預設會先對齊「基準線 baseline」,但因為每個字型的內容區域和中心線都不同,所以會根據每個字型的行高找出「最大值」,採用這個數值作為「行框的高度」。
line-height 行高
line-height
樣式屬性可以設定文字的「行高」,屬性值可以的用 normal 或非負值的數字,行高的樣式通常會設在父元素,運用繼承特性,讓子元素繼承「父元素的行高」,因為行高會改變「行框高度」,進而會影響顯示類型為「區塊容器 block」的元素高度,但不會影響顯示類型為「行內容器 inline」或「行內容器相關 inline-*」的元素高度,雖然不影響元素實際高度,但由於行框的高度已經改變,所以仍然會影響到周圍元素的位置。
行高可以使用多種數值與單位,下表列出屬性值說明:
屬性值或單位 | 說明 |
---|---|
使用 normal | 預設值,等於 content area 高度 ( 字型尺寸的 1.2 倍 )。 |
px | 像素,絕對長度單位。 |
無單位 | 元素文字尺寸的幾倍。 |
% | 元素文字尺寸的多少百分比。 |
em | 元素文字尺寸的幾倍。 |
rem | 根元素文字尺寸的幾倍。 |
下方範例使用 h3 元素包覆文字,並透過 line-height
設定不同的行高,透過背景顏色就能觀察出行高影響行框並影響元素的變化,淺色是 span
背景,深色是 h3
背景。
<!-- HTML 程式碼 -->
<h3 class="a"><span>Morning!! (line-height: normal;)</span></h3>
<h3 class="b"><span>Morning!! (line-height: 1;)</span></h3>
<h3 class="c"><span>Morning!! (line-height: 1em;)</span></h3>
<h3 class="d"><span>Morning!! (line-height: 100%;)</span></h3>
<h3 class="e"><span>Morning!! (line-height: 30px;)</span></h3>
<h3 class="f"><span>Morning!! (line-height: 0.3;)</span></h3>
<h3 class="g"><span>Morning!! (line-height: 3;)</span></h3>
<!-- CSS 程式碼 -->
<style>
h3 {
font-size: 20px;
background: orange;
border: 1px solid #000;
}
h3 span {
background: #fff7;
}
.a {line-height: normal;} /* 等於 content area 高度 */
.b {line-height: 1;} /* 30px x 1 = 30px */
.c {line-height: 1em;} /* 30px x 1 = 30px */
.d {line-height: 100%;} /* 30px x 100% = 30px */
.e {line-height: 30px; } /* 絕對長度單位 = 30px */
.f {line-height: 0.3;} /* 30px x 0.3 = 9px */
.g{line-height: 3;} /* 30px x 3 = 90px */
</style>
如果子元素是空元素 ( 沒有任何內容 ),因為沒有任何「字型」出現,所以不存在內容區域,使用 line-height
也就沒有任何作用,下方範例會展示空元素、換行符與有內容的差異。
<!-- HTML 程式碼 -->
<div class="a"><span></span></div> <!-- 子元素為空元素 -->
<div class="b">
<br> <!-- 換行符 -->
</div>
<div class="c"> 有設定行高,沒有子元素</div>
<div class="c"> <span>有設定行高,有子元素</span></div>
<!-- CSS 程式碼 -->
<style>
div {
width: 250px;
border: 1px solid #000;
background: orange;
margin: 10px;
line-height: 1.5;
}
</style>
line-height 不同屬性值在繼承時的差異
如果一行文字裡有多種不同的字型,或有不同尺寸的文字,會運用這些不同的行高,計算出「最大行高」撐開該行的行框,特別注意,由於行高會「繼承」,「無單位」和「有單位」的屬性數值在繼承的表現上有極大的差異,這種差異會明顯影響不同元素進行「垂直置中」的對齊,甚至會產生出現「超出範圍」或「莫名空白」等奇怪狀況,下方列出繼承表現的差異:
- 無單位數值:繼承「無單位數值」,子元素的行高會重新計算 ( 乘以自己的文字尺寸 )。
- px 單位數值:繼承「固定尺寸」。
- em、% 單位數值:先乘以父元素的文字尺寸,接著繼承「計算後的結果」,等同繼承固定尺寸,但需要先換算。
下方使用五個 div
,分別表現不同屬性值的繼承特性 ( 粉紅色 div
為對照組高度 120px ),從中可以觀察當設定為 1 的時候,繼承後會使用自己計算的行高,對齊時採用「最大行高」,高度也就和粉紅區域相同,但設定為 6 的時候,子元素的行高也出現 120px,但因為預設「對齊基線」,也就造成下方凸出的狀況,其他設定 px、em 和 % 會直接繼承 120px,就會產生和設定 6 相同的狀況。
<!-- HTML 程式碼 -->
父元素行高使用無單位數值 1
<section class="s1">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
父元素行高使用無單位數值 1,子元素設定 6
<section class="s2">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
父元素行高使用 px 單位數值
<section class="s3">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
父元素行高使用 em 單位數值
<section class="s4">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
父元素行高使用 % 單位數值
<section class="s4">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
<!-- CSS 程式碼 -->
<style>
section {
height: 150px;
margin: 10px;
}
div {
background: orange;
float: left;
font-size: 120px;
}
.s1 div {line-height: 1;}
.s2 div {line-height: 1;}
.s2 div * {line-height: 6;}
.s3 div {line-height: 120px;}
.s4 div {line-height: 1em;}
.s5 div {line-height: 100%;}
.a {
font-size: 20px;
background: #0f08;
}
.b {
font-size: 10px;
background: #00f6;
}
.d2 {
height: 120px;
background: pink;
width: 20px;
}
</style>
行框高度除了被行高影響,行框高度也會「不具有行高屬性,但具有高度屬性的元素」所影響 ( 例如 inline-block
類型的圖片元素 ),當整體計算高度大於圖片高度時,在部分垂直對齊屬性下,圖片下方就會一個空白區域,如果要避免這種狀況,可以縮小行高數值,當計算後數值小於圖片高度,就不會出現空白區域。
<!-- HTML 程式碼 -->
<div class="a">
<span>line-height: 0px;</span>
<img src="https://steam.oxxostudio.tw/download/css/function-filter-demo.jpg">
</div>
<div class="b">
<span>line-height: 10px;</span>
<img src="https://steam.oxxostudio.tw/download/css/function-filter-demo.jpg">
</div>
<div class="c">
<span>line-height: 30px;</span>
<img src="https://steam.oxxostudio.tw/download/css/function-filter-demo.jpg">
</div>
<div class="d">
<span>line-height: 50px;</span>
<img src="https://steam.oxxostudio.tw/download/css/function-filter-demo.jpg">
</div>
<!-- CSS 程式碼 -->
<style>
div {
width: 250px;
border: 1px solid #000;
background: orange;
margin: 20px;
}
img {height: 80px;}
.a {line-height: 0;}
.b {line-height: 10px;}
.c {line-height: 30px;}
.d {line-height: 60px;}
</style>
text-indent 縮排
text-indent
樣式屬性可以設定「第一行文字縮排」,具有繼承特性,如果第一行開頭是 inline-block 顯示屬性的元素 ( 例如圖片 ),這個元素也會跟著縮排,屬性值需為具有長度單位的數字,長度單位說明如下:
縮排單位 | 說明 |
---|---|
px | 像素。 |
em | 文字大小的倍數。 |
rem | 根元素文字大小的倍數。 |
% | 元素寬度的百分比。 |
下方範例使用不同的數字單位,呈現縮排 40px 的效果。
<!-- HTML 程式碼 -->
<div class="a">STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。</div>
<div class="b">STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。</div>
<div class="c">STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。</div>
<div class="d">STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。</div>
<!-- CSS 程式碼 -->
<style>
:root {font-size: 10px;}
div {
border: 2px solid #000;
width: 400px;
margin: 10px;
padding: 10px;
font-size:20px;
line-break: anywhere;
}
.a {text-indent: 40px;} /* 使用絕對長度 = 40px */
.b {text-indent: 2em;} /* 20px x 2 = 40px */
.c {text-indent: 10%;} /* 400px x 0.1 = 40px */
.d {text-indent: 4rem;} /* 10px x 4 = 40px */
</style>
縮排的方向會依據文字顯示的方向,如果是左向右,正值向右,負值向左,設定負值時會產生「凸排」的效果,但也有可能會被元素邊緣「裁切」,使用時常會搭配 margin 或 padding 設定邊界,下方範例展示一些凸排的效果。
<!-- HTML 程式碼 -->
<div class="a">STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。</div>
<div class="b">STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。</div>
<div class="c">STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。
<img src="https://steam.oxxostudio.tw/download/css/function-filter-demo.jpg">
</div>
<!-- CSS 程式碼 -->
<style>
div {
box-sizing: border-box;
border: 2px solid #000;
width: 400px;
margin: 10px;
padding: 10px;
font-size:20px;
line-break: anywhere;
}
.a {text-indent: -40px;} /* 單純凸排,畫面被裁切 */
.b {
padding-left: 50px; /* 設定內邊距,顯示凸排內容 */
text-indent: -40px;
}
.c {
padding-left: 60px; /* 設定內邊距,顯示凸排內容 */
text-indent: -50px;
}
.c img {
float: left; /* 設定圖片為靠左浮動 */
width: 40px;
margin-top: -24px; /* 設定圖片外邊距 */
margin-left: -50px;
}
</style>
小結
文字的行高 line-height
和縮排 text-indent
,都是在網頁中表現大量文字時常用的樣式屬性,兩者在設定上也不困難,大多只需要設定數字就完成,透過這篇教學的介紹,應該就能對這兩個樣式有更深的認識。
意見回饋
如果有任何建議或問題,可傳送「意見表單」給我,謝謝~