CSS实现居中的7种方法_html/css_WEB-ITnose

WBOY
풀어 주다: 2016-06-24 11:53:16
원래의
1181명이 탐색했습니다.

实现HTML元素的居中 看似简单,实则不然

水平居中比如容易,垂直居中比较难搞定,水平垂直都居中更不容易。在这个响应式布局的年代,很难固定元素的宽高,俺统计了一下,目前的几种方法。本文由浅入深逐个介绍,使用了同一段HTML代码:

<div class="center">
로그인 후 복사</div></div>
<img src="jimmy-choo-shoe.jpg" alt="">
로그인 후 복사</div></div>
</div>
로그인 후 복사</div></div>
로그인 후 복사</div></div>

下面鞋子图片会变化但原始大小始终是500px × 500px,主题背景颜色使用了HSL colors

1.水平居中?使用 text-align

有些场景下 简单的方法就是最好的方法

div.center { text-align: center; background: hsl(0, 100%, 97%); }
로그인 후 복사</div></div>
div.center img { width: 33%; height: auto; }
로그인 후 복사</div></div>

但该方法不能让图片垂直居中:需要给 div 添加 padding 或 给 div 中的元素添加 margin-top 和 margin-bottom

2. margin: auto 居中

同样也是水平居中,局限性跟第1种方法一样:

div.center { background: hsl(60, 100%, 97%); }
로그인 후 복사</div></div>
div.center img { display: block; width: 33%; height: auto; margin: 0 auto; }
로그인 후 복사</div></div>

注意 display: block, 这种情况下必须有 margin: 0 auto.

3. table-cell 居中

使用 display: table-cell, 可以实现水平垂直都居中。通常需要添加一个额外的空元素。

<div class="center-aligned">
로그인 후 복사</div></div>
<div class="center-core">
로그인 후 복사</div></div>
<img  src="jimmy-choo-shoe.jpg" alt="CSS实现居中的7种方法_html/css_WEB-ITnose" >
로그인 후 복사</div></div>
</div>
로그인 후 복사</div></div>
로그인 후 복사</div></div>
</div>
로그인 후 복사</div></div>

CSS 代码:

.center-aligned { display: table; background: hsl(120, 100%, 97%);width: 100%; }
로그인 후 복사</div></div>
.center-core { display: table-cell; text-align: center; vertical-align:middle; }
로그인 후 복사</div></div>
.center-core img { width: 33%; height: auto; }
로그인 후 복사</div></div>

注意 width: 100% 是为了防止 div 折叠,外面的容器需要一个高度才能垂直居中。 如果垂直居中的元素是放在 . body 中的话,需要给 html 和 body 设置 height. 在所有浏览器中均有效,包括 IE8+.

4. Absolute 居中

最近 Stephen Shaw 推广的一项新技术可以很好地兼容各种浏览器。唯一的缺点是外部容器必须声明height

