美文网首页
css实现元素水平垂直居中

css实现元素水平垂直居中

作者: 不会潜水的猫小喵 | 来源:发表于2019-04-26 01:16 被阅读0次

以下面这段html代码为例,container为父元素,center为子元素。其中根据父元素和子元素分别定宽高或者不定宽高的组合,分为四种情况来说明。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
</head>
<body>
    <div class="container">
        <div class="center"></div>
    </div>
</body>
</html>

一、父元素和子元素定宽高

1.绝对定位 + 偏移量50% + 负margin值
.container {  
    position: relative;
    width: 200px;
    height: 200px;
    background-color: aquamarine;
}
.center {   
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -50px;
    width: 100px;
    height: 100px;
    background-color: pink;
}
2.绝对定位 + 偏移量50% + transform
.container {  
    position: relative;
    width: 200px;
    height: 200px;
    background-color: aquamarine;
}
.center {   
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100px;
    height: 100px;
    background-color: pink;
}
3.绝对定位 + 偏移量0 + margin:auto
.container {  
    position: relative;
    width: 200px;
    height: 200px;
    background-color: aquamarine;
}
.center {   
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    width: 100px;
    height: 100px;
    background-color: pink;
}
4.相对定位 + 偏移量50% + transform
.container {  
    width: 200px;
    height: 200px;
    background-color: aquamarine;
}
.center {   
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100px;
    height: 100px;
    background-color: pink;
}
5.相对定位+具体值
.container {  
    width: 200px;
    height: 200px;
    background-color: aquamarine;
}
.center {   
    position: relative;
    top: 50px;
    left: 50px;
    width: 100px;
    height: 100px;
    background-color: pink;
}
6.flex布局
.container {  
    display: flex;
    justify-content: center;
    align-items: center;
    width: 200px;
    height: 200px;
    background-color: aquamarine;
}
.center {   
    width: 100px;
    height: 100px;
    background-color: pink;
}
7.line-height + text-align + vertical-align
.container {  
    width: 200px;
    height: 200px;
    line-height: 200px;
    text-align: center;
    background-color: aquamarine;
}
.center {   
    display: inline-block;
    vertical-align: middle;
    width: 100px;
    height: 100px;
    background-color: pink;
}
8.使用table-cell
.container {  
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    width: 200px;
    height: 200px;
    background-color: aquamarine;
}
.center {   
    display: inline-block;
    width: 100px;
    height: 100px;
    background-color: pink;
}
9.使用-webkit-box
.container {  
    display:-webkit-box;
    -webkit-box-pack:center;
    -webkit-box-align:center;
    width: 200px;
    height: 200px;
    background-color: aquamarine;
}
.center {   
    width: 100px;
    height: 100px;
    background-color: pink;
}

二、父元素定宽高,子元素不定宽高

用情况一中的2,3,4,6,7,8,9方法可以实现。

三、父元素不定宽高,子元素定宽高

用情况一中的1,2,3,4,6,7,8,9方法可以实现。
其中1,2,3方法需要把父容器的高度撑起来才能看到效果,可以用beforeafter或多添加一个div给其设置高度,撑开父元素的高度,其他都大同小异。

四、父元素和子元素都不定宽高

这种情况下垂直居中其实没有多大意义,一般给父元素设置padding值或者给子元素设置margin值就能实现。

相关文章

  • css 水平垂直居中实现方式

    css 水平垂直居中实现方式 水平垂直居中包括行内元素居中,以及块级元素居中 行内元素html结构 块级元素结构 ...

  • 垂直居中,水平居中

    CSS设置行内元素的水平居中 CSS设置行内元素的垂直居中 CSS设置块级元素的水平居中 CSS设置块级元素的垂直居中

  • margin负值应用实例

    1. 水平垂直居中 利用margin负值可以实现元素的水平垂直居中 html代码: CSS代码 实现效果 2. 列...

  • 实现水平/垂直居中

    css实现水平/垂直方向居中 在开始介绍如何实现水平/垂直方向居中之前,需要先介绍一下行内元素和块级元素 行内元素...

  • 2020-03-05 CSS水平垂直居中学

    1.块级元素水平居中,水平元素垂直居中 CodePen:CSS块级水平居中 2.块级元素垂直居中 CodePen:...

  • CSS - 垂直水平居中方法

    前言 总括:整理 css 垂直水平居中方法,区分内联元素与块级元素 CSS垂直居中和水平居中 用css让一个容器水...

  • CSS解决盒模型居中的问题

    CSS实现盒子模型水平居中、垂直居中、水平垂直居中的多种方法 CSS实现盒子模型水平居中的方法 全局样式 第一种:...

  • CSS图片居中(水平居中和垂直居中)

    css图片水平居中 css图片垂直居中 css图片水平垂直居中

  • Flexbox实现元素的水平居中和垂直居中

    网上有N种方法实现元素的水平居中和垂直居中,如CSS制作水平垂直居中对齐,这些方法各有各的优缺点。在这里,我们使用...

  • css 图片居中

    css图片居中(水平居中和垂直居中) css图片水平居中 块状元素直接用text-align:center, di...

网友评论

      本文标题:css实现元素水平垂直居中

      本文链接:https://www.haomeiwen.com/subject/qdfzhftx.html