CSS 文字水平對齊、垂直對齊
透過網頁呈現文字時,常需要讓文字進行「對齊」,這篇教學會介紹使用使用 CSS 的 text-align、text-align-last 和 vertical-align 等樣式,控制文字水平對齊、垂直對齊和最後一行對齊。
快速導覽:
教學影片
搭配教學影片一起閱讀,效果會更好!歡迎大家訂閱 STEAM 教育學習網的 Youtube 頻道。
text-align 水平對齊
text-align
樣式屬性可以設定「行內容器元素 ( 通常是文字或圖片 ) 水平對齊」( 注意,其他顯示類型的元素不支援水平對齊 ),具有繼承特性,下方列出相關屬性值:
屬性值 | 說明 |
---|---|
start | 對齊系統文字方向開頭,左到右系統等同 left,反之為 right,預設值。 |
end | 對齊系統文字方向結尾,左到右系統等同 right,反之為 left。 |
left | 靠左對齊,垂直書寫為靠上對齊。 |
right | 靠右對齊,垂直書寫為靠下對齊。 |
center | 置中對齊。 |
justify | 平均分散對齊,單字間距會自動加大或縮小 ( 最後一行預設按照 start 規則 )。 |
match-parent | 類似繼承,但會根據父元素的文字方向決定對齊方向。 |
下方範例使用系統文字方向 ( 左到右 ) 的文字,透過 text-align
呈現不同對齊方式。
<!-- HTML 程式碼 -->
<h2>text-align: start;</h2>
<div class="a">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: end;</h2>
<div class="b">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: left;</h2>
<div class="c">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: right;</h2>
<div class="d">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: center;</h2>
<div class="e">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: justify;</h2>
<div class="f">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: match-parent;</h2>
<div class="g">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<!-- CSS 程式碼 -->
<style>
h2 {
font-size: 20px;
color: #dd0066;
margin: 15px 10px -5px;
}
div {
border: 1px solid #000;
padding: 10px;
margin: 10px;
width: 420px;
}
.a {text-align: start;}
.b {text-align: end;}
.c {text-align: left;}
.d {text-align: right;}
.e {text-align: center;}
.f {text-align: justify; }
.g {text-align: match-parent;}
</style>
如果是垂直書寫的文字,則 start 和 left 對應到「上方」,end 和 right 則對應到「下方」。
<!-- HTML 程式碼 -->
<h2>text-align: start;</h2>
<div class="a">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: end;</h2>
<div class="b">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: left;</h2>
<div class="c">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: right;</h2>
<div class="d">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: center;</h2>
<div class="e">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: justify;</h2>
<div class="f">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<h2>text-align: match-parent;</h2>
<div class="g">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/> STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。 </div>
<!-- CSS 程式碼 -->
<style>
h2 {
font-size: 20px;
color: #dd0066;
margin: 15px 10px -5px;
float: right;
writing-mode:vertical-rl; /* 改成垂直書寫模式 */
}
div {
border: 1px solid #000;
padding: 10px;
margin: 10px;
height: 400px;
float: right;
writing-mode: vertical-rl; /* 改成垂直書寫模式 */
}
.a {text-align: start;}
.b {text-align: end;}
.c {text-align: left;}
.d {text-align: right;}
.e {text-align: center;}
.f {text-align: justify; }
.g {text-align: match-parent;}
</style>
如果分不清楚 start/left 或 end/right 的差異,可以參考下方範例,在元素裡增加 direction
的樣式,就能觀察左到右、右到左的文字方向,對於文字對齊的影響。
<!-- HTML 程式碼 -->
<h2>text-align: start;</h2>
<div class="a">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/>STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。</div>
<h2>text-align: left;</h2>
<div class="b">The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.<br/>STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。</div>
<!-- CSS 程式碼 -->
<style>
div {
border: 1px solid #000;
padding: 10px;
margin: 10px;
width: 400px;
direction: rtl; /* 右到左 */
}
h2 {
font-size: 20px;
color: #dd0066;
margin: 15px 10px -5px;
direction: ltr; /* 左到右 */
}
.a {text-align: start;}
.b {text-align: left;}
</style>
text-align-last 最後一行對齊
text-align-last
樣式屬性可以設定「文字的最後一行對齊」,具有繼承特性,text-align-last
的屬性和 text-align
幾乎相同。
屬性值 | 說明 |
---|---|
start | 對齊系統文字方向開頭,左到右系統等同 left,反之為 right,預設值。 |
end | 對齊系統文字方向結尾,左到右系統等同 right,反之為 left。 |
left | 靠左對齊,垂直書寫為靠上對齊。 |
right | 靠右對齊,垂直書寫為靠下對齊。 |
center | 置中對齊。 |
justify | 平均分散對齊,單字間距會自動加大或縮小。 |
使用時需要注意。最後一行是指「軟換行」產生的最後一行 ( 文字長度超過單行限制而自動換行 ),如果使用「硬換行」產生的換行 ( 使用 <br/>
元素強制換行 ),硬換行前的文字為前一段的最後一行,後一段則再度從第一行開始算起。
從下方範例可以看出,如果遇到 <br/>
元素,前一行會被視作最後一行,而按照 text-align-last
所設定的方式對齊,如果沒有 <br/>
,text-align-last
就會真正控制文字在區塊中的最後一行 ( 箭頭處為最後一行 )。
<!-- HTML 程式碼 -->
<h2>text-align-last: end; ( 有 br )</h2>
<div class="a">
The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.
<br/>
STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。
</div>
<h2>text-align-last: end; ( 沒有 br )</h2>
<div class="b">
The STEAM Education Website enables everyone to easily enter the learning realm of STEAM.STEAM 教育學習網透過一系列免費且高品質的教學與範例,讓所有人都能輕鬆跨入 STEAM 的學習領域。
</div>
<!-- CSS 程式碼 -->
<style>
div {
border: 1px solid #000;
padding: 10px;
margin: 10px;
width: 400px;
}
h2 {
font-size: 20px;
color: #dd0066;
margin: 15px 10px -5px;
}
.a {
text-align: start;
text-align-last: end;
}
.b {
text-align: start;
text-align-last: end;
}
</style>
除了硬換行和軟換行的差異,如果文字只有「單一行」,text-align-last
的權重會高於 text-align
( 通常設定 text-align
就能搞定大多數的狀況 ),下方的範例雖然將 text-align:start;
,但因為又設定了 text-align-last:end;
,就算排列順序擺在前方,權重仍然比較高,最後就會呈現對齊右邊的狀況。
<!-- HTML 程式碼 -->
<div>Hello Oxxo!!</div>
<!-- CSS 程式碼 -->
<style>
div {
border: 1px solid #000;
padding: 10px;
margin: 10px;
width: 400px;
text-align-last: end; /* 雖然擺在前面,但權重仍高於 text-align */
text-align: start;
}
</style>
vertical-align 垂直對齊
vertical-align
樣式屬性可以設定「行內元素和表格元素」與「父元素行框參考線」的垂直對齊 ( 注意,其他顯示類型的元素無法使用垂直對齊樣式 ),沒有繼承特性,這些基準線會根據字體的不同而有所不同 ( 上標線和下標線通常由瀏覽器或作業系統定義 )。
屬性值 | 說明 |
---|---|
baseline | 對齊基準線,預設值。 |
middle | 對齊中線 ( 基本上是小寫字母的一半 )。 |
sub | 對齊下標線。 |
super | 對齊上標線。 |
top | 對齊行框上緣。 |
bottom | 對齊行框下緣。 |
text-top | 對齊文字內容區域上緣。 |
text-bottom | 對齊文字內容區域下緣。 |
長度 | 從基準線提升或減少對應的長度數值。 |
百分比 | 從基準線提升或減少行高的百分比。 |
vertical-align: baseline 垂直對齊基準線
垂直對齊 baseline
是所有行內元素和表格垂直對齊的預設值,會對齊父元素行框的基準線 ( baseline ),通常會有下列幾種情況:
- 注意,通常預設行高是 1.2,表示文字尺寸的 1.2 倍,但不同字體可能會有不同預設值。
- 如果元素裡包含「文字」,就算使用
display
樣式屬性強制更換顯示類型進行垂直對齊,仍然會以元素裡「最後一行文字」的作為對齊的參考依據。
狀況 | 行框高度 |
---|---|
父元素行高 > 子元素最大行高 | 父元素行高 |
父元素行高 < 子元素最大行高 | 子元素最大行高 |
父元素或子元素最大行高 < 圖片高度 | 圖片高度 + 對齊後多出來的行高 |
下方範例展示三種不同的情境,三個情境中的最左邊的 div
展示父元素沒有文字但仍然會產生行框,但文字尺寸仍然會影響行框高度,中間的 div
表示父元素有文字,最右邊的粉紅色 div
是 60px 高度的對照參考 ( 如果父元素文字尺寸 50px,行高 1.2 會等於 60px ),可以特別注意當子元素行高較大 ( 文字尺寸較大所以行高較大 ) 或出現高度較高圖片的時候,就會發生行框高度被撐開的狀況,雖然被行框撐開,但所有元素都會對齊父元素文字的 baseline,這也是為什麼圖片進行垂直置中對齊時,常常會出現底部空白空間的原因。
- 如果不希望子元素行高影響行框高度,可調降子元素行高數值,直到小於父元素行高。
- 線上展示:https://codepen.io/oxxo/pen/LYoOXEG
<!-- HTML 程式碼 -->
父元素行高大於子元素行高
<section class="s1">
<div><span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
父元素行高小於子元素行高
<section class="s2">
<div><span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
父元素行高小於子元素行高、加入圖片
<section class="s3">
<div><span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2"></div>
</section>
<!-- CSS 程式碼 -->
<style>
section {
height: 120px;
margin: 0 0 30px;
}
div {
background: orange;
float: left;
line-height: 1.2; /* 設定行高為 1.2,因為不同字體預設值會有所不同 */
font-size: 50px; /* 父元素行高 50 x 1.2 = 60px */
}
.d1 {margin-left: 10px;}
div * {vertical-align: baseline;} /* 所有子元素對齊 baseline */
.s1 .a {
font-size: 20px; /* .s1 子元素行高 20 x 1.2 = 24px,使用父元素 60px 撐開行框 */
background: #0f08;
}
.s1 .b {
font-size: 10px; /* .s1 子元素行高 10 x 1.2 = 12px,使用父元素 60px 撐開行框 */
background: #00f6;
}
.s2 .a {
font-size: 100px; /* .s2 子元素行高 100 x 1.2 = 120px,使用 .a 子元素 120px 撐開行框 */
background: #0f08;
}
.s2 .b {
font-size: 80px; /* .s2 子元素行高 80 x 1.2 = 96px,使用 .a 子元素 120px 撐開行框 */
background: #00f6;
}
.s3 .a {
font-size: 80px; /* .s3 子元素行高 80 x 1.2 = 96px,和父元素行高都小於圖片高度 150px,對齊後和圖片高度計算產生最後高度 */
background: #0f08;
}
.s3 .b {
font-size: 20px; /* .s3 子元素行高 20 x 1.2 = 24px,和父元素行高都小於圖片高度 150px,對齊後和圖片高度計算產生最後高度 */
background: #00f6;
}
img {width: 50px;}
.img1 {height: 150px;}
.img2 {height: 20px;}
.d2 {
margin-left: 10px;
height: 60px;
background: pink;
width: 20px;
}
</style>
vertical-align: middle 垂直對齊中線
垂直對齊 middle
是「最容易搞錯」的樣式屬性,表示用「子元素」的中線,去對齊父元素行框中「小寫字母的中線」,因為是對齊小寫字母中線,所以對齊時往往會發現「無法垂直置中」的狀況 ( 因為不是子元素中線對齊行框中線 ),但如果子元素高度大於父元素,反而是父元素會發生移動,讓子母中線去去對齊子元素中線,就會真正垂直置中,下方列出幾種常見的狀況:
狀況 | 行框高度 | 真正垂直置中 |
---|---|---|
父元素行高 > 子元素最大行高 | 父元素行高 | X |
父元素行高 < 子元素最大行高 | 子元素最大行高 | O |
父元素行高 > 圖片高度或子元素最大行高 | 父元素行高 | X |
父元素或子元素最大行高 < 圖片高度 | 圖片高度 | O |
下方範例展示四種不同的情境,情境中的最左邊的 div
展示父元素沒有文字但仍然會產生行框,但文字尺寸仍然會影響行框高度,中間的 div
表示父元素有文字,最右邊的粉紅色 div
是 120px 高度的對照參考 ( 如果父元素文字尺寸 100px,行高 1.2 會等於 120px ),可以特別注意當父元行高較大的時候,會發生子元素去對齊父元素小寫字母中線,無法真正垂直置中。
- 如果不希望子元素行高影響行框高度,可調降子元素行高數值,直到小於父元素行高。
- 線上展示:https://codepen.io/oxxo/pen/JjqOeGY
<!-- HTML 程式碼 -->
父元素行高大於子元素行高
<section class="s1">
<div><span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d1">Gfg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
父元素行高小於子元素行高
<section class="s2">
<div><span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d1">Gfg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
父元素行高和子元素的最大行高都小於圖片高度
<section class="s3">
<div><span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d1">Gfg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2"></div>
</section>
父元素行高大於子元素的最大行高或圖片高度
<section class="s4">
<div><span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d1">Gfg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2"></div>
</section>
<!-- CSS 程式碼 -->
<style>
section {
height: 120px;
margin: 0 0 50px;
}
div {
background: orange;
float: left;
line-height: 1.2; /* 設定行高為 1.2,因為不同字體預設值會有所不同 */
font-size: 100px; /* 父元素行高 100 x 1.2 = 120px */
}
.d1 {margin-left: 10px;}
div * {vertical-align: middle;}
.s1 .a {
font-size: 50px; /* .s1 子元素行高 50 x 1.2 = 60px,使用父元素 120px 撐開行框,子元素中線對齊父元素小寫字母中線 */
background: #0f08;
}
.s1 .b {
font-size: 30px; /* .s1 子元素行高 30 x 1.2 = 36px,使用父元素 120px 撐開行框,子元素中線對齊父元素小寫字母中線 */
background: #00f6;
}
.s2 .a {
font-size: 120px; /* .s2 子元素行高 120 x 1.2 = 144px,使用 144px 撐開行框,父元素小寫字母中線去對齊子元素中線 */
background: #0f08;
}
.s2 .b {
font-size: 80px; /* .s2 子元素行高 80 x 1.2 = 96px,使用 144px 撐開行框,中線去對齊最大的子元素中線 */
background: #00f6;
}
.s3 .a,
.s4 .a{
font-size: 80px; /* .s3 .s4 子元素行高 80 x 1.2 = 96px,和圖片判斷哪個比較高,然後對齊中線 */
background: #0f08;
}
.s3 .b,
.s4 .b{
font-size: 20px; /* .s3 .s4 子元素行高 20 x 1.2 = 24px,和圖片判斷哪個比較高,然後對齊中線 */
background: #00f6;
}
img {width: 50px;}
.img1 {height: 150px;}
.img2 {height: 20px;}
.s4 .img1 {height: 50px;}
.d2 {
margin-left: 10px;
height: 120px;
background: pink;
width: 20px;
}
</style>
因為 middle
屬性值的特性,在進行垂直置中時,其中一種方式會先在父元素建立一個「虛擬元素」,並將其顯示屬性改為 inline-block
,接著將這個虛擬元素撐開到比父元素預設行框高度還要大 ( 改變父元素的行高、改變虛擬元素高度等做法 ),如此就能採用類似上方範例「圖片高度較大」的對齊方式,讓子元素全部都對齊這個虛擬元素的中線,實現真正垂直對齊的效果,下方範例將父元素行高設為 0.5,並設定虛擬元素高度為 100px,實現真正垂直置中效果。
<!-- HTML 程式碼 -->
<div><span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d1">Gfg<span class="a">Gg</span><span class="b">Gg</span></div>
<!-- CSS 程式碼 -->
<style>
div {
background: orange;
float: left;
line-height: 0.5; /* 行高為 100 x 0.5 = 50px,小於虛擬元素高度,對齊虛擬元素 */
font-size: 100px;
}
div * { vertical-align: middle;}
.d1 {margin-left: 10px;}
.a {
font-size: 50px; /* 行高為 50 x 0.5 = 25px,小於虛擬元素高度,對齊虛擬元素 */
background: #0f08;
}
.b {
font-size: 30px; /* 行高為 30 x 0.5 = 15px,小於虛擬元素高度,對齊虛擬元素 */
background: #00f6;
}
div::before {
content: "";
display: inline-block; /* 改變虛擬元素顯示類型 */
height: 100px; /* 改變虛擬元素高度為 100px */
width: 0;
vertical-align: middle; /* 虛擬元素要額外設定垂直對齊 */
}
</style>
vertical-align: sub | super 垂直對齊下標、上標
sub
和 super
使用後會將行內元素的「baseline 基準線」,對齊父元素行框文字的「上標線」和「下標線」,如果是圖片、表格或表單元素,會使用底線作為 baseline 進行對齊,但因為對齊後子元素的行高可能會超過行框高度,可能會有下列幾種情況:
注意,如果元素裡包含「文字」,就算使用
display
樣式屬性強制更換顯示類型進行垂直對齊,仍然會以元素裡「最後一行文字」的作為「上標線」和「下標線」的參考依據。
狀況 | 行框高度 |
---|---|
子元素對齊後,行高超過行框高度 | 高度加上超過的數值 |
子元素對齊後,行高小於行框高度 | 行框原本高度 ( 父元素行高決定 ) |
圖片對齊後,高度超過行框高度 | 高度加上超過的數值 |
圖片對齊後,高度小於行框高度 | 行框原本高度 ( 父元素行高決定 ) |
下方範例會展示多種對齊 super
的狀況,最後方的粉紅 div
是行框原始高度的對照,可以特別觀察對齊後撐開行框高度的元素 ( 由於對齊 sub
下標線的做法和對齊上標線完全相同,就只單純展示上標線,可自行修改為 sub
觀察對齊下標線效果 )。
- 如果不希望子元素行高影響行框高度,可調降子元素行高數值,直到小於父元素行高。
- 線上展示:https://codepen.io/oxxo/pen/oNRoQZY
<!-- HTML 程式碼 -->
子元素對齊後行高沒有超過行框高度
<section class="s1">
<div><span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
子元素對齊後行高超過行框高度
<section class="s2">
<div><span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2"></div>
</section>
圖片對齊後高度超過行框高度
<section class="s3">
<div><span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2"></div>
</section>
圖片對齊後高度沒有超過行框高度
<section class="s4">
<div><span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2"></div>
</section>
<!-- CSS 程式碼 -->
<style>
section {
height: 120px;
margin: 0 0 50px;
}
div {
background: orange;
float: left;
line-height: 1.2; /* 設定行高為 1.2,因為不同字體預設值會有所不同 */
font-size: 100px; /* 父元素行高 100 x 1.2 = 120px */
}
.d1 {margin-left: 10px;}
section div * {vertical-align: super;}
.s1 div * {line-height: 0;}
.a {
font-size: 50px; /* 基本上子元素行高 50 x 1.2 = 60px,使用父元素 120px 撐開行框 */
background: #0f08;
}
.b {
font-size: 30px; /* 基本上子元素行高 30 x 1.2 = 36px,使用父元素 120px 撐開行框 */
background: #00f6;
}
.s2 div * {line-height: 3;} /* .s2 子元素行高 50 x 3 = 150px,使用父元素 150px 撐開行框 */
img {width: 50px;}
.img1 {height: 150px;}
.img2 {height: 20px;}
.s3 .img1 {height: 50px;}
.d2 {
margin-left: 10px;
height: 120px;
background: pink;
width: 20px;
}
</style>
vertical-align: top | bottom 垂直對齊行框上緣、下方
top
和 bottom
使用後會對齊父元素「行框」的上緣或下緣,在大部分的情況下,可以很有效的靠上或靠下對齊,但如果有行高較高的子元素,也是會發生將父元素行框撐開的狀況,通常有下列幾種情境:
狀況 | 行框高度 |
---|---|
行框高度 > 子元素和父元素計算的最大高度 | 不變 |
行框高度 < 子元素和父元素計算的最大高度 | 被撐開 |
下方範例會展示對齊行框上緣與下緣的情境,粉紅色 div
是正常的行框高度,可以注意當子元素行高過高,或圖片高度過高時,會發生撐開行框的狀況。
<!-- HTML 程式碼 -->
父元素行框高度不變
<section class="s1">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d3">Gg</div>
</section>
父元素行框高度被撐開
<section class="s2">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d3">Gg</div>
</section>
父元素行框高度不變
<section class="s3">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d3">Gg</div>
</section>
父元素行框高度被撐開
<section class="s4">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d3">Gg</div>
</section>
<!-- CSS 程式碼 -->
<style>
section {
height: 120px;
margin: 0 0 100px;
}
div {
background: orange;
float: left;
line-height: 1;
font-size: 120px;
}
.d1, .d2, .d3 {margin-left: 5px;}
.d3 {background: pink;}
div * {line-height: 2;}
.d1 * {vertical-align: top;}
.d2 * {vertical-align: bottom;}
.s1 .a, .s3 .a {
font-size: 50px;
background: #0f08;
}
.s1 .b, .s3 .b {
font-size: 20px;
background: #06f8;
}
.s2 .a, .s4 .a {
line-height: 4; /* 讓子元素行高較高,撐開行框 */
font-size: 50px;
background: #0f08;
}
.s2 .b, .s4 .b {
font-size: 20px;
background: #06f8;
}
img {width: 50px;}
.img1 {height: 100px;}
.img2 {height: 20px;}
.s4 .img1 {height: 300px;} /* 讓圖片較高,撐開行框 */
</style>
vertical-align: text-top | text-bottom 對齊文字內容區域上方、下方
text-top
和 text-bottom
使用後會對齊行框「文字內容區域」的上緣或下緣,特別注意因為每種字型的內容區域高度不同,且內容區域無法透過樣式屬性修改,如果使用不同字體,很容易出現無法準確對齊的狀況,因此如果要進行靠上或靠下對齊,建議採用 top
或 bottom
會更為準確,下方列出一些常見的使用情況:
狀況 | 行框高度 |
---|---|
行框高度 > 子元素和父元素計算的最大高度 | 不變 |
行框高度 < 子元素和父元素計算的最大高度 | 被撐開,如果是靠下對齊,內容區域下移 |
下方範例會展示對齊行框文字內容區域上緣與下緣的情境,如果要呈現準確的內容區域,可以在使用字體時不要設定行高,所呈現的高度就是內容區域高度,範例中粉紅色 div
是父元素行框內容區域高度高度,可以注意當子元素行高過高,或圖片高度過高時,會發生撐開行框的狀況,也會一併造成父元素行框內容區域往下移動。
<!-- HTML 程式碼 -->
父元素行框高度不變
<section class="s1">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d3">Gg</div>
</section>
父元素行框高度被撐開
<section class="s2">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span></div>
<div class="d3">Gg</div>
</section>
父元素行框高度不變
<section class="s3">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d3">Gg</div>
</section>
父元素行框高度被撐開
<section class="s4">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d3">Gg</div>
</section>
<!-- CSS 程式碼 -->
<style>
section {
height: 120px;
margin: 0 0 100px;
}
div {
background: orange;
float: left;
font-size: 120px; /* 不要設定行高,行高就會等於內容區域,呈現內容區域高度 */
}
.d1, .d2, .d3 {margin-left: 5px;}
.d3 {background: pink;}
div * {line-height: 2;}
.d1 * {vertical-align: top;}
.d2 * {vertical-align: bottom;}
.s1 .a, .s3 .a {
font-size: 50px;
background: #0f08;
}
.s1 .b, .s3 .b {
font-size: 20px;
background: #06f8;
}
.s2 .a, .s4 .a {
line-height: 4; /* 讓子元素行高較高,撐開行框 */
font-size: 50px;
background: #0f08;
}
.s2 .b, .s4 .b {
font-size: 20px;
background: #06f8;
}
img {width: 50px;}
.img1 {height: 100px;}
.img2 {height: 20px;}
.s4 .img1 {height: 240px;} /* 讓圖片較高,撐開行框 */
</style>
vertical-align: 長度
使用「長度」作為垂直對齊的屬性後,會將所設定元素的 baseline,對齊父元素行框文字的「baseline + 長度」,正值向上升高,負值向下降低,如果使用 px 則為絕對長度單位,使用 em 為單位則會乘以元素字體的尺寸,如果子元素行高或圖片行高超過父元素行框高度,就會發生撐開行框的狀況。
<!-- HTML 程式碼 -->
父元素行框高度不變 ( 向下還是撐開了 )
<section class="s1">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d3">Gg</div>
</section>
父元素行框高度被撐開
<section class="s2">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d3">Gg</div>
</section>
<!-- CSS 程式碼 -->
<style>
section {
height: 120px;
margin: 0 0 100px;
}
div {
background: orange;
float: left;
font-size: 120px;
}
.d1, .d2, .d3 {margin-left: 5px;}
.d3 {background: pink;}
.d1 * {vertical-align: 50px;}
.d2 * {vertical-align: -50px;}
div .a{
font-size: 50px;
background: #0f08;
}
div .b {
font-size: 20px;
background: #06f8;
}
img {width: 30px;}
.img1 {height: 50px;}
.img2 {height: 20px;}
.s2 .img1 {height: 240px;}
</style>
vertical-align: 百分比
使用「長度」作為垂直對齊的屬性後,會將所設定元素的 baseline,對齊父元素行框文字的「baseline + 子元素行高的百分比」,正值向上升高,負值向下降低,因為是使用子元素的行高,如果計算後行高不同,就會對齊在不同的位置,如果是「圖片或表格」等沒有行高的元素,會使用行框高度的百分比,如果子元素行高或圖片行高超過父元素行框高度,就會發生撐開行框的狀況。
<!-- HTML 程式碼 -->
父元素行框高度不變
<section class="s1">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d3">Gg</div>
</section>
父元素行框高度被撐開
<section class="s2">
<div class="d1">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d2">Gg<span class="a">Gg</span><span class="b">Gg</span><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img1"><img src="https://www.oxxostudio.tw/img/articles/201405/about-me.jpg" class="img2"></div>
<div class="d3">Gg</div>
</section>
<!-- CSS 程式碼 -->
<style>
section {
height: 150px;
margin: 0 0 100px;
}
div {
background: orange;
float: left;
font-size: 120px;
line-height: 2;
}
.d1, .d2, .d3 {margin-left: 5px;}
.d3 {background: pink;}
.d1 * {vertical-align: 30%;}
.d2 * {vertical-align: -30%;}
div .a{
font-size: 50px;
background: #0f08;
}
div .b {
font-size: 20px;
background: #06f8;
}
img {width: 30px;}
.img1 {height: 50px;}
.img2 {height: 20px;}
.s2 .img1 {height: 240px;}
</style>
小結
水平對齊和垂直對齊是排版中非常重要的技巧,只要熟練應用這篇教學所介紹的對齊樣式和屬性,就能輕鬆掌握排版對齊的方式。
意見回饋
如果有任何建議或問題,可傳送「意見表單」給我,謝謝~