美文网首页
css使元素水平垂直居中总结

css使元素水平垂直居中总结

作者: zhangjianli | 来源:发表于2017-02-14 22:11 被阅读0次

在下面的演示代码中,用到的html结构为:

<div class="content">
    <div class="center"></div>
</div>

方法一:positoin+margin

        .content{
            height: 200px;
            width: 200px;
            background-color: #ccc;
            position: relative;
        }
        .center{
            width: 100px;
            height: 100px;
            background-color: pink;
            margin: auto;
            position:absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0; 
        }

方法二:display:tell-cell;

.content{
            height: 200px;
            width: 200px;
            background-color: #ccc;
            display: table-cell;
            vertical-align: middle;
            text-align: center;
        }
        .center{
            display: inline-block;
            width: 100px;
            height: 100px;
            background-color: pink;
            vertical-align: middle;
            
        }

方法三:display:flex;align-items:center;justify-content:center;

        .content{
            height: 200px;
            width: 200px;
            background-color: #ccc;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .center{
             background-color: pink;
             width: 100px;
             height: 100px;
        }

方法四:display:flex;margin:auto;(主要适用移动端)

          .content{
            height: 200px;
            width: 200px;
            background-color: #ccc;
            display: flex;
        }
        .center{
            background-color: pink;
               width: 100px;
            height: 100px;
            margin: auto;
        }

方法五:纯position

          .content{
            height: 200px;
            width: 200px;
            background-color: #ccc;
            position: relative;
        }
        .center{
            background-color: pink;
             width: 100px;
            height: 100px;
            position: absolute;
            left: 50px;
            top: 50px;
        }

content{
height: 200px;
width: 200px;
background-color: #ccc;
position: relative;
}
.center{
background-color: pink;
width: 100px;
height: 100px;
position: absolute;
left: 50%;
margin-left: -50px;//-1/2宽度
top: 50%;
margin-top: -50px;//-1/2高度
}

相关文章

  • 垂直居中,水平居中

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

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

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

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

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

  • css使元素水平垂直居中总结

    在下面的演示代码中,用到的html结构为: 方法一:positoin+margin 方法二:display:tel...

  • CSS居中小结

    CSS居中总结 最近在学习CSS居中,居中里面又包含水平居中和垂直居中,分不太清内联元素(inline or in...

  • CSS - 垂直水平居中方法

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

  • CSS居中的方法总结

    CSS水平和垂直居中在开发中经常用到,在此加以总结。 水平居中方法 1.行内元素水平居中,设置父元素的text-a...

  • CSS水平垂直居中总结

    CSS水平居中、垂直居中、水平垂直居中方法总结 文字的水平居中: 单行文字的垂直居中: 让有宽度的div水平居中:...

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

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

  • css 图片居中

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

网友评论

      本文标题:css使元素水平垂直居中总结

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