传统布局方式

目录

一、浮动布局

1.1、属性介绍

1.2、代码实例

二、绝对定位布局

2.1、属性介绍

 2.2、relative、absolute代码实例

 2.3、fixed代码实例

三、弹性盒子布局

3.1、flex-direction

3.1.1、属性说明

3.1.2、代码实例

3.2、flex-wrap

3.2.1、属性说明

3.2.2、代码实例

3.3、flex-flow

3.3.1、属性说明

3.3.2、代码实例

3.4、align-items

3.4.1、属性说明

 3.4.2、代码实例

3.5、justify-content

3.5.1、属性说明

3.5.2、代码实例

3.6、align-content

3.6.1、属性说明

3.6.2、代码实例

一、浮动布局

1.1、属性介绍

float是最开始出现的一种布局方式,帮助程序员处理一些简单的布局。

属性float有以下属性值:

属性值效果说明
none默认值
left元素向左浮动
right元素向右浮动
inherit规定应该从父元素继承 float 属性的值

1.2、代码实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .main{
            width: 300px;
            height: 300px;
            background-color: red;
            
        }
        .main div{
            width: 70px;
            height: 70px;
            float: right;
        }
        
    </style>
</head>
<body>
    <div class="main">
        <div style="background-color: orange;"></div>
        <div style="background-color: deepskyblue;"></div>
        <div style="background-color: greenyellow;"></div>
    </div>
</body>
</html>

二、绝对定位布局

2.1、属性介绍

position定位允许的是从正常的文档流布局中取出元素,并使它们具有不同的行为

属性position有以下属性值:

属性值效果说明
static默认值,无效果
relative相对定位,相对于其正常位置
absolute绝对定位,相对于其父元素
fixed绝对定位,相对于其浏览窗口
inherit规定应该从父元素继承 position 属性的值。

 其中relative、absolute、fixed通常用以下属性定位

属性名

效果说明
top顶部,默认单位:像素px
bottom底部,默认单位:像素px
left左,默认单位:像素px
right右,默认单位:像素px

relative通常与absolute搭配使用,父元素设置relative,子元素absolute。

图层显示优先级用z-index设置

属性名效果说明
z-index设置元素显示优先级,属性值为数值,值越大越上层

 2.2、relative、absolute代码实例

​
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .main{
            width: 300px;
            height: 300px;
            background-color: red;
            position: relative;
            left: 200px;
        }
        .main div{
            width: 70px;
            height: 70px;
            position: absolute;
        }
        #p1{
            background-color: orange;
            left: 0;
            top: 0;
        }
        #p2{
            background-color: deepskyblue;
            right: 0;
            top: 0;
        }
        #p3{
            background-color: greenyellow;
            left: 0;
            bottom: 0;
        }
    </style>
</head>
<body>
    <div class="main">
        <div id="p1"></div>
        <div id="p2"></div>
        <div id="p3"></div>
    </div>
</body>
</html>

​

 2.3、fixed代码实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        body{
            background-color: black;
            height: 2000px;
            width: 100%;
        }
        div{
            width: 50px;
            height: 50px;
            background-color: white;
            position: fixed;
            top: 300px;
            left: 300px;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

三、弹性盒子布局

弹性盒子是一种用于按行或者按列布局元素的一维布局方法。元素可以膨胀以填充额外空间,也可收缩以适应更小的空间。

将一个容器元素设置为弹性盒子只需要设置display元素为: flex || inline-flex。

同时可以为弹性盒子设置以下属性:

属性名效果说明
flex-direction规定弹性项目的方向
flex-wrap规定弹性项目是否应换行
flex-flowflex-direction与flex-wrap的简写
align-items为弹性容器内的项目指定默认对齐方式(
justify-content为弹性容器内的项目指定默认对齐方式(
align-content属性修改flex-wrap属性的行为。它与align-items相似,但是它不对齐弹性项目

3.1、flex-direction

3.1.1、属性说明

flex-direction 属性规定弹性项目的方向。ps:若元素不是弹性盒子,则属性无效

flex-direction有以下属性值:

属性值效果说明
row默认值,作为
row-reverse作为,方向相反
column作为
column-reverse作为,方向相反
initial将此属性设置为其默认值
inherit从其父元素继承此属性

3.1.2、代码实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .main{
            width: 300px;
            height: 300px;
            background-color: red;
            display: flex;
            flex-direction: row-reverse;
        }
        .main div{
            width: 70px;
            height: 70px;
        }
        #p1{
            background-color: orange;
        }
        #p2{
            background-color: deepskyblue;
        }
        #p3{
            background-color: greenyellow;
        }
    </style>
</head>
<body>
    <div class="main">
        <div id="p1"></div>
        <div id="p2"></div>
        <div id="p3"></div>
    </div>
</body>
</html>

