资讯专栏INFORMATION COLUMN

如何实现CSS居中?–CSS居中常用方法

Carl / 2318人阅读

摘要:文章参考翻译不当之处请谅解一水平居中内联元素居中相对父级块级元素居中对齐块级元素居中设置和为让它居中同时还要设置,否则它就会承满整个容器,无法看出居中效果如果有很多块级元素需要水平居中成一行,最好使用一个不同的类型。

文章参考:http://css-tricks.com/centering-css-complete-guide/?utm_source=ourjs.com(翻译不当之处请谅解)

一、水平居中 1、内联元素居中:相对父级块级元素居中对齐
   1: .center-children {

   2:   text-align: center;

   3: }
2、块级元素居中:设置margin-left和margin-right为auto让它居中(同时还要设置width,否则它就会承满整个容器,无法看出居中效果)
   1: .center-me {

   2:   margin: 0 auto;

   3: }

如果有很多块级元素需要水平居中成一行,最好使用一个不同的display类型。这是一个使用inline-block和flex的例子。

演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/embedded/result/

1: 
2:
3: I"m an element that is block-like with my siblings and we"re centered in a row. 4:
5:
6: 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. 7:
8:
9: I"m an element that is block-like with my siblings and we"re centered in a row. 10:
11:
12:
13:
14: I"m an element that is block-like with my siblings and we"re centered in a row. 15:
16:
17: 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. 18:
19:
20: I"m an element that is block-like with my siblings and we"re centered in a row. 21:
22:

css:

  1: body {

   2:   background: #f06d06;

   3:   font-size: 80%;

   4: }

   5: main {

   6:   background: white;

   7:   margin: 20px 0;

   8:   padding: 10px;

   9: }

  10: main div {

  11:   background: black;

  12:   color: white;

  13:   padding: 15px;

  14:   max-width: 125px;

  15:   margin: 5px;

  16: }

  17: .inline-block-center {

  18:   text-align: center;

  19: }

  20: .inline-block-center div {

  21:   display: inline-block;

  22:   text-align: left;

  23: }

  24: .flex-center {

  25:   display: flex;

  26:   justify-content: center;

  27: }
二、垂直居中 1、内联元素:设置相等的上下padding,或者利用line-height
   1: .link {

   2:   padding-top: 30px;

   3:   padding-bottom: 30px;

   4: }

文本不会换行的情况下,可以使用line-height,让其与height相等去对齐文本。

 1: .center-text-trick {

   2:   height: 100px;

   3:   line-height: 100px;

   4:   white-space: nowrap;

   5: }

多行的文本也可以利用上下等padding的方式也可以让多行居中,但是如果这方法没用,你可以让这些文字的容器按table cell模式显示,然后设置文字的vertical-align属性对齐,
演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/1/embedded/result/
html:

  1: 

   2:   

   3:     

   6:   

   7: 
4: I"m vertically centered multiple lines of text in a real table cell. 5:
8:
9:

I"m vertically centered multiple lines of text in a CSS-created table layout.

10:

css

  1: body {

   2:   background: #f06d06;

   3:   font-size: 80%;

   4: }

   5: table {

   6:   background: white;

   7:   width: 240px;

   8:   border-collapse: separate;

   9:   margin: 20px;

  10:   height: 250px;

  11: }

  12: table td {

  13:   background: black;

  14:   color: white;

  15:   padding: 20px;

  16:   border: 10px solid white;

  17:   /* default is vertical-align: middle; */

  18: }

  19: .center-table {

  20:   display: table;

  21:   height: 250px;

  22:   background: white;

  23:   width: 240px;

  24:   margin: 20px;

  25: }

  26: .center-table p {

  27:   display: table-cell;

  28:   margin: 0;

  29:   background: black;

  30:   color: white;

  31:   padding: 20px;

  32:   border: 10px solid white;

  33:   vertical-align: middle;

  34: }
2、块级元素

若元素有固定高度,可以这样垂直居中

 1: .parent {

   2:   position: relative;

   3: }

   4: .child {

   5:   position: absolute;

   6:   top: 50%;

   7:   height: 100px;

   8:   margin-top: -50px; /* 如果没有使用: border-box; 的盒子模型则需要设置这个 */

   9: }

如果不知道元素高度,则这样

 1: .parent {

   2:   position: relative;

   3: }

   4: .child {

   5:   position: absolute;

   6:   top: 50%;

   7:   transform: translateY(-50%);

   8: }

也可以使用flexbox

   1: 
2:
3: I"m a block-level element with an unknown height, centered vertically within my parent. 4:
5:
1: body { 2: background: #f06d06; 3: font-size: 80%; 4: } 5: main { 6: background: white; 7: height: 300px; 8: width: 200px; 9: padding: 20px; 10: margin: 20px; 11: display: flex; 12: flex-direction: column; 13: justify-content: center; 14: resize: vertical; 15: overflow: auto; 16: } 17: main div { 18: background: black; 19: color: white; 20: padding: 20px; 21: resize: vertical; 22: overflow: auto; 23: }
三、同时水平和垂直居中 1、元素有固定高度和宽度:先绝对居中,然后上移和左移50%的宽度即可
 1: //这种方案有极好的跨浏览器支持。

   2: .parent {

   3:   position: relative;

   4: }

   5: .child {

   6:   width: 300px;

   7:   height: 100px;

   8:   padding: 20px;

   9:   position: absolute;

  10:   top: 50%;

  11:   left: 50%;

  12:   margin: -70px 0 0 -170px;

  13: }
2、元素的高度和宽度未知或可变的:使用transofrm属性在两个方向都平移负50%
 1: .parent {

   2:   position: relative;

   3: }

   4: .child {

   5:   position: absolute;

   6:   top: 50%;

   7:   left: 50%;

   8:   transform: translate(-50%, -50%);

   9: }

原文首发:http://www.ido321.com/824.html

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/110993.html

相关文章

  • 前端-CSS3&H5

    摘要:高度模型浅识为的简写,简称为块级格式化上下文,为浏览器渲染某一区域的机制,中只有和中还增加了和。并非所有的布局都会在开发中使用,但是其中也会涉及一些知识点。然而在不同的纯制作各种图形纯制作各种图形多图预警 一劳永逸的搞定 flex 布局 寻根溯源话布局 一切都始于这样一个问题:怎样通过 CSS 简单而优雅的实现水平、垂直同时居中。记得刚开始学习 CSS 的时候,看到 float 属性不...

    xiaolinbang 评论0 收藏0
  • CSS常用布局简洁解决方案

    摘要:与常人的直觉不符的是,实际上表示视口宽度的,而不是。与类似,表示视口高度的。存在问题它只适用于在视口中居中的场景基于的解决方案伸缩盒是专门针对这类需求所设计的。 相关基础知识 1.内部与外部尺寸模型:(w3c草案)亲测google可支持。(http://w3.org/TR/css3-sizing ) 基于原有CSS尺寸特性,可以使CSS更容易描述内容自适应以及适应固定上下文的盒模型: ...

    2450184176 评论0 收藏0
  • CSS—总结常用垂直居中方法

    摘要:总结常用垂直居中方法与方法实现居中这是比较常用的方法。绝对居中实现垂直居中这是一个兼容性比较好的能够实现垂直居中的方法。 CSS—总结常用垂直居中方法 1、text-align与line-hight方法实现居中 这是比较常用的方法。通过line-hight来设置行间距是实现垂直居中的关键 .wrap{ width: 500px; heidth: 200px; l...

    asoren 评论0 收藏0

发表评论

0条评论

Carl

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<