html fixed 居中,CSS中position属性之fixed实现div居中

position 属性规定元素的定位类型。这个属性定义建立元素布局所用的定位机制。任何元素都可以定位,不过绝对或固定元素会生成一个块级框,而不论该元素本身是什么类型。相对定位元素会相对于它在正常流中的默认位置偏移。

上下左右 居中

div{

position:fixed;

margin:auto;

left:0;

right:0;

top:0;

bottom:0;

width:200px;

height:150px;

}

如果只需要左右居中,那么把 bottom:0; 或者 top:0; 删掉即可

如果只需要上下居中,那么把 left:0; 或者 right:0; 即可

下面附一个DIV 元素在浏览器窗口居中

其实,实现这个效果并不复杂,利用 CSS 中的 position 定位就可以轻松搞定了。来看看代码吧:

.dialog{

position: fixed;

_position:absolute;

z-index:1;

top: 50%;

left: 50%;

margin: -141px 0 0 -201px;

width: 400px;

height:280px;

border:1px solid #CCC;

line-height: 280px;

text-align:center;

font-size: 14px;

background-color:#F4F4F4;

overflow:hidden;

}

我是在窗口正中央的,呵呵!

设置的技巧全部在这里:

.dialog{

position: fixed;

_position:absolute; /* hack for IE6 */

z-index:1;

top: 50%;

left: 50%;

margin: -141px 0 0 -201px;

width: 400px;

height:280px;

border:1px solid #CCC;

line-height: 280px;

text-align:center;

font-size: 14px;

background-color:#F4F4F4;

overflow:hidden;

}

设置 position: fixed; _position:absolute;

设置 left:50% 和 top:50%;

设置 margin: -(DIV的offsetWidth/2) 0 0 -(DIV的offsetHeight/2)

效果图

56315eab36e68974b79fcf81205323f3.png

以上内容是小编给大家分享的CSS中position属性之fixed实现div居中的全部叙述,希望大家喜欢。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值