php元素浮动会产生哪些影响,元素浮动的影响与三列布局的实现原理——2019年9月4日22时30分...

一、元素浮动造成父元素高度折叠同一个块元素中,子级元素浮动,会造成附骥元素的高度折叠,包裹不住子级元素。

网页实际效果展示

fe9f9e0eddbc19368c60c6f750a59fac.png

消除子元素浮动的影响实例

html>

清除浮动的影响

.box1 {

width: 200px;

border: 5px dashed red;

}

.box2 {

width: inherit;

height: 200px;

background: lightcyan;

}

.box3 {

margin-top: 20px;

width: 200px;

border: 5px dashed red;

}

.box4 {

width: inherit;

height: 200px;

background: lightcyan;

float: left;

}

.clear {

clear: both;

}

.box5 {

width: 200px;

border: 5px dashed red;

overflow: hidden;

}

.box6 {

width: inherit;

height: 200px;

background: lightcyan;

float: left;

}

我是一个子块在没有浮动时候的样式。

我是一个子块设置了向左浮动的样式,造成父级元素高度折叠。

解决方法

我是一个子块设置了向左浮动的样式,造成父级元素高度折叠。在父级元素中利用overflow: hidden 属性,可以消除浮动的影响。

运行实例 »

点击 "运行实例" 按钮查看在线实例

二、相对定位相对定位是用 position: relative 属性设置,只是相对它原来。

网页实际效果展示

be362406be7566b7908928a0d5cf494d.png

相对定位实例

html>

相对定位

.box1 {

width: 80px;

height: 80px;

background: lightcyan;

}

.box2 {

width: 80px;

height: 80px;

background: lightgoldenrodyellow;

}

.box3 {

width: 80px;

height: 80px;

background: lightgray;

}

.box4 {

width: 80px;

height: 80px;

background: lightgreen;

}

.box5 {

width: 80px;

height: 80px;

background: lightpink;

}

.box1 {

position: relative;

left: 80px;

}

.box3 {

position: relative;

left: 80px;

top: -80px;

}

.box4 {

position: relative;

left: 160px;

top: -160px;

}

.box5 {

position: relative;

left: 80px;

top: -160px;

}

1
2
3
4
5

上面的方块是通过相对定位,position: relative 属性进行放置的。

运行实例 »

点击 "运行实例" 按钮查看在线实例

三、绝对定位绝对定位脱离了文档流,所有必须要有参照物。如body,如定位父级就参照定位父级。

绝对定位网页实际效果展示

8e76db52a8425d3275e3df86efff682a.png

绝对定位实例

html>

绝对定位

.parent {

position: relative;

border: 1px dashed red;

width: 240px;

height: 240px;

}

.box1 {

width: 80px;

height: 80px;

background: lightcyan;

}

.box2 {

width: 80px;

height: 80px;

background: lightgoldenrodyellow;

}

.box3 {

width: 80px;

height: 80px;

background: lightgray;

}

.box4 {

width: 80px;

height: 80px;

background: lightgreen;

}

.box5 {

width: 80px;

height: 80px;

background: lightpink;

}

.box1 {

position: absolute;

left: 80px;

}

.box2 {

position: absolute;

top: 80px;

}

.box3 {

position: absolute;

left: 80px;

top: 80px;

}

.box4 {

position: absolute;

left: 160px;

top: 80px;

}

.box5 {

position: absolute;

left: 80px;

top: 160px;

}

1
2
3
4
5

上面的方块是通过绝对定位,position: absolute 属性进行放置的。

运行实例 »

点击 "运行实例" 按钮查看在线实例

四、固定定位固定定位是将某元素固定在页面中的特定位置,随固定条的拖动,依然在固定位置显示。用position: fixed 属性设置。

固定定位网页实际效果展示

0eee2457f35c0f9156530b7fac9f14a5.png

固定定位实例

html>

固定定位

body {

height: 2000px;

}

.ads {

width: 300px;

height: 300px;

background: lightpink;

position: fixed;

top: 10px;

left: 0;

}

我是一个被固定的元素,不管鼠标怎么拖动,我都在用一个位置。

运行实例 »

点击 "运行实例" 按钮查看在线实例

四、用绝对定位实现三列布局利用绝对定位position:absolute 的属性设置网页成三列布局。

绝对定位三列布局网页实际效果展示

70db42d282c49fc70f796d8520c16a5f.png

绝对定位实现三列布局实例

html>

绝对定位实现三列布局

.container {

width: 1000px;

margin: 0 auto;

}

.header,

.footer {

height: 60px;

background: lightskyblue;

}

.main {

background: lightgrey;

margin: 5px auto;

}

.left {

width: 200px;

height: 500px;

background: lightyellow;

}

.content {

min-height: 500px;

background: lightpink;

}

.right {

width: 200px;

height: 500px;

background: lightyellow;

}

.main {

position: relative;

}

.left {

position: absolute;

top: 0;

left: 0;

}

.right {

position: absolute;

top: 0;

right: 0;

}

.content {

margin: 0 200px;

}

网页头部
左侧内容

我是用绝对定位position:absolute 属性设置的三列布局

中间内容

我是用绝对定位position:absolute 属性设置的三列布局

右侧内容

我是用绝对定位position:absolute 属性设置的三列布局

网页底部

运行实例 »

点击 "运行实例" 按钮查看在线实例

五、浮动实现三列布局利用浮动属性 float 来实现网页成三列布局。

浮动实现三列布局实际页面展示

b525768d3f94e04269d420256a273918.png

浮动实现三列布局实例

html>

浮动实现三列布局

.container {

width: 1000px;

margin: 0 auto;

}

.header,

.footer {

height: 60px;

background: lightskyblue;

}

.main {

background: lightgrey;

margin: 5px auto;

overflow: hidden;

}

.left {

width: 200px;

height: 500px;

background: lightyellow;

}

.content {

min-height: 500px;

background: lightpink;

}

.right {

width: 200px;

height: 500px;

background: lightyellow;

}

.left {

float: left;

}

.right {

float: right;

}

.content {

float: left;

width: 600px;

}

网页头部
左侧内容

我是通过float: left 来实现的布局

中间内容

我是通过float 的属性来实现的

右侧内容

我是通过float: right 来实现的布局

网页底部

运行实例 »

点击 "运行实例" 按钮查看在线实例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值