Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

html中div水平居中-垂直居中的多种方式

秋天招骋时间到了,很多人都需要换工作了。前端面试,总是会有些人跟我一个基本上难题,div居中法。在这儿,我将汇总一下常见的方式。

肯定本土化

父级原素(parent)选用相对定位(relative),必须垂直居中的原素(demo)选用绝对定位(absolute)。垂直居中原素往上偏位50%,往左边偏位50%,这时左端点坐落于核心,但是大家必须的是总体垂直居中,因此在偏位本身一半的高宽。(如下图)

html中div水平居中-垂直居中的多种方式插图

沒有挪动它本身总宽和极度的一半。

.parent { position: relative; width: 500px; height: 500px; border: solid red 1px; } .demo { position: absolute; width: 100px; height: 100px; border: solid blue 1px; top: 50%; left: 50%; margin-top: -50px; margin-left: -50px; }

延展性定心决。

根据灵便的合理布局设定竖直和水平居中。

.parent { width: 500px; height: 500px; border: solid red 1px; display: flex; // 竖直,水平居中 align-items: center; justify-content: center; } .demo { width: 100px; height: 100px; border: solid blue 1px; }

应用转换垂直居中。

当子原素不清楚一个人的间距和特定时,应用转换开展比例转移。

.parent { position: relative; width: 500px; height: 500px; border: solid red 1px; } .demo { position: absolute; border: solid blue 1px; top: 50%; left: 50%; transform: translate(-50%, -50%); } 垂直居中

之上三种方式全是常见的,自然也有别的垂直居中的方式,例如网格图合理布局,报表表格中合理布局这些。

温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
未整理

雨婷《服装直播带货运营落地实操课》新号0粉快速带货日销10W+

2021-7-31 0:00:00

未整理

XnSay机器人授权系统源码

2021-8-2 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索