cssfun icon indicating copy to clipboard operation
cssfun copied to clipboard

总结一些DIV居中的方法

Open simaQ opened this issue 10 years ago • 10 comments

总结下使DIV居中的不同方法。

1.:+1:the better method:

    可参考How to Center Anything With CSS :clap::clap::clap:

  1. 重要: 先对元素 设置高度
  2. 运用以下规则
    position: absolute;
    margin: auto;
    top: 0; left: 0; right: 0; bottom: 0;
    overflow: auto;

    考虑多浏览器兼容性的话 display: table or display: inline-block;

2.宽度高度不固定DIV水平居中

  html部分

     <div class="container">
     <div class="center"><a href="#">1</a><a href="#">2</a><a href="#">3</a>
     <div style="clear:both"></div></div>  

  css部分

     .container{width:500px;height:80px;background:#C2300B;margin-left:50px;padding-top:10px;text-align:center;}
     .center{display:inline-block;border:2px solid #fff;}
     .center{_display:inline;} /*针对ie6 hack*/
     .center a{float:left;border:1px solid #fff;padding:5px 10px;margin:10px;color:#fff;text-decoration:none;}

  代码要点:

  • 父容器container加css属性 text-align:center;
  • 子容器center加css属性display:inline-block;
  • .center{_display:inline;} 为针对ie6的hack

3.宽度高度不固定DIV垂直居中

  html部分

     <div id="vc"><div id="vci"><div id="content">
     我们垂直居中了,我们水平居中了
     </div></div></div>

  css部分

     #vc { display:table; background-color:#C2300B; width:500px; height:200px; overflow:hidden; margin-left:50px; _position:relative; }
     #vci { vertical-align:middle; display:table-cell; text-align:center; _position:absolute; _top:50%; _left:50%; }
     #content { color:#fff; border:1px solid #fff; display:inline-block; _position:relative; _top:-50%; _left:-50%; }

  代码要点:

  • 父容器vc的css属性 display:table;overflow:hidden;
  • 子容器vci的css属性 vertical-align:middle;display:table-cell;
  • 针对ie6的hack,vci容器的 _position:absolute;_top:50%; 和content容器的 _position:relative; _top:-50%;
  • 如果不需要水平居中的话,需要注释掉vci容器的text-align:center;_left:50%;以及content的display:inline-block;_left:-50%;

4.宽度高度固定水平垂直居中

  html部分

     <div class="guding"><div class="gd">居中了</div></div>

  css部分

    .guding{width:500px;height:200px;background:#c2300b;margin-left:50px;position:relative;}
    .gd{width:50px;height:20px;background:#fff;position:absolute;top:50%;left:50%;margin-top:-10px;margin-left:-25px;}

  代码要点:

  • 父容器要用相对定位position:relative;否则的话子元素会相对于浏览器窗口进行绝对定位。
  • 子容器绝对定位,top:50%;left:50%;margin-top,margin-left的值取该容器高度,宽度的一半的负值。

5. 然后CSS3来了,使用flexbox布局

抛开兼容性,我想这是一个完美的居中,不需要考虑宽度和高度值。

<div id="parent">
    <div id="item">test</div>
</div>
#parent {
    display: flex;
    width: 400px; /* 宽度值,随便啦 */
    height: 400px;  /* 高度值,随便啦 */
    background-color: yellow;
}

#item {
    width: 100px;/* 宽度值,随便啦 */
    height: 20px;/* 高度值,随便啦 */
    margin: auto;
    background-color: red; /* Magic! */
}

simaQ avatar Nov 25 '14 14:11 simaQ

多谢分享,很是感谢

wjch avatar Dec 25 '15 03:12 wjch

感谢分享: )

a1exlism avatar Sep 25 '16 13:09 a1exlism

感谢分享;+利用绝对定位,transform来居中。

skyFi avatar Oct 27 '16 04:10 skyFi

很赞,补充一点:

<div id="parent">
    <div id="item">test</div>
</div>
#parent {
    display: flex;
    width: 400px; /* 宽度值,随便啦 */
    height: 400px;  /* 高度值,随便啦 */
    justify-content: center;
    align-items: center;
    background-color: yellow;
}

#item {
    width: 100px;/* 宽度值,随便啦 */
    height: 20px;/* 高度值,随便啦 */
    background-color: red; /* Magic! */
}

bilibiliou avatar Mar 09 '17 16:03 bilibiliou

thank you!

AppleLliao avatar Jun 21 '17 01:06 AppleLliao

thank you

smilepasta avatar Jun 27 '17 02:06 smilepasta

Good!

web1992 avatar Sep 28 '17 04:09 web1992

前端面试必备..

hunterhug avatar Dec 04 '17 09:12 hunterhug

多谢!

tsyzzz avatar May 30 '18 08:05 tsyzzz

#parent { display: flex; width: 400px; /* 宽度值,随便啦 / height: 400px; / 高度值,随便啦 */ background-color: yellow; }

#item { width: 100px;/* 宽度值,随便啦 / height: 20px;/ 高度值,随便啦 / margin: auto; background-color: red; / Magic! */ }

ghost avatar Jul 30 '18 09:07 ghost