CSS盒模型的几种居中方式

作者 : 开心源码 本文共1235个字,预计阅读时间需要4分钟 发布时间: 2022-05-13 共255人阅读

css实现盒子模型居中方式

<div class="parent">    <div class="child"></div></div>

1.绝对定位 margin负值

.parent{    width:600px;    height:600px;    margin:auto;    position: relative;    background-color: #000; }.child{    width:200px;    height:200px;    background-color: red;    position: absolute;    top:50%;    left:50%;    margin:-100px 0 0 -100px;}

2.绝对定位 四个方位为0

.parent{    width:600px;    height:600px;    margin:auto;    position: relative;    background-color: #000;}.child{    width:200px;    height:200px;    background-color: red;    position: absolute;    top:0;    left:0;    right:0;    bottom:0;    margin:auto;}

3.绝对定位 translate

.parent{    width:600px;    height:600px;    margin:auto;    position: relative;    background-color: #000;}.child{    width:200px;    height:200px;    background-color: red;    position: absolute;    top:50%;    left:50%;    transform:translate(-50%,-50%)}

4.margin属性居中

.parent{    width:600px;    height:600px;    margin:auto;        overflow: hidden;    background-color: #000;}.child{    width:200px;    height:200px;    background-color: red;    margin: 150px auto 0;}

5.table-cell

.parent{    width:600px;    height:600px;        background-color: #000;        display: table-cell;        vertical-align: middle;}.child{    width:200px;    height:200px;    background-color: red;    margin: 0 auto;}

6.flex 居中

.parent{    width:600px;    height:600px;        display: flex;        justify-content: center;        align-items: center;    background-color: #000;    margin:0 auto;}.child{    width:200px;    height:200px;    background-color: red;}
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » CSS盒模型的几种居中方式

发表回复