Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
css浮动float属性值_css设置div上下浮动,希望能够帮助你!!!。
网页布局的本质:用 CSS 来摆放盒子,把盒子摆放到相应位置。
CSS 提供了三种传统布局方式(简单说就是盒子如何进行排列)。
这里指的只是传统布局,其实还有一些特殊高级的布局方式。
所谓的标准流:就是标签按照规定好的默认方式排列。
以上都是标准流布局,我们前面学习的就是标准流,标准流是最基本的布局方式。
这三种布局方式都是用来摆放盒子的,盒子摆放到合适位置,布局自然就完成了。
注意: 实际开发中,一个页面基本都包含了这三种布局方式(后面移动端学习新的布局方式) 。
提问:我们用标准流能很方便的实现如下效果吗?
比较难,虽然转换为行内块元素可以实现一行显示,但是他们之间会有大的空白缝隙,很难控制。
<!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>
总结: 有很多的布局效果,标准流没有办法完成,此时就可以利用浮动完成布局。 因为浮动可以改变元素标签默认的排列方式。
浮动最典型的应用:可以让多个块级元素一行内排列显示。
网页布局第一准则:多个块级元素纵向排列找标准流,多个块级元素横向排列找浮动!
<!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>
**拓展:**浮动的盒子不会发生外边距合并!
float
属性用于创建浮动框,将其移动到一边,直到左边缘或右边缘触及包含块或另一个浮动框的边缘。
语法:
选择器 {
float: 属性值;}
属性 | 描述 |
---|---|
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>
<!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>
<!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>
<!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>
<!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>
加了浮动之后的元素,会具有很多特性,需要我们掌握。
下面分别解释:
<!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>
<!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>
任何元素都可以浮动。不管原先是什么模式的元素,添加浮动之后具有行内块元素相似的特性。
<!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>
注意:之所以顶部没有对齐,原因是 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>
为了约束浮动元素位置,我们网页布局一般采取的策略是:
先用标准流的父元素排列上下位置,之后内部子元素采取浮动排列左右位置。符合网页布局第一准侧。
应用举例:
<!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>
<!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>
<!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>
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章