css元素居中 垂直水平 定宽不定宽

主要参考了这篇文章

如何居中一个元素(终结版)

行内元素

水平居中

利用 text-align: center 可以实现在块级元素内部的行内元素水平居中。此方法对inline、inline-block、inline-table和inline-flex元素水平居中都有效。

  .parent{
        text-align:center;//在父容器设置
    }

此外,如果块级元素内部包着也是一个块级元素,我们可以先将其由块级元素改变为行内块元素,再通过设置行内块元素居中以达到水平居中。

<div class="parent">
  <div class="child">Demo</div>
</div>
<style>
  .parent{
    text-align:center;  
  }
  .child {
    display: inline-block;
  }
</style>

垂直居中

单行 内联元素

通过 line-height

<div id="box">
     <span>单行内联元素垂直居中。</span>。
</div>
<style>
 #box {
    height: 120px;
    line-height: 120px;
    border: 2px dashed #f69c55;
    }
</style>

多行内联元素

利用flex布局

利用flex布局实现垂直居中,其中flex-direction: column定义主轴方向为纵向。这种方式在较老的浏览器存在兼容性问题。

<div class="parent">
    <p>Dance like nobody is watching, code like everybody is.    
    Dance like nobody is watching, code like everybody is.    
    Dance like nobody is watching, code like everybody is.</p>
</div>
<style>
    .parent { 
        height: 140px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        border: 2px dashed #f69c55;
    }
</style>
利用表布局(table)

利用表布局的vertical-align: middle可以实现子元素的垂直居中

<div class="parent">
    <p class="child">The more technology you learn, the more you realize how little you know.
    The more technology you learn, the more you realize how little you know.
    The more technology you learn, the more you realize how little you know.</p>
</div>
 <style>
    .parent {
        display: table;
        height: 140px;
        border: 2px dashed #f69c55;
    }
    .child {
        display: table-cell;
        vertical-align: middle;
    }
</style>

块级元素

垂直居中

已知child高度宽度 使用absolute+负margin

<div class="parent">
    <div class="child">固定高度的块级元素垂直居中。</div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px;
}
</style>

未知child宽高 使用absolute+transform (ie9+)

transform属性只支持到ie9

<div class="parent">
    <div class="child">未知高度的块级元素垂直居中。</div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
</style>

使用flex+align-items

通过设置flex布局中的属性align-items,使子元素垂直居中。

<div class="parent">
    <div class="child">未知高度的块级元素垂直居中。</div>
</div>
<style>
.parent {
    display:flex;
    align-items:center;
}
</style>

水平居中

元素定宽:margin:0 auto;

.child{
     100px;//确保该块级元素定宽
    margin:0 auto;
}

使用table+margin

先将子元素设置为块级表格来显示(类似),再将其设置水平居中

display:table在表现上类似block元素,但是宽度为内容宽。

<div class="parent">
  <div class="child">Demo</div>
</div>
<style>
  .child {
    display: table;
    margin: 0 auto;
  }
</style>

使用absolute+transform (ie9+)

先将父元素设置relative,再将子元素设置为absolute,向右移动子元素,移动距离为父容器的一半,最后通过向左移动子元素的一半宽度以达到水平居中。

<div class="parent">
  <div class="child">Demo</div>
</div>
<style>
  .child {
    position:absolute;
    left:50%;
    transform:translateX(-50%);
  }
  .parent {
    position:relative;
  }
</style>

使用flex+justify-content

通过CSS3中的布局利器flex中的justify-content属性来达到水平居中。

<div class="parent">
  <div class="child">Demo</div>
</div>
<style>
  .parent {
    display: flex;
    justify-content:center;
  }
</style>

使用flex+margin

通过flex将父容器设置为为Flex布局,再设置子元素居中。

<div class="parent">
  <div class="child">Demo</div>
</div>
<style>
  .parent {
    display: flex;
  }
  .child {
    margin:0 auto;
  }
</style>

多块级元素水平居中

多块级元素水平居中

利用flex布局

 #container {
    display: flex;
    justify-content: center;
}

利用inline-block

将要水平排列的块状元素设为display:inline-block,然后在父级元素上设置text-align:center,达到与上面的行内元素的水平居中一样的效果。

.container {
text-align: center;
}
.inline-block {
display: inline-block;
}
原文地址:https://www.cnblogs.com/riwang/p/12370983.html