CSS3 弹性盒子属性解析

news/2024/5/19 20:16:08 标签: html, css, flex, css3
htmledit_views">

1、CSS3 弹性盒子(Flex Box)

  • 弹性盒子是 CSS3 的一种新的布局模式。
  • CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式。
  • 引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子元素进行排列、对齐和分配空白空间。
  • 传统的布局方案,基于css盒子模型,float+display+position,对于很多特殊布局方案就非常不方便

2、CSS3 弹性盒子内容

  • 弹性盒子由弹性容器(Flex container)和弹性子元素(Flex item)组成。
  • 弹性容器通过设置 display 属性的值为 flex 或 inline-flex将其定义为弹性容器。
  • 弹性容器内包含了一个或多个弹性子元素。
  • 注意: 弹性容器外及弹性子元素内是正常渲染的。弹性盒子只定义了弹性子元素如何在弹性容器内布局。
  •            弹性子元素通常在弹性盒子内一行显示。默认情况每个容器只有一行。 

3、flex-direction属性

flex-direction 属性指定了弹性子元素在父容器中的位置。

语法

flex-direction: row | row-reverse | column | column-reverse

flex-direction的值有:

  • row:横向从左到右排列(左对齐),默认的排列方式。
  •  
  • row-reverse:反转横向排列(右对齐,从后往前排,最后一项排在最前面。
  • 实例:
    ​
    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>flex-direction</title>
        <style>
          
            
            .father {
                width: 400px;
                height: 400px;
                margin: 0 auto;
                margin-top: 50px;
                border: 1px solid springgreen;
                display: flex;
                flex-direction: row-reverse;
                
            }
            
            .son {
                width: 100px;
                height: 100px;
                background-color: blueviolet;
                margin-left: 5px;
            }
        </style>
    </head>
    
    <body>
        <div class="father">
            <div class="son">1</div>
            <div class="son">2</div>
            <div class="son">3</div>
        </div>
        <div class="father1">
            <div class="son1">1</div>
            <div class="son1">2</div>
            <div class="son1">3</div>
        </div>
    </body>
    
    </html>​

  • column:纵向排列。
  • 实例:
    ​
    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>flex-direction</title>
        <style>
           
            
            .father {
                width: 400px;
                height: 400px;
                margin: 0 auto;
                margin-top: 50px;
                border: 1px solid springgreen;
                display: flex;
                flex-direction: column;
            }
            
            .son {
                width: 100px;
                height: 100px;
                background-color: blueviolet;
                margin-left: 5px;
            }
        </style>
    </head>
    
    <body>
        <div class="father">
            <div class="son">1</div>
            <div class="son">2</div>
            <div class="son">3</div>
        </div>
        <div class="father1">
            <div class="son1">1</div>
            <div class="son1">2</div>
            <div class="son1">3</div>
        </div>
    </body>
    
    </html>
    
    ​

  • column-reverse:反转纵向排列,从后往前排,最后一项排在最上面。
  • 实例:
    ​
    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>flex-direction</title>
        <style>
            .father {
                width: 400px;
                height: 400px;
                margin: 0 auto;
                margin-top: 50px;
                border: 1px solid springgreen;
                display: flex;
                flex-direction: column;
            }
            
            .son {
                width: 100px;
                height: 100px;
                background-color: blueviolet;
                margin-left: 5px;
            }
        </style>
    </head>
    
    <body>
        <div class="father">
            <div class="son">1</div>
            <div class="son">2</div>
            <div class="son">3</div>
        </div>
        <div class="father1">
            <div class="son1">1</div>
            <div class="son1">2</div>
            <div class="son1">3</div>
        </div>
    </body>
    </html>


4、flex-wrap属性

flex-wrap 属性用于指定弹性盒子的子元素换行方式。

语法

flex-wrap: nowrap| wrap |wrap-reverse| initial | inherit;

各个值解析:

  • nowrap - 默认, 弹性容器为单行。该情况下弹性子项可能会溢出容器。
  • 实例:
    <style>
    	div{
    		display: flex;
    		border: 1px solid #000;
    		flex-direction: row;
    		flex-wrap: nowrap;
    	}
    	p{
    		border: 1px solid red;
    		width: 100px;
    		height: 100px;
    		line-height: 100px;
    		text-align: center;
    		margin-left: 20px;
    	}
    </style>
    <body>
    <div>
    	<p>1</p>
    	<p>2</p>
    	<p>3</p>
    	<p>4</p>
    	<p>5</p>
    	<p>6</p>
    	<p>7</p>
    	<p>8</p>
    </div>
    </body>
    

  • wrap - 弹性容器为多行。该情况下弹性子项溢出的部分会被放置到新行,子项内部会发生断行
  • 实例:
    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .flex-container {
                display: -webkit-flex;
                display: flex;
                flex-wrap: wrap;
                width: 300px;
                height: 250px;
                background-color: lightgrey;
            }
            
            .flex-item {
                background-color: cornflowerblue;
                width: 100px;
                height: 100px;
                margin: 10px;
            }
        </style>
    </head>
    
    <body>
    
        <div class="flex-container">
            <div class="flex-item"> item 1</div>
            <div class="flex-item">item 2</div>
            <div class="flex-item"> item 3</div>
        </div>
    
    </body>
    
    </html>

  • wrap-reverse -反转 wrap 排列。
  • 实例:
    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .flex-container {
                display: flex;
                flex-wrap: wrap-reverse;
                width: 300px;
                height: 250px;
                background-color: lightgrey;
            }
            
            .flex-item {
                background-color: cornflowerblue;
                width: 100px;
                height: 100px;
                margin: 10px;
            }
        </style>
    </head>
    
    <body>
    
        <div class="flex-container">
            <div class="flex-item"> item 1</div>
            <div class="flex-item">item 2</div>
            <div class="flex-item"> item 3</div>
        </div>
    
    </body>
    
    </html>

    5、justify-content 属性

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

    justify-content 语法如下:

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

    各个值解析:

  • flex-start:

    弹性项目向行头紧挨着填充。这个是默认值。第一个弹性项的main-start外边距边线被放置在该行的main-start边线,而后续弹性项依次平齐摆放。

  • flex-end:

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

  • center:

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

  • space-between:

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

  • space-around:

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

对比图:


6、align-items 属性

align-items 设置或检索弹性盒子元素在侧轴(纵轴)方向上的对齐方式。

语法

align-items: flex-start | flex-end | center | baseline | stretch

各个值解析:

  • flex-start:弹性盒子元素的侧轴(纵轴)起始位置的边界紧靠住该行的侧轴起始边界。
  • flex-end:弹性盒子元素的侧轴(纵轴)起始位置的边界紧靠住该行的侧轴结束边界。
  • center:弹性盒子元素在该行的侧轴(纵轴)上居中放置。(如果该行的尺寸小于弹性盒子元素的尺寸,则会向两个方向溢出相同的长度)。
  • baseline:如弹性盒子元素的行内轴与侧轴为同一条,则该值与'flex-start'等效。其它情况下,该值将参与基线对齐。
  • stretch:如果指定侧轴大小的属性值为'auto',则其值会使项目的边距盒的尺寸尽可能接近所在行的尺寸,但同时会遵照'min/max-width/height'属性的限制。

7、align-content 属性

align-content 属性用于修改 flex-wrap 属性的行为。类似于 align-items, 但它不是设置弹性子元素的对齐,而是设置各个行的对齐。

语法

align-content: flex-start | flex-end | center | space-between | space-around | stretch

各个值解析:

  • stretch - 默认。各行将会伸展以占用剩余的空间。
  • flex-start - 各行向弹性盒容器的起始位置堆叠。
  • flex-end - 各行向弹性盒容器的结束位置堆叠。
  • center -各行向弹性盒容器的中间位置堆叠。
  • space-between -各行在弹性盒容器中平均分布。
  • space-around - 各行在弹性盒容器中平均分布,两端保留子元素与子元素之间间距大小的一半。

 

8、弹性子元素属性

排序

语法

order: 

各个值解析:

  • <integer>:用整数值来定义排列顺序,数值小的排在前面。可以为负值。

order 属性设置弹性容器内弹性子元素的属性:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-direction</title>
    <style>
        .father {
            width: 400px;
            height: 400px;
            margin: 0 auto;
            margin-top: 50px;
            border: 1px solid springgreen;
            display: flex;
            flex-direction: row;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: blueviolet;
            margin-left: 5px;
        }
        
        .son:nth-child(3) {
            order: -2;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son">1</div>
        <div class="son">2</div>
        <div class="son">3</div>
    </div>

</body>

</html>


9、align-self属性

align-self 属性用于设置弹性元素自身在侧轴(纵轴)方向上的对齐方式。

语法

align-self: auto | flex-start | flex-end | center | baseline | stretch

各个值解析:

  • auto:如果'align-self'的值为'auto',则其计算值为元素的父元素的'align-items'值,如果其没有父元素,则计算值为'stretch'。
  • flex-start:弹性盒子元素的侧轴(纵轴)起始位置的边界紧靠住该行的侧轴起始边界。
  • flex-end:弹性盒子元素的侧轴(纵轴)起始位置的边界紧靠住该行的侧轴结束边界。
  • center:弹性盒子元素在该行的侧轴(纵轴)上居中放置。(如果该行的尺寸小于弹性盒子元素的尺寸,则会向两个方向溢出相同的长度)。
  • baseline:如弹性盒子元素的行内轴与侧轴为同一条,则该值与'flex-start'等效。其它情况下,该值将参与基线对齐。
  • stretch:如果指定侧轴大小的属性值为'auto',则其值会使项目的边距盒的尺寸尽可能接近所在行的尺寸,但同时会遵照'min/max-width/height'属性的限制。

10、对齐

设置"margin"值为"auto"值,自动获取弹性容器中剩余的空间。所以设置垂直方向margin值为"auto",可以使弹性子元素在弹性容器的两上轴方向都完全居中。

完美的居中

以下实例将完美解决我们平时碰到的居中问题。

使用弹性盒子,居中变的很简单,只想要设置 margin: auto; 可以使得弹性子元素在两上轴方向上完全居中:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .big-box {
            width: 800px;
            height: 800px;
            display: flex;
            border: 1px solid springgreen;
        }
        
        .box {
            width: 200px;
            height: 200px;
            background-color: brown;
            margin: auto;
        }
    </style>
</head>

<body>
    <div class="big-box">
        <div class="box">我是完美居中的</div>
    </div>
</body>

</html>


11、flex属性

flex 属性用于指定弹性子元素如何分配空间。

语法

flex: auto | initial | none | inherit |  [ flex-grow ] || [ flex-shrink ] || [ flex-basis ]

各个值解析:

  • auto: 计算值为 1 1 auto
  • initial: 计算值为 0 1 auto
  • none:计算值为 0 0 auto
  • inherit:从父元素继承
  • flex-grow ]:定义弹性盒子元素的扩展比率。
  • flex-shrink ]:定义弹性盒子元素的收缩比率。
  • flex-basis ]:定义弹性盒子元素的默认基准值。

