PHP如何设置垂直居中,html的元素水平垂直居中应该怎么设置

这次给大家带来html的元素水平垂直居中应该怎么设置,设置html的元素水平垂直居中的注意事项有哪些,下面就是实战案例,一起来看一下。

我们在设计页面的时候,经常要把DIV居中显示,而且是相对页面窗口水平和垂直方向居中显示,如让登录窗口居中显示。

到现在为止,探讨了很多种方法。

HTML:

第一种: CSS绝对定位

主要利用绝对定位,再用margin调整到中间位置。

父元素:.maxbox{

position: relative;

width: 500px;

height: 500px;

margin: 5px;

box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);

}

子元素:.minbox{

width: 200px;

height: 200px;

box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);

}

水平垂直居中对齐:.align-center{

position: absolute;

left: 50%;

top: 50%;

margin-left: -100px; /*width/-2*/

margin-top: -100px; /*height/-2*/

}

第二种: CSS绝对定位 + Javascript/JQuery

主要利用绝对定位,再用Javascript/JQuery调整到中间位置。相比第一种方法,此方法提高了class的灵活性。

父元素:.maxbox{

position: relative;

width: 500px;

height: 500px;

margin: 5px;

box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);

}

子元素:.minbox{

width: 200px;

height: 200px;

box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);

}

水平垂直居中对齐:.align-center{

position: absolute;

left: 50%;

top: 50%;

}

JQuery:$(function(){

$(".align-center").css(

{

"margin-left": ($(".align-center").width()/-2),

"margin-top": ($(".align-center").height()/-2)

}

);

});

几种方法的比较:

第一种CSS绝对定位margin调整,兼容性很好但是欠缺灵活性。如果有很多box里需要水平垂直居中,因其width,height不同而需要写不同的 .align-center 。

第二种使用脚本语言,兼容性很好且弥补了第一种的缺点。不因width,height的改变而影响水平垂直居中的效果。

第三种使用CSS3的一些新的属性,兼容IE10, Chrome, Firefox, 和 Opera。兼容性不太很好,不因width,height的改变而影响水平垂直居中的效果。

相信看了这些案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

相关阅读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值