定制app开发CSS浮动(float)

一 定制app开发传统网页布局的三种方式

定制app开发网页布局的本质:用 CSS 定制app开发来摆放盒子,定制app开发把盒子摆放到相应位置。

CSS 定制app开发提供了三种传统布局方式(定制app开发简单说就是盒子如何进行排列)。

  • 普通流(标准流)
  • 浮动
  • 定位

定制app开发这里指的只是传统布局,定制app开发其实还有一些特殊高级定制app开发的布局方式。

二 标准流(普通流/文档流)

定制app开发所谓的标准流:定制app开发就是标签按照规定好的定制app开发默认方式排列。

  1. 定制app开发块级元素会独占一行,定制app开发从上向下顺序排列。
  2. 定制app开发行内元素会按照顺序,定制app开发从左到右顺序排列,定制app开发碰到父元素边缘则自动换行。

定制app开发以上都是标准流布局,定制app开发我们前面学习的就是标准流,定制app开发标准流是最基本的布局方式。

这三种布局方式都是用来摆放盒子的,盒子摆放到合适位置,布局自然就完成了。

注意: 实际开发中,一个页面基本都包含了这三种布局方式(后面移动端学习新的布局方式) 。

三 为什么需要浮动?

提问:我们用标准流能很方便的实现如下效果吗?

  1. 如何让多个块级盒子(div)水平排列成一行?