以下实例中,第一个弹性子元素占用了 2/4 的空间,其他两个各占 1/4 的空间:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        .flex-container {
            display: -webkit-flex;
            display: flex;
            width: 400px;
            height: 250px;
            background-color: lightgrey;
        }
        
        .flex-item {
            background-color: green;
            margin: 10px;
        }
        
        .item1 {
            flex: 2;
        }
        
        .item2 {
            flex: 1;
        }
        
        .item3 {
            flex: 1;
        }
    </style>
</head>

<body>

    <div class="flex-container">
        <div class="flex-item item1"> item 1</div>
        <div class="flex-item item2"> item 2</div>
        <div class="flex-item item3"> item 3</div>
    </div>

</body>

</html>


http://www.niftyadmin.cn/n/1381282.html

相关文章

[服务端与网络]一条http请求(一次回车后)到底经历了什么

客户端发出当我们在浏览器地址栏输入一个地址juejin.im/timeline&#xff0c;回车&#xff0c;回车后Http的header包装请求&#xff0c;添加一些需要用到的属性&#xff08;Accrpt...&#xff09;进行域名解析&#xff0c;根据域名找到服务器的IP&#xff0c;发起TCP三次握手&a…

浅析SkipList跳跃表原理及代码实现

2019独角兽企业重金招聘Python工程师标准>>> SkipList在leveldb以及lucence中都广为使用&#xff0c;是比较高效的数据结构。由于它的代码以及原理实现的简单性&#xff0c;更为人们所接受。我们首先看看SkipList的定义&#xff0c;为什么叫跳跃表&#xff1f; “ …

