- Flexbox 教程
- Flexbox - 首頁
- Flexbox - 概述
- Flexbox - 彈性容器
- Flexbox - Flex-Direction
- Flexbox - Flex-Wrap
- Flexbox - 對齊內容
- Flexbox - 對齊專案
- Flexbox - 對齊內容
- Flexbox - Flex-Order
- Flexbox - 靈活性
- Flexbox - 自對齊
- Flexbox 有用資源
- Flexbox - 快速指南
- Flexbox - 有用資源
- Flexbox - 討論
Flexbox - 對齊專案
align-items 屬性與 justify-content 屬性類似。但在這裡,專案是在交叉軸線(垂直方向)上對齊的。
用法 -
align-items: flex-start | flex-end | center | baseline | stretch;
此屬性接受以下值 -
flex-start - 彈性專案在容器頂部垂直對齊。
flex-end - 彈性專案在容器底部垂直對齊。
flex-center - 彈性專案在容器中心垂直對齊。
stretch - 彈性專案垂直對齊,以填充容器的整個垂直空間。
baseline - 彈性專案對齊,使它們的文字基線沿水平線對齊。
flex-start
將此值傳遞給 align-items 屬性時,彈性專案將在容器頂部垂直對齊。
以下示例演示了將 flex-start 值傳遞給 align-items 屬性的結果。
<!doctype html>
<html lang = "en">
<style>
.box1{background:green;}
.box2{background:blue;}
.box3{background:red;}
.box4{background:magenta;}
.box5{background:yellow;}
.box6{background:pink;}
.box{
font-size:35px;
padding:15px;
}
.container{
display:flex;
height:100vh;
align-items:flex-start;
}
</style>
<body>
<div class = "container">
<div class = "box box1">One</div>
<div class = "box box2">two</div>
<div class = "box box3">three</div>
<div class = "box box4">four</div>
<div class = "box box5">five</div>
<div class = "box box6">six</div>
</div>
</body>
</html>
它將產生以下結果 -
flex-end
將此值傳遞給 align-items 屬性時,彈性專案將在容器底部垂直對齊。
以下示例演示了將 flex-end 值傳遞給 align-items 屬性的結果。
<!doctype html>
<html lang = "en">
<style>
.box1{background:green;}
.box2{background:blue;}
.box3{background:red;}
.box4{background:magenta;}
.box5{background:yellow;}
.box6{background:pink;}
.box{
font-size:35px;
padding:15px;
}
.container{
display:flex;
height:100vh;
align-items:flex-end;
}
</style>
<body>
<div class = "container">
<div class = "box box1">One</div>
<div class = "box box2">two</div>
<div class = "box box3">three</div>
<div class = "box box4">four</div>
<div class = "box box5">five</div>
<div class = "box box6">six</div>
</div>
</body>
</html>
它將產生以下結果 -
center
將此值傳遞給 align-items 屬性時,彈性專案將在容器中心垂直對齊。
以下示例演示了將 flex-center 值傳遞給 align-items 屬性的結果。
<!doctype html>
<html lang = "en">
<style>
.box1{background:green;}
.box2{background:blue;}
.box3{background:red;}
.box4{background:magenta;}
.box5{background:yellow;}
.box6{background:pink;}
.box{
font-size:35px;
padding:15px;
}
.container{
display:flex;
height:100vh;
align-items:center;
}
</style>
<body>
<div class = "container">
<div class = "box box1">One</div>
<div class = "box box2">two</div>
<div class = "box box3">three</div>
<div class = "box box4">four</div>
<div class = "box box5">five</div>
<div class = "box box6">six</div>
</div>
</body>
</html>
它將產生以下結果 -
stretch
將此值傳遞給 align-items 屬性時,彈性專案將垂直對齊,以填充容器的整個垂直空間。
以下示例演示了將 stretch 值傳遞給 align-items 屬性的結果。
<!doctype html>
<html lang = "en">
<style>
.box1{background:green;}
.box2{background:blue;}
.box3{background:red;}
.box4{background:magenta;}
.box5{background:yellow;}
.box6{background:pink;}
.box{
font-size:35px;
padding:15px;
}
.container{
display:flex;
height:100vh;
align-items:stretch;
}
</style>
<body>
<div class = "container">
<div class = "box box1">One</div>
<div class = "box box2">two</div>
<div class = "box box3">three</div>
<div class = "box box4">four</div>
<div class = "box box5">five</div>
<div class = "box box6">six</div>
</div>
</body>
</html>
它將產生以下結果 -
baseline
將此值傳遞給 align-items 屬性時,彈性專案將對齊,使它們的文字基線沿水平線對齊。
以下示例演示了將 baseline 值傳遞給 align-items 屬性的結果。
<!doctype html>
<html lang = "en">
<style>
.box1{background:green;}
.box2{background:blue;}
.box3{background:red;}
.box4{background:magenta;}
.box5{background:yellow;}
.box6{background:pink;}
.box{
font-size:35px;
padding:15px;
}
.container{
display:flex;
height:100vh;
align-items:baseline;
}
</style>
<body>
<div class = "container">
<div class = "box box1">One</div>
<div class = "box box2">two</div>
<div class = "box box3">three</div>
<div class = "box box4">four</div>
<div class = "box box5">five</div>
<div class = "box box6">six</div>
</div>
</body>
</html>
它將產生以下結果 -
廣告