比较难,虽然转换为可以实现一行显示,但是他们之间会有大的空白缝隙,很难控制。

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>行内块中间有缝隙</title>    <style>        div {            width: 150px;            height: 200px;            background-color: #d87093;            display: inline-block;        }    </style></head><body>    <div>1</div>    <div>2</div>    <div>3</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

  1. 如何实现两个盒子的左右对齐?


总结: 有很多的布局效果,标准流没有办法完成,此时就可以利用浮动完成布局。 因为浮动可以改变元素标签默认的排列方式。

浮动最典型的应用:可以让多个块级元素一行内排列显示。

网页布局第一准则:多个块级元素纵向排列找标准流,多个块级元素横向排列找浮动!

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>多个块级元素横向排列找浮动</title>    <style>        div {            float: left;            width: 150px;            height: 200px;            background-color: #d87093;        }    </style></head><body>    <div>1</div>    <div>2</div>    <div>3</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25


**拓展:**浮动的盒子不会发生合并!

四 什么是浮动?

float 属性用于创建浮动框,将其移动到一边,直到左边缘或右边缘触及包含块或另一个浮动框的边缘。

语法:

选择器 { float: 属性值;}
  • 1
属性描述
none元素不浮动(默认值)
left元素向左浮动
right元素向右浮动
<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>什么是浮动</title>    <style>        .left,        .right {            float: left;            width: 200px;            height: 200px;            background-color: pink;        }    </style></head><body>    <div class="left">左青龙</div>    <div class="right">右白虎</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>什么是浮动</title>    <style>        .left,        .right {            float: left;            width: 200px;            height: 200px;            background-color: pink;        }        /* 层叠性 */        .right {            float: right;        }    </style></head><body>    <div class="left">左青龙</div>    <div class="right">右白虎</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <style>        div {            /* 浏览器解析行内块或行内标签的时候, 如果标签换行书写会产生一个空格的距离 */            display: inline-block;            width: 100px;            height: 100px;        }        .one {            background-color: pink;        }        .two {            background-color: skyblue;        }    </style></head><body>    <div class="one">one</div><div class="two">two</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <style>        /* img {            float: left;        } */        div {            width: 100px;            height: 100px;        }        .one {            background-color: pink;            float: left;        }        .two {            background-color: skyblue;            /* flr */            /* float: right; */            float: left;        }    </style></head><body>    <!-- 1. 图文环绕 -->    <!-- <img src="./images/1.jpg" alt="">    阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了阿里的萨里肯定是福建安老师看见了 -->    <!-- 2. 网页布局: 块在一行排列 -->    <div class="one">one</div>    <div class="two">two</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <style>        /* 浮动的标签  顶对齐 */        /* 浮动: 在一行排列, 宽高生效 -- 浮动后的标签具备行内块特点 */        .one {            width: 100px;            height: 100px;            background-color: pink;            float: left;            margin-top: 50px;        }        .two {            width: 200px;            height: 200px;            background-color: skyblue;            float: left;            /* 因为有浮动, 不能生效 - 盒子无法水平居中 */            margin: 0 auto;        }        .three {            width: 300px;            height: 300px;            background-color: orange;        }    </style></head><body>    <div class="one">one</div>    <div class="two">two</div>    <div class="three">three</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

五 浮动特性(重难点)

加了浮动之后的元素,会具有很多特性,需要我们掌握。

  1. 浮动元素会脱离标准流(脱标)
  2. 浮动的元素会一行内显示并且元素顶部对齐
  3. 浮动的元素会具有行内块元素的特性

下面分别解释:

(1)浮动元素会脱离标准流(脱标)

  • 脱离标准普通流的控制(浮) 移动到指定位置(动),(俗称脱标)
  • 浮动的盒子不再保留原先的位置

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>浮动特性1</title>    <style>        /* 设置了浮动(float)的元素会:        1.脱离标准普通流的控制(浮)移动到指定位置(动)。        2.浮动的盒子不再保留原先的位置 */        .box1 {            float: left;            width: 200px;            height: 200px;            background-color: pink;        }        .box2 {            width: 300px;            height: 300px;            background-color: gray;        }    </style></head><body>    <div class="box1">浮动的盒子</div>    <div class="box2">标准流的盒子</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

(2)浮动的元素会一行内显示并且元素顶部对齐

  • 如果多个盒子都设置了浮动,则它们会按照属性值一行内显示并且顶端对齐排列。
  • 浮动的元素是互相贴靠在一起的(不会有缝隙),如果父级宽度装不下这些浮动的盒子,多出的盒子会另起一行对齐。
<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>浮动元素特性-浮动元素一行显示</title>    <style>        div {            float: left;            width: 200px;            height: 200px;            background-color: pink;        }        .two {            background-color: skyblue;            height: 249px;        }        .four {            background-color: skyblue;        }    </style></head><body>    <div>1</div>    <div class="two">2</div>    <div>3</div>    <div class="four">4</div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35


(3)浮动的元素会具有行内块元素的特性

任何元素都可以浮动。不管原先是什么模式的元素,添加浮动之后具有行内块元素相似的特性。

  • 块级盒子:没有设置宽度时默认宽度和父级一样宽,但是添加浮动后,它的大小根据内容来决定
  • 行内盒子:宽度默认和内容一样宽,直接设置高宽无效,但是添加浮动后,它的大小可以直接设置
  • 浮动的盒子中间是没有缝隙的,是紧挨着一起的
  • 即:默认宽度由内容决定,同时支持指定高宽,盒子之间无空隙
<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>浮动的元素具有行内块元素特点</title>    <style>        /* 任何元素都可以浮动。不管原先是什么模式的元素,添加浮动之后具有行内块元素相似的特性。 */        span,        div {            float: left;            width: 200px;            height: 100px;            background-color: pink;        }        /* 如果行内元素有了浮动,则不需要转换块级\行内块元素就可以直接给高度和宽度 */        p {            float: right;            height: 200px;            background-color: skyblue;        }    </style></head><body>    <span>span1</span>    <span>span2</span>    <div>div</div>    <p>pppppppppppppp</p></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

注意:之所以顶部没有对齐,原因是 p 标签自带的外边距 > span div 自带的外边距。

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>浮动的元素具有行内块元素特点</title>    <style>        * {            margin: 0px;        }        /* 任何元素都可以浮动。不管原先是什么模式的元素,添加浮动之后具有行内块元素相似的特性。 */        span,        div {            float: left;            width: 200px;            height: 100px;            background-color: pink;        }        /* 如果行内元素有了浮动,则不需要转换块级\行内块元素就可以直接给高度和宽度 */        p {            float: right;            height: 200px;            background-color: skyblue;        }    </style></head><body>    <span>span1</span>    <span>span2</span>    <div>div</div>    <p>pppppppppppppp</p></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

六 浮动元素经常和标准流父级搭配使用

为了约束浮动元素位置,我们网页布局一般采取的策略是:

先用标准流的父元素排列上下位置,之后内部子元素采取浮动排列左右位置。符合网页布局第一准侧。


应用举例:

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>浮动元素搭配标准流父盒子1</title>    <style>        .box {            width: 1200px;            height: 460px;            background-color: black;            margin: 0 auto;        }        .left {            float: left;            width: 230px;            height: 460px;            background-color: pink;        }        .right {            float: left;            width: 970px;            height: 460px;            background-color: skyblue;        }    </style></head><body>    <div class="box">        <div class="left">左侧</div>        <div class="right">右侧</div>    </div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>浮动元素搭配标准流父盒子2</title>    <style>        * {            margin: 0;            padding: 0;        }        /* 取消 li 前的圆点 */        li {            list-style: none;        }        .box {            width: 1226px;            height: 285px;            background-color: pink;            /* 让大盒子水平居中 */            margin: 0 auto;        }        .box li {            width: 296px;            height: 285px;            background-color: gray;            float: left;            /* 每个小盒子用右边距隔开 */            margin-right: 14px;        }        /* 取消最后一个小盒子的右外边距 */        /* 这里必须写 .box .last 要注意权重的问题  20 */        .box .last {            margin-right: 0;        }    </style></head><body>    <ul class="box">        <li>1</li>        <li>2</li>        <li>3</li>        <li class="last">4</li>    </ul></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>浮动布局练习3</title>    <style>        .box {            width: 1226px;            height: 615px;            background-color: pink;            margin: 0 auto;        }        .left {            float: left;            width: 234px;            height: 615px;            background-color: gray;        }        .right {            float: left;            width: 992px;            height: 615px;            background-color: skyblue;        }        .right>div {            float: left;            width: 234px;            height: 300px;            background-color: pink;            margin-left: 14px;            margin-bottom: 14px;        }    </style></head><body>    <div class="box">        <div class="left">左青龙</div>        <div class="right">            <div>1</div>            <div>2</div>            <div>3</div>            <div>4</div>            <div>5</div>            <div>6</div>            <div>7</div>            <div>8</div>        </div>    </div></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发