帝王谷资源网 Design By www.wdxyy.com
flex实现水平垂直居中
适用场景:父子宽高都可未知(比较推荐这种方式,简单,而且目前兼容性也不错。)
<html> <head> <style> .parent { width: 100%; height: 100px; background: cyan; display: flex; justify-content: center; align-items: center; } .son { width: 20%; height: 20%; background: pink; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
绝对定位加上负margin
适用场景:父元素宽高已知未知都行,但是首先得有宽高。其次子元素的宽高必须已知,因为需要设置子元素的负margin. (也可以将负margin设置成translate来做位移实现)
<html> <head> <style> .parent { position: relative; width: 200px; height: 200px; background: pink; } .son { position: absolute; left: 50%; top: 50%; margin-left: -25px; margin-top: -25px; width: 50px; height: 50px; background: yellow; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
绝对定位 + auto margin
适用场景:父子元素宽高都未知的情况(该方式不需要明确知道子元素宽高,子元素宽高可用百分比,对于子元素宽高不确定需要居中的情况比较适合。)
<html> <head> <style> .parent { position: relative; width: 200px; height: 200px; background: cyan; } .son { position: absolute; left: 0; top: 0; bottom: 0; right: 0; margin: auto; width: 10%; height: 10%; background: yellow; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
网格布局
适用场景:父子元素宽高未知,兼容性不大好
<html> <head> <style> .parent { display: grid; } .son { jusitify-self: center; align-self: center; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
Table-cell + text-align + vertical-align
适用场景: 父元素大小已知(非百分比高度),子元素大小未知,但子元素须为行内块元素,较好的兼容性
<html> <head> <style> .parent { display: table-cell; vertical-align: middle; text-align: center; width: 100vw; height: 90vh; background-color: yellowgreen; } .son { display: inline-block; width: 200px; height: 200px; background-color: Indigo; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
伪元素
适用场景:父子宽高都可未知,子元素需为行内块元素(这种方式其实就是使用伪元素的高度为100%,子元素和伪元素都设置 vertical-align: middle实现垂直居中的效果)
<html> <head> <style> .parent { height: 100vh; width: 100vw; text-align: center; background: #c0c0c0; } .parent:before { content: "\200B"; display: inline-block; height: 100%; vertical-align: middle; } .son { display: inline-block; vertical-align: middle; width: 200px; height: 200px; padding: 10px 15px; background: #f5f5f5; } </style> </head> <body> <div class="parent"> <div class="son"></div> </div> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
css,元素水平垂直居中
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
更新日志
2024年10月07日
2024年10月07日
- 班得瑞原装进口《第八张新世纪专辑:琉璃湖畔》1CD[APE/CUE分轨][286.4MB]
- 魏如萱.2011-在哪里(EP)【添翼创越】【WAV+CUE】
- 群星.1981-新金曲奖·青年创作园地12合辑(喜玛拉雅复刻版)【歌林】【WAV+CUE】
- 麦洁文.1989-新曲与精丫新艺宝】【WAV+CUE】
- SNH48《薄荷糖 (Mint Candy)》[320K/MP3][33.72MB]
- SNH48《薄荷糖 (Mint Candy)》[FLAC/分轨][78.59MB]
- 刘美麟《同生》[320K/MP3][70.99MB]
- 群星.1994-宝丽金最流行3CD【宝丽金】【WAV+CUE】
- 群星.2024-聆听“某某”影视原声带【华纳】【FLAC分轨】
- 陈慧娴.1989-永远是你的朋友(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 何洛洛.2024-别叫醒我(EP)【光羽】【FLAC分轨】
- 林忆莲.1996-爱莲说2CD【华纳】【WAV+CUE】
- 黄妃.2005-红【亚律】【WAV+CUE】
- 刘美麟《同生》[FLAC/分轨][161.95MB]
- 群星《前途海量 电影原声专辑》[320K/MP3][106.26MB]