iT邦幫忙

2025 iThome 鐵人賽

DAY 9
0
自我挑戰組

前端轉職學習記錄系列 第 23

DAY21- CSS 基礎-多行對齊:align-content

  • 分享至 

  • xImage
  •  

拉伸行(預設)

css

.content-stretch {
  display: flex;
  flex-wrap: wrap;/*允許換行*/
  align-content: stretch;/*拉伸每一行間距*/
  height: 100px;/*固定高度*/
  width: 250px;
  background: #f0f8ff;
  border: 2px solid #007bff;
  gap: 10px;/*子元素之間的間距10px*/
}

.flex-item {
  display: flex;
  
  background: #4caf50;
  color: white;
  height: 20px;
  width: 50px;
  align-items: center;/* 垂直方向置中內容 */
  justify-content: center;/* 水平方向置中內容 */
  border-radius: 4px;
 }

html

 <div class="content-stretch">
   <div class="flex-item">1</div>
   <div class="flex-item">2</div>
   <div class="flex-item">3</div>
   <div class="flex-item">4</div>
   <div class="flex-item">5</div>
   <div class="flex-item">6</div>
   <div class="flex-item">7</div>
   <div class="flex-item">8</div>
   <div class="flex-item">9</div>
 </div>

https://ithelp.ithome.com.tw/upload/images/20251007/20175965pIEu38uWZ5.png


行頂部對齊

css

.content-start {
display: flex;
flex-wrap: wrap;
align-content: flex-start;
height: 200px;
width: 250px;
background: #f0f8ff;
border: 2px solid #007bff;
gap: 10px;
}
.flex-item {
display: flex;

background: #4caf50;
color: white;
height: 20px;
width: 50px;
align-items: center;/* 垂直方向置中內容 */
justify-content: center;/* 水平方向置中內容 */
border-radius: 4px;
}

html與stretch相同
https://ithelp.ithome.com.tw/upload/images/20251007/20175965i8f04wN5xn.png


行居中對齊

css

.content-center {
display: flex;
flex-wrap: wrap;
align-content: center;
height: 200px;
width: 250px;
background: #f0f8ff;
border: 2px solid #007bff;
gap: 10px;
}
.flex-item {
display: flex;
background: #4caf50;
color: white;
height: 20px;
width: 50px;
align-items: center;
justify-content: center;
border-radius: 4px;
}

html與stretch相同
https://ithelp.ithome.com.tw/upload/images/20251007/20175965hs7yBqj6Q2.png


行間距分散

css

.content-between {
 display: flex;
 flex-wrap: wrap;
 align-content: space-between;
 height: 200px;
 width: 250px;
 background: #f0f8ff;
 border: 2px solid #007bff;
 gap: 10px;
}
.flex-item {
 display: flex;
 background: #4caf50;
 color: white;
 height: 20px;
 width: 50px;
 align-items: center;
 justify-content: center;
 border-radius: 4px;
}

html與stretch相同
https://ithelp.ithome.com.tw/upload/images/20251007/20175965IT0zVrOYPV.png

https://ithelp.ithome.com.tw/upload/images/20251007/20175965C3upsSGtJZ.png
https://ithelp.ithome.com.tw/upload/images/20251007/20175965L6T7E3MiNS.png


上一篇
DAY20- CSS 基礎-交叉軸對齊練習
系列文
前端轉職學習記錄23
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言