美文网首页
css关于居中的方式

css关于居中的方式

作者: 读心的心 | 来源:发表于2019-11-19 16:58 被阅读0次

父元素没有固定宽高

水平垂直居中

html:

    <div class="wrapper">
        <div class="info">
            垂直居中
        </div>
    </div>

css:

//绝对定位水平垂直居中,方法1
.info {
    position: absolute;
     width: 500px;
     height: 300px;
     margin: auto;
     top: 0;
     left: 0;
     bottom: 0;
     right: 0;
     background-color: green;
}
//方法2
.info {
    position: absolute;
     width:300px;
     height:200px;
     top: 50%;
     left: 50%;
     transform: translate(-50%, -50%);
     background-color: green;
}

水平居中

//方法1
.wrapper{
    display: flex;
    justify-content: center;
}
.info {
     width:300px;
     height:200px;
      background-color: green;
}

//方法2
.info {
    width: 300px;
    height: 200px;
    margin: auto;
    background-color: green;
}

不确定子元素宽高

设置水平居中,先将子元素转化为行内元素,即display:inline;或者display:inline-block;,给父元素设置text-align:center;。这是方法一

.wrapper {
    width: 500px;
    height: 500px;
    border: 1px solid red;
    text-align: center;
}

.info {
    display: inline;
    margin: 0 auto;
    background-color: green;
}

方法二:使用定位居中

.wrapper {
    width: 500px;
    height: 500px;
    border: 1px solid red;
    position: relative;
}

.info {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    background-color: green;
}

方法三:使用flex在弹性布局

.wrapper {
    width: 500px;
    height: 500px;
    border: 1px solid red;
    display: flex;
    justify-content: center;
}

.info {
    //这里可以设置高度
    background-color: green;
}

垂直居中

方法一:

.wrapper {
    width: 500px;
    height: 500px;
    border: 1px solid red;
    display: table-cell;
    vertical-align: middle;
}

.info {
    background-color: green;
}

方法二:

.wrapper {
    width: 500px;
    height: 500px;
    border: 1px solid red;
    position: relative;
}

.info {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background-color: green;
}

如果确定子元素高度,则:

.wrapper {
    width: 500px;
    height: 500px;
    border: 1px solid red;
    position: relative;
}

.info {
    height:100px;
    position: absolute;
    top: 50%;
    margin-top:-50px;
    background-color: green;
}

方法三:使用flex(高定不定都可以)

.wrapper {
    width: 500px;
    height: 500px;
    border: 1px solid red;
    display: flex;
    justify-content: center;
    align-items: center;
}

.info {
    background-color: green;
}

相关文章

  • css关于居中的方式

    父元素没有固定宽高 水平垂直居中 html: css: 水平居中 不确定子元素宽高 设置水平居中,先将子元素转化为...

  • CSS居中小结

    下面是CSS居中的几种方法: 水平居中元素: 通用方法,元素的宽高未知 方式一:CSS3 transform 方式...

  • 前端常见编程题

    CSS篇 垂直居中 方式一 方式二 方式三 方式四 方式五 方式六 水平居中 方式一 方式二 方式三 方式四 布局...

  • 2018-09-26

    转载:CSS实现水平垂直居中的10种方式

  • CSS常见布局技巧

    1.HTML中css水平居中的几种方式

  • CSS布局(不完全)总结

    CSS布局(不完全)总结 实现水平居中布局的几种方式 方法一: 通过以下CSS代码实现水平居中布局 方法二: 通过...

  • (转)水平垂直居中

    CSS实现水平垂直居中的1010种方式(史上最全)

  • 垂直居中的方法

    行内元素居中 html css before元素居中 html css table-cell居中 css 垂直居中...

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

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

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

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

网友评论

      本文标题:css关于居中的方式

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