.absolute-aligned {
로그인 후 복사</div></div>
position: relative; min-height: 500px;
로그인 후 복사</div></div>
background: hsl(200, 100%, 97%);
로그인 후 복사</div></div>
.absolute-aligned img {
로그인 후 복사</div></div>
width: 50%;
로그인 후 복사</div></div>
min-width: 200px;
로그인 후 복사</div></div>
height: auto;
로그인 후 복사</div></div>
overflow: auto; margin: auto;
로그인 후 복사</div></div>
position: absolute;
로그인 후 복사</div></div>
top: 0; left: 0; bottom: 0; right: 0;
로그인 후 복사</div></div>

Stephen 在 他的文章 中验证了这段代码的许多版本

5. 使用 translate 居中

Chris Coiyer 提出了一个新的方法:使用 CSS transforms. 同时支持水平居中和垂直居中:

.center { background: hsl(180, 100%, 97%); position: relative; min-height: 500px; }
로그인 후 복사</div></div>
.center img { position: absolute; top: 50%; left: 50%;
로그인 후 복사</div></div>
transform: translate(-50%, -50%); width: 30%; height: auto; }
로그인 후 복사</div></div>

有以下缺点:

  • CSS transform 需要针对不同的浏览器使用 特定的前缀 (-moz 或 -o 或 -webkit)
  • 在低版本的IE (IE8 及以下 )中无效
  • 外部容器需要设置高度 height (or gain it in some other way) 因为它不能从它的absolutely-positioned 内容上获得高度.
  • 如果内容包含 text, 当前浏览器合成技术对文本解释得很模糊.
  • 6. 使用 Flexbox 居中

    一旦属性变量和特定前缀消失的话,这种方法很可能成为首选的居中方案.

    .center { background: hsl(240, 100%, 97%); display: flex; justify-content: center; align-items: center; }
    로그인 후 복사</div></div>
    .center img { width: 30%; height: auto; }
    로그인 후 복사</div></div>

    在许多方面 flexbox 是最简单的解决方案,但制约因素是 各种陈旧语法和低版本的IE, (尽管 display: table-cell是一个可以接受的方案). 完整的 CSS代码:

    .center { background: hsl(240, 100%, 97%);
    로그인 후 복사</div></div>
    display: -webkit-box; /* OLD: Safari, iOS 6 and earlier; Android browser, older WebKit */
    로그인 후 복사</div></div>
    display: -moz-box; /* OLD: Firefox (can be buggy) */
    로그인 후 복사</div></div>
    display: -ms-flexbox; /* OLD: IE 10 */
    로그인 후 복사</div></div>
    display: -webkit-flex; /* FINAL, PREFIXED, Chrome 21+ */
    로그인 후 복사</div></div>
    display: flex; /* FINAL: Opera 12.1+, Firefox 22+ */
    로그인 후 복사</div></div>
    -webkit-box-align: center;
    로그인 후 복사</div></div>
    -moz-box-align: center;
    로그인 후 복사</div></div>
    -ms-flex-align: center;
    로그인 후 복사</div></div>
    -webkit-align-items: center;
    로그인 후 복사</div></div>
    align-items: center;
    로그인 후 복사</div></div>
    -webkit-box-pack: center;
    로그인 후 복사</div></div>
    -moz-box-pack: center;
    로그인 후 복사</div></div>
    -ms-flex-pack: center;
    로그인 후 복사</div></div>
    -webkit-justify-content: center;
    로그인 후 복사</div></div>
    justify-content: center;
    로그인 후 복사</div></div>

    现在规范已经形成,浏览器也支持, I have written extensively on flexbox layout and its uses.

    7. 使用 calc 居中

    在某些场景下比 flexbox 更通用:

    .center { background: hsl(300, 100%, 97%); min-height: 600px; position:relative; }
    로그인 후 복사</div></div>
    .center img { width: 40%; height: auto; position: absolute; top:calc(50% - 20%); left: calc(50% - 20%); }
    로그인 후 복사</div></div>

    显而易见, calc 允许在当前的页面布局上进行计算。在上面的例子中,50% 是容器中元素的中间点,但是单独使用会使得image的左上角位于

    中间。我们需要把width 和height 再往回拉一下,大小分别是图片width 和height 的一半。表达式如下:

    top: calc(50% - (40% / 2)); left: calc(50% - (40% / 2));
    로그인 후 복사</div></div>

    在目前的浏览器中,你可以发现:当内容 fixed 且大小已知的时候,该技术效果最佳:

    .center img { width: 500px; height: 500px; position: absolute; top:calc(50% - (300px / 2)); left: calc(50% - (300px ? 2)); }
    로그인 후 복사</div></div>

    calc 这种方法跟 flexbox 一样也有很多潜在的缺点: 支持Firefox 4 及更高版本浏览器,对于更早版本的浏览器,需要添加前缀,不支持IE8。图片居中的完整代码:

    .center img { width: 40%; height: auto; position: absolute;
    로그인 후 복사</div></div>
    top: -webkit-calc(50% - 20%); left: -webkit-calc(50% - 20%);
    로그인 후 복사</div></div>
    top: -moz-calc(50% - 20%); left: -moz-calc(50% - 20%);
    로그인 후 복사</div></div>
    top: calc(50% - 20%); left: calc(50% - 20%); }
    로그인 후 복사</div></div>

    当然还有很多其他的方法,例如 使用伪元素来垂直居中 , 理解这些技术可以让web开发人员在处理居中问题的时候不麻爪!

    原文在这里

    관련 라벨:
    원천:php.cn
    본 웹사이트의 성명
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
    최신 이슈
    인기 튜토리얼
    더>
    최신 다운로드
    더>
    웹 효과
    웹사이트 소스 코드
    웹사이트 자료
    프론트엔드 템플릿
    회사 소개 부인 성명 Sitemap
    PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!