CSS Flexbox 之 justify-content

栏目: CSS · 发布时间: 7年前

内容简介:Flexbox 的CSS 3

Flexbox 的 justify-content 決定 Container 中的 Item 該 靠左 、 靠右 或 置中 對齊,但 靠左對其 或 靠右對齊 是由 justify-content 與 row-direction 共同決定。

Version

CSS 3

Layout

CSS Flexbox 之 justify-content

RGB 由左至右 開始長,因為 item 總和小於 container,所以長到 900px 就停止,右側灰色為 container 底色。

HTML

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Flex</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="box">
    <div class="item item1">1</div>
    <div class="item item2">2</div>
    <div class="item item3">3</div>
  </div>
</body>
</html>

對 3 個 <div> 做 layout。

CSS

style.css

.box {
  display: flex;
  justify-content: flex-start;
  width: 960px;
  height: 240px;
  margin: auto;
  background-color: #d3d3d3;
}

.item {
  width: 300px;
}

.item1 {
  background-color: #faa;
}

.item2 {
  background-color: #afa;
}

.item3 {
  background-color: #aff;
}

第 1行

.box {
  display: flex;
  justify-content: flex-start;
}

除了設定 display 為 flex 外,還加上 justify-content 為 flex-start 。

justify-content: flex-start

從 flexbox 的開始處展開,並不一定是 由左至右 或 由上至下 ,而是由 flex-direction 決定

CSS Flexbox 之 justify-content

若沒特別指定 flex-direction ,預設為 由左至右 ,視覺上相當於 靠左對齊 。

.box 
  display: flex;
  justify-content: flex-start;
  flex-direction: row-reverse;
}

若沒特別指定 flex-direction ,預設 flex-direction 為 row ,表示 由左至右 ,若特別指定為 row-reverse ,則為 由右至左 。

flex-direction: row-reverse

Flexbox 由右至左 展開

CSS Flexbox 之 justify-content

視覺上相當於 靠右對齊 。

.box {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
}

若設定 flex-direction 為 column ,表示 由上至下 。

flex-direction: column

Flexbox 由上至下 展開

CSS Flexbox 之 justify-content

視覺上相當於 靠上對齊 。

.box {
  display: flex;
  justify-content: flex-start;
  flex-direction: column-reverse;
}

若設定 flex-direction 為 column-reverse ,表示 由下至上 。

flex-direction: column-reverse

Flexbox 由下至上 展開

CSS Flexbox 之 justify-content

視覺上相當於 靠下對齊 。

.box {
  display: flex;
  justify-content: center;
}

若設定 justify-content 為 center ,表示 item 將 置中 於 container。

CSS Flexbox 之 justify-content

視覺上相當於 置中對齊 。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Android开发艺术探索

Android开发艺术探索

任玉刚 / 电子工业出版社 / 2015-9-1 / CNY 79.00

《Android开发艺术探索》是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。《Android开发艺术探索》从三个方面来组织内容。第一,介绍Android开发者不容易掌握的一些知识点;第二,结合Android源代码和应用层开发过程,融会贯通,介绍一些比较深入的知识点;第三,介绍一些核心技术和Android的性能优化思想。 《Andro......一起来看看 《Android开发艺术探索》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具