您的位置首页生活百科

CSS3弹性盒子之内容对齐justify-content

CSS3弹性盒子之内容对齐justify-content

的有关信息介绍如下:

CSS3弹性盒子之内容对齐justify-content

弹性盒子(box-flex)是CSS3新增的一种布局模式,相比传统的布局模式来说,它更简单好用,而不存在浮动元素脱离正常文档流之后需要在某些地方清除浮动的问题。但是该属性目前只有部分浏览器支持,因此在pc端开发中应用的比较少。但是对于移动端,webkit核心浏览器几乎一统天下,web界面的制作上,使用弹性盒子是非常不错的!

CSS3弹性盒子justify-content(内容对齐)属性应用在弹性容器上,把弹性项(子元素)沿着弹性容器的主轴线(main axis)对齐。

justify-content 语法:

justify-content: flex-start | flex-end | center | space-between | space-around

flex-start:默认值,弹性子元素向行头紧挨着填充。

例子:

css部分:

.father1{

width:500px;

height:400px;

background: slategrey;

margin:20px auto;

display: -webkit-flex;

display:flex;

-webkit-justify-content: flex-start;

justify-content: flex-start;

}

.son1{

width:100px;

height:100px;

border:2px solid darkslategray;

background: darkseagreen;

margin:10px;

line-height: 100px;

text-align: center;

color: #fff;

}

html部分:

内容对齐1

内容对齐2

内容对齐3

效果如图:

justify-content:flex-end;

弹性子元素向行尾紧挨着填充。第一个弹性子元素的main-end外边距边线被放置在该行的main-end边线,而后续弹性子元素依次平齐摆放。

例子:

css部分:

.father2{

width:500px;

height:400px;

background: slategrey;

margin:20px auto;

display: -webkit-flex;

display:flex;

-webkit-justify-content: flex-end;

justify-content: flex-end;

}

.son2{

width:100px;

height:100px;

border:2px solid darkslategray;

background: darkseagreen;

margin:10px;

line-height: 100px;

text-align: center;

color: #fff;

}

html部分:

内容对齐1

内容对齐2

内容对齐3

效果如图:

justify-content:center;

弹性项目居中紧挨着填充。但是如果剩余的自由空间是负的,则弹性项目将在两个方向上同时溢出。

例子:

css部分:

.father3{

width:500px;

height:400px;

background: lightcoral;

margin:20px auto;

display: -webkit-flex;

display:flex;

-webkit-justify-content: center;

justify-content: center;

}

.son3{

width:100px;

height:100px;

border:2px solid crimson;

background: coral;

margin:10px;

line-height: 100px;

text-align: center;

color: #fff;

}

html部分:

内容对齐1

内容对齐2

内容对齐3

效果如图:

justify-content:space-between;

弹性项目平均分布在该行上。如果剩余空间为负或者只有一个弹性项,则该值等同于flex-start。否则,第1个弹性项的外边距和行的main-start边线对齐,而最后1个弹性项的外边距和行的main-end边线对齐,然后剩余的弹性项分布在该行上,相邻项目的间隔相等。

例子:

css部分:

.father4{

width:500px;

height:400px;

background: midnightblue;

margin:20px auto;

display: -webkit-flex;

display:flex;

-webkit-justify-content: space-between;

justify-content: space-between;

}

.son4{

width:100px;

height:100px;

border:2px solid darkblue;

background: cornflowerblue;

margin:10px;

text-align: center;

color: #fff;

}

html部分:

space-between1

space-between2

space-between3

效果如图:

justify-content:space-around;

弹性项目平均分布在该行上,两边留有一半的间隔空间。如果剩余空间为负或者只有一个弹性项,则该值等同于center。否则,弹性项目沿该行分布,且彼此间隔相等(比如是20px),同时首尾两边和弹性容器之间留有一半的间隔(1/2*20px=10px)。

例子:

css部分:

.father5{

width:500px;

height:400px;

background: midnightblue;

margin:20px auto;

display: -webkit-flex;

display:flex;

-webkit-justify-content: space-around;

justify-content: space-around;

}

.son5{

width:100px;

height:100px;

border:2px solid darkblue;

background: cornflowerblue;

margin:10px;

text-align: center;

color: #fff;

}

html部分:

space-around1

space-around2

space-around3

效果如图: