CSS让子元素快速水平垂直居中的几种方法
2020-02-13 21:47
507 查看
方法有很多这里列出几种常用的
CSS代码如下 父元素类名以out打头,子元素类名以in打头 one,two…代表不同的方法
<style> *{ padding: 0; margin: 0; } [class*=out]{ width: 200px; height: 200px; background-color: pink; margin: 20px; } [class*=in]{ width: 100px; height: 100px; background-color: black; } .out-one{ display: table-cell; vertical-align: middle; text-align: center; } .in-one{ display: inline-block; vertical-align: middle; } .out-two{ position: relative; } .in-two{ position: absolute; margin: auto; left: 0; right: 0; top: 0; bottom: 0; } .out-three{ position: relative; } .in-three{ position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); } .out-four{ display: flex; align-items: center; justify-content: center; } .in-four{ } .out-five{ display: flex; } .in-five{ margin: auto; } .out-six{ position: relative; } .in-six{ position: absolute; left: 50%; top: 50%; margin-left: -50px; margin-top: -50px; } </style>
HTML代码
<body> <!-- display:table-cell --> <div class="out-one"> <div class="in-one"></div> </div> <!-- position + margin --> <div class="out-two"> <div class="in-two"></div> </div> <!-- position + transform --> <div class="out-three"> <div class="in-three"></div> </div> <!-- display:flex align-items: center;justify-content: center --> <div class="out-four"> <div class="in-four"></div> </div> <!-- display:flex margin:auto --> <div class="out-five"> <div class="in-five"></div> </div> <!-- position --> <div class="out-six"> <div class="in-six"></div> </div> </body>
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- css几种方法使元素水平垂直居中
- 纯CSS设置元素垂直水平居中的几种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- 图片水平、垂直居中与父元素的css方法
- CSS水平垂直居中的几种方法
- CSS-几种未知长宽的垂直水平居中方法
- CSS内联元素、块级元素的水平居中和垂直居中方法总结
- CSS水平垂直居中的几种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中元素水平垂直居中4种方法介绍
- css 块级元素、行内元素的水平、垂直居中方法总结
- CSS水平垂直居中的几种方法
- 用CSS将元素设置成水平居中的几种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css:盒子垂直水平居中的几种方法
- CSS水平垂直居中的几种方法
- 【css】实现垂直水平居中的几种方法
- css控制元素垂直居中的几种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- 元素垂直水平居中的几种方法