[DIV/CSS] [译]CSS 居中(Center)方法大合集

来源:互联网 发布:我国粮食产量 知乎 编辑:程序博客网 时间:2024/05/16 12:48
水平居中 行内元素

对于行内元素(inline)或复合行内元素(inline-*),直接为其父元素设置文本居中即可。

  1. .center-children {
  2. text-align: center;
  3. }http://www.ynmxzx.com/rzjw/20160622/3739.html
复制代码

http://codepen.io/chriscoyier/pen/HulzB

该方法适用于 inline、inline-block、inline-table、inline-flex 之类的元素。

块级元素

对于块级元素,如果给定了宽度,直接设置左右边距为 auto 即可。
注意:对于未指定宽度的元素,默认会占满允许的最大宽度,这时设置居中是无效的。

  1. .center-me {
  2. margin: 0 auto;
  3. }http://www.ynmxzx.com/rzjw/20160622/3740.html
复制代码

http://codepen.io/chriscoyier/pen/eszon

不管元素或者父元素的宽度如何,只要指定了宽度,该方法就有效。

但是,对于设置了浮动(float)的元素就不行了,这里有一个取巧的办法可以实现浮动元素居中。

多个块级元素

对于多个块级元素需要整体居中的情况,还可细分为以下三类:

并排显示,高度无要求

如果不要求并排的多个块级元素有同样的高度,那么可为这些块级元素的父元素设置 display: inline-block 属性,以实现所需效果。

  1. <main class="inline-block-center">
  2. <div>I'm an element that is block-like with my siblings and we're centered in a row.</div>
  3. <div>I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.</div>
  4. <div>I'm an element that is block-like with my siblings and we're centered in a row.</div>
  5. </main>http://www.ynmxzx.com/rzjw/20160622/3741.html
复制代码
  1. .inline-block-center {
  2. text-align: center;
  3. }
  4. .inline-block-center div {
  5. display: inline-block;
  6. text-align: left;
  7. }http://www.ynmxzx.com/rzjw/20160622/3742.html
复制代码

http://codepen.io/chriscoyier/pen/ebing

并排显示,要求相同高度

如果要求多个块级元素并排居中且高度相同,则要为其父元素设置 display: flex 属性。

  1. <main class="flex-center">
  2. <div>I'm an element that is block-like with my siblings and we're centered in a row.</div>
  3. <div>I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.</div>
  4. <div>I'm an element that is block-like with my siblings and we're centered in a row.</div>
  5. </main>http://www.ynmxzx.com/rzjw/20160622/3743.html
复制代码
  1. .flex-center {
  2. display: flex;
  3. justify-content: center;
  4. }
复制代码
成一列显示

如果只是需要让多个块级元素整体水平居中,并且按默认的方式纵向排列,那直接设置左右边距为 auto 即可。

http://codepen.io/chriscoyier/pen/haCGt


垂直居中

要实现元素的垂直居中就需要一些小技巧了。

行内元素 单行内容

为行内元素/文本元素设置相等的上下内边距即可实现单行元素的垂直居中。

  1. .link {http://www.ynmxzx.com/rzjw/20160622/3744.html
  2. padding-top: 30px;
  3. padding-bottom: 30px;
  4. }
复制代码

http://codepen.io/chriscoyier/pen/ldcwq

如果出于种种原因不能用上面的设置上下内边距的方式,那么可以设置行高与元素的高度相同,这样也能实现垂直居中的效果,但是使用这种方法要保证文本不会换行。

  1. .center-text-trick {
  2. height: 100px;
  3. line-height: 100px;
  4. white-space: nowrap;
  5. }http://www.ynmxzx.com/xbzx/20160622/3745.html
复制代码

http://codepen.io/chriscoyier/pen/LxHmK

多行内容

对多行文本设置相等的上下内边距也是可以实现垂直居中的,但是有时候会因为文本在表格的单元格中,或者文本所属的元素通过 CSS 设置为表格单元格的表现方式,而无法实现垂直居中的效果。在这种情况下,可以用 vertical-align 属性来实现垂直居中。

http://codepen.io/chriscoyier/pen/ekoFx