3.2、flex-wrap

3.2.1、属性说明

flex-wrap 属性规定弹性项目是否换行。ps:若元素不是弹性盒子,则属性无效。

flex-wrap有以下属性值:

属性值效果说明
nowrap默认值,不换行
wrap需要时换行
wrap-reverse需要时换行,但是反方向
initial将此属性设置为其默认值
inherit从其父元素继承此属性

3.2.2、代码实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .main{
            width: 300px;
            height: 300px;
            background-color: red;
            display: flex;
            flex-wrap: wrap;
        }
        .main div{
            width: 70px;
            height: 70px;
            background-color: black;
        }
        #p1{
            background-color: orange;
        }
        #p2{
            background-color: deepskyblue;
        }
        #p3{
            background-color: greenyellow;
        }
    </style>
</head>
<body>
    <div class="main">
        <div id="p1"></div>
        <div id="p2"></div>
        <div id="p3"></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>

3.3、flex-flow

3.3.1、属性说明

flex-flow 属性是flex-direction和flex-wrap的缩写

基本格式为:flex-flow: [flex-direction flex-wrap] or [initial] or [inherit];

属性值效果与flex-direction、flex-wrap一致。

3.3.2、代码实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .main{
            width: 300px;
            height: 300px;
            background-color: red;
            display: flex;
            flex-flow: column wrap;
        }
        .main div{
            width: 70px;
            height: 70px;
            background-color: black;
        }
        #p1{
            background-color: orange;
        }
        #p2{
            background-color: deepskyblue;
        }
        #p3{
            background-color: greenyellow;
        }
    </style>
</head>
<body>
    <div class="main">
        <div id="p1"></div>
        <div id="p2"></div>
        <div id="p3"></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>

3.4、align-items

3.4.1、属性说明

align-items属性为弹性容器内的项目指定默认对齐方式(横)。

align-items有以下属性值:

属性值效果说明
stretch默认、项目被拉伸以适合容器
center项目位于容器的中央
flex-start项目位于容器的开头
flex-end项目位于容器的末端
baseline项目被定位到容器的基线
initial将此属性设置为其默认值
inherit从其父元素继承此属性

 3.4.2、代码实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .main{
            width: 300px;
            height: 300px;
            background-color: red;
            display: flex;
            align-items: center;
        }
        .main div{
            width: 70px;
            height: 70px;
            background-color: black;
        }
        #p1{
            background-color: orange;
        }
        #p2{
            background-color: deepskyblue;
        }
        #p3{
            background-color: greenyellow;
        }
    </style>
</head>
<body>
    <div class="main">
        <div id="p1"></div>
        <div id="p2"></div>
        <div id="p3"></div>
    </div>
</body>
</html>

3.5、justify-content

3.5.1、属性说明

justify-content属性为弹性容器内的项目指定默认对齐方式(纵)。

justify-content有以下属性值:

属性值效果说明
flex-start默认值、项目位于容器的开头
flex-end项目位于容器的结尾
center项目位于容器中央
space-between项目在行与行之间留有间隔
space-around项目在行之前、行之间和行之后留有空间
initial将此属性设置为其默认值
inherit从其父元素继承此属性

3.5.2、代码实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .main{
            width: 300px;
            height: 300px;
            background-color: red;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .main div{
            width: 70px;
            height: 70px;
            background-color: black;
        }
        #p1{
            background-color: orange;
        }
        #p2{
            background-color: deepskyblue;
        }
        #p3{
            background-color: greenyellow;
        }
    </style>
</head>
<body>
    <div class="main">
        <div id="p1"></div>
        <div id="p2"></div>
        <div id="p3"></div>
    </div>
</body>
</html>

3.6、align-content

3.6.1、属性说明

align-content属性修改flex-wrap属性的行为。它与align-items相似,但是它不对齐弹性项目,而是对齐弹性线。ps:必须有多行项目,否则此属性将失效。

align-content有以下属性值:

属性值效果说明
stretch默认值、行拉伸以占据剩余空间
center朝着弹性容器的中央对行打包
flex-start朝着弹性容器的开头对行打包
flex-end朝着弹性容器的结尾对行打包
space-between行均匀分布在弹性容器中
space-around行均匀分布在弹性容器中,两端各占一半

3.6.2、代码实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .main{
            width: 300px;
            height: 300px;
            background-color: red;
            display: flex;
            flex-wrap: wrap;
            align-content: space-between;
        }
        .main div{
            width: 70px;
            height: 70px;
            background-color: black;
        }
        #p1{
            background-color: orange;
        }
        #p2{
            background-color: deepskyblue;
        }
        #p3{
            background-color: greenyellow;
        }
    </style>
</head>
<body>
    <div class="main">
        <div id="p1"></div>
        <div id="p2"></div>
        <div id="p3"></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值