css定位

一、常见的布局方式

1.标准流:

块级元素独占一行,垂直布局

2.浮动:

让块级元素水平布局

3.定位:

可以在网页页面中任意摆放

定位后层叠在其他盒子上面

二、定位

属性名:position

属性值:

static

静态定位

relative

相对定位

absolute

绝对定位

fixed

固定定位

偏移值:

水平方向left数字+px距离左边的距离
水平方向right数字+px距离右边的距离
垂直方向top数字+px距离上边的距离
垂直方向bottom数字+px距离下边的距离


2.1静态定位

属性:position: static;

静态定位的元素是默认值,不脱标,跟标准流一样,不能使用方位值进行移动

.box {
    position: static;
    left: 500px;
    top: 500px;
    width: 200px;
    height: 200px;
    background-color: #ccc;
}

2.2相对定位

属性:position: relative;

相对定位的元素不脱标,还占据原来的位置,位置偏移参考自身的位置进行移动

        .red {
            position: relative;
            right: 50px;
            bottom: 50px;
            background-color: red;
        }

2.3绝对定位

属性:position: absolute;

绝对定位的元素脱标,不占据原来的位置,绝对定位的父元素都没有使用定位,自身位置移动参考浏览器,父元素有定位,自身位置移动参考离最近的使用了定位父元素

        .fu {
            position: relative;
            width: 800px;
            height: 800px;
            background-color: #ccc;
            margin: 0 auto;
        }
        .zi {
            position: absolute;
            right: 0;
            bottom: 0;
            background-color: red;
        }

2.4固定定位

属性:position: absolute;

固定定位将元素固定在页面的某个位置,不会随着滚动条滚动

  • 固定定位元素脱标,不占据原来的位置
  • 自身位置移动参考浏览器可视窗口
        .box {
            position: fixed;
            right: 0;
            bottom: 0;
            width: 100px;
            height: 30px;
            background-color: rgba(255,0,0,.3);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄昏终结者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值