上面的方式不可行的话,还可以使用 flexbox,flex 只有一个子元素的话,要实现垂直居中还是很简单的。

  1. .flex-center-vertically {
  2. display: flex;
  3. justify-content: center;
  4. flex-direction: column;
  5. height: 400px;
  6. }http://www.ynmxzx.com/xbzx/20160622/3746.html
复制代码

http://codepen.io/chriscoyier/pen/uHygv

对于 flexbox 方法,原文中说到父元素有指定高度之后,这种方法才有意义,但是通过测试代码发现,父元素未指定高度也是可以垂直居中的。
Remember that it's only really relevant if the parent container has a fixed height (px, %, etc), which is why the container here has a height.

除此之外,还可以使用 ghost element,这种方法是将一个完整高度的伪类放在容器中,让文本与这个伪类纵向居中对齐。

  1. .ghost-center {
  2. position: relative;
  3. }
  4. .ghost-center::before {
  5. content: " ";
  6. display: inline-block;
  7. height: 100%;
  8. width: 1%;
  9. vertical-align: middle;
  10. }http://www.ynmxzx.com/xbzx/20160622/3747.html
  11. .ghost-center p {
  12. display: inline-block;
  13. vertical-align: middle;
  14. }
复制代码

http://codepen.io/chriscoyier/pen/ofwgD

块级元素 元素高度已知

在网页布局中,元素高度不确定的情况很普遍:比如元素宽度改变,文本重排之后元素高度就会改变。文本的样式不同,元素高度也会不同。文本的字数不同,元素高度也会不同。具有固定高宽比的元素,比如图片,当尺寸改变时,其高度也会变化,等等情况。

但是如果元素高度已知,可以用下面的方法来实现垂直居中:

  1. .parent {
  2. position: relative;
  3. }
  4. .child {
  5. position: absolute;
  6. top: 50%;
  7. height: 100px;
  8. marglin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
  9. }http://www.ynmxzx.com/xbzx/20160622/3748.htm
复制代码

http://codepen.io/chriscoyier/pen/HiydJ

元素高度未知

该方法就是先将元素相对于其原始位置向下移动父元素高度的一半距离,再将该元素相对其本身的高度向上移动一半,这样就能实现垂直居中的效果了。

  1. .parent {
  2. position: relative;
  3. }
  4. .child {
  5. position: absolute;
  6. top: 50%;
  7. transform: translateY(-50%);
  8. }
复制代码

http://codepen.io/chriscoyier/pen/lpema

简单粗暴的 flexbox

用 flexbox 的话,实现起来就简单多了。

  1. .parent {
  2. display: flex;
  3. flex-direction: column;
  4. justify-content: center;
  5. }www.ynmxzx.com
复制代码

http://codepen.io/chriscoyier/pen/FqDyi


水平和垂直均居中

简单地把上面提到的实现水平居中和垂直居中的方法结合起来自然是可以的,但是这个需求也可以分为以下三类来实现:

元素宽度及高度已知

将元素相对于其父元素的宽度/高度值向右并向下移动一半的距离,这时元素左上角的顶点刚好位于父元素的中心。然后再通过设置负边距值的方法,将元素相对于其自身的宽度/高度值向左并向上移动一半的距离,就可实现水平垂直均居中的效果了。并且这种方法的浏览器兼容性是很好的。

  1. .parent {
  2. position: relative;
  3. }
  4. .child {
  5. width: 300px;
  6. height: 100px;
  7. padding: 20px;
  8. position: absolute;
  9. top: 50%;
  10. left: 50%;
  11. margin: -70px 0 0 -170px;
  12. }
复制代码

http://codepen.io/chriscoyier/pen/JGofm

元素的宽度或高度未知

如果元素的宽度或者高度未知,则在将元素相对于父元素的宽高往向右并向下移动一半距离后,再用 transform 属性来将其向左并向上移动自身宽度及高度值一般的距离即可。

  1. .parent {
  2. position: relative;
  3. }
  4. .child {
  5. position: absolute;
  6. top: 50%;
  7. left: 50%;
  8. transform: translate(-50%, -50%);
  9. }
复制代码

http://codepen.io/chriscoyier/pen/lgFiq

flexbox 大法

要通过 flexbox 来实现水平垂直均居中,就要使用两个居中属性来实现:

  1. .parent {
  2. display: flex;
  3. justify-content: center;
  4. align-items: center;
  5. }
复制代码
0 0
原创粉丝点击