shell中if的讲解及注意事项

If语句格式&#xff1a; Eg: #!/bin/bash if [ “3” –lt “6” ]//要注意if后面有一个空格&#xff0c;“3”和“6”的前后各有一个空格 then echo “3 is less 6” else echo “3 is more 6” fi//注意在结束的时候一定要有结尾的返写否则会出错 eg&#xff1a; 这是另…

H5 新增语义标签

1、HTML5 简介 HTML5是HTML最新的修订版本&#xff0c;2014年10月由万维网联盟&#xff08;W3C&#xff09;完成标准制定。HTML5的设计目的是为了在移动设备上支持多媒体。HTML5 简单易学。1.2、什么是 HTML5? HTML5 是下一代 HTML 标准。HTML , HTML 4.01的上一个版本诞生于…

SQL删除重复数据只保留一条

http://blog.csdn.net/anya/article/details/6407280/ 用SQL语句,删除掉重复项只保留一条在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢1、查找表中多余的重复记录&#xff0c;重复记录是根据单个字段&#xff08;peopleId&#xff09;来判断 select * fr…

promise

## 前言 今天来分享下promise的用法&#xff0c;es6伟大发明之一&#xff0c;当初我学习的时候也是蛮头大的&#xff0c;不知道为啥&#xff0c;整个脑子就是&#xff0c;我在哪&#xff0c;我要干啥的懵圈&#xff0c;后面认真学习之后&#xff0c;觉得真是十分好用&#xff0…

Jupyter(Ipython) Notebook 入门

upyter Notebook&#xff08;此前被称为 IPython notebook&#xff09;是一个交互式笔记本&#xff0c;支持运行 40 多种编程语言。 一般用来编写漂亮的交互式文档。 文学编程的读者不是机器&#xff0c;而是人。 我们从写出让机器读懂的代码&#xff0c;过渡到向人们解说如何让…

ASP.NET MVC Model绑定(二)

ASP.NET MVC Model绑定(二) 前言 上篇对于Model绑定的简单演示想必大家对Model绑定的使用方式有一点的了解&#xff0c;那大家有没有想过Model绑定器是在什么时候运行的&#xff1f;又或是运行的过程是什么样的&#xff1f;将在本篇为大家解除这些疑惑&#xff0c;在当中涉及到…