您现在的位置是:网站首页> 编程资料编程资料

仅使用CSS做到完全居中的超级攻略CSS中的垂直和水平居中完全指南CSS中垂直居中的简单实现方法CSS实现定位元素居中的方法

2023-10-24 223人已围观

简介 这篇文章主要介绍了仅使用CSS做到完全居中效果的方法,强烈推荐!需要的朋友可以参考下

我们都知道 margin:0 auto; 的样式能让元素水平居中,而 margin: auto; 却不能做到垂直居中……直到现在。但是,请注意!想让元素绝对居中,只需要声明元素高度,并且附加以下样式,就可以做到:
 

CSS Code复制内容到剪贴板
  1. .Absolute-Center {   
  2.   marginauto;   
  3.   positionabsolute;   
  4.   top: 0; left: 0; bottombottom: 0; rightright: 0;   
  5. }  

  我并不是第一个发现这种方法的人(不过我还是敢把它叫做“完全居中”),它有可能是种非常普遍的技巧。但大多数介绍垂直居中的文章中并没有提到过这种方法。

  以前从未用过这种方法的我想试试,看看这种”完全居中”的方法到底有多么神奇。 好处:

    跨浏览器,兼容性好(无需hack,可兼顾IE8~IE10)
    无特殊标记,样式更精简
    自适应布局,可以使用百分比和最大最小高宽等样式
    居中时不考虑元素的padding值(也不需要使用box-sizing样式)
    布局块可以自由调节大小
    img的图像也可以使用

  同时注意:

    必须声明元素高度
    推荐设置overflow:auto;样式避免元素溢出,显示不正常的问题
    这种方法在Windows Phone上不起作用

浏览器支持:Chrome、Firefox、Safari、Mobile Safari、IE8-10。 “完全居中”经测试可以完美地应用在最新版本的Chrome、Firefox、Safari、Mobile Safari中,甚至也可以运行在IE8~IE10上
 对照表

  “完全居中”并不是本篇文章中唯一的选项。要做到垂直居中,还存在着其他方法,各有各的长处。采取什么样的方法,取决于你所支持的浏览器,以及现有标签的结构。下面这张对照表能够帮你选出最符合你需要的方法。
2015715155321329.jpg (692×645)

说明

  在研究了规范和文档后,我总结出了“完全居中”的工作原理:

  1.在普通文档流里,margin: auto; 的意思是设置元素的margin-top和margin-bottom为0。

  W3.org If ‘margin-top’, or ‘margin-bottom’ are ‘auto’, their used value is 0.

  2. 设置了position: absolute; 的元素会变成块元素,并脱离普通文档流。而文档的其余部分照常渲染,元素像是不在原来的位置一样。 Developer.mozilla.org …an element that is positioned absolutely is taken out of the flow and thus takes up no space

  3. 设置了top: 0; left: 0; bottom: 0; right: 0; 样式的块元素会让浏览器为它包裹一层新的盒子,因此这个元素会填满它相对父元素的内部空间,这个相对父元素可以是是body标签,或者是一个设置了position: relative; 样式的容器。 Developer.mozilla.org For absolutely positioned elements, the top, right, bottom, and left properties specify offsets from the edge of the element’s containing block (what the element is positioned relative to).

  4. 给元素设置了宽高以后,浏览器会阻止元素填满所有的空间,根据margin: auto; 的要求,重新计算,并包裹一层新的盒子。 Developer.mozilla.org The margin of the [absolutely positioned] element is then positioned inside these offsets.

  5. 既然块元素是绝对定位的,又脱离了普通文档流,因此浏览器在包裹盒子之前会给margin-top和margin-bottom设置一个相等的值。 W3.org If none of the three [top, bottom, height] are ‘auto’: If both ‘margin-top’ and ‘margin-bottom’ are ‘auto’, solve the equation under the extra constraint that the two margins get equal values.?AKA: center the block vertically

  使用“完全居中”,有意遵照了标准margin: auto; 样式渲染的规定,所以应当在与标准兼容的各种浏览器中起作用。
 对齐
  容器内对齐

  使用“完全居中”,就可以在一个设置了position: relative的容器中做到完全居中元素了! (居中例子,请前往英文原文查看)

 

CSS Code复制内容到剪贴板
  1. .Center-Container {   
  2.   positionrelative;   
  3. }   
  4.     
  5. .Absolute-Center {   
  6.   width: 50%;   
  7.   height: 50%;   
  8.   overflow"width: auto; height: auto; float: none;" id="22_nwp">"text-decoration: none;" mpid="22" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f15759f1d844d7fd&k=auto&k0=auto&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=fdd744d8f15957f1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3058%2Ehtml&urlid=0" id="22_nwl">"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">auto;   
  9.   marginauto;   
  10.   positionabsolute;   
  11.   top: 0; left: 0; bottombottom: 0; rightright: 0;   
  12. }  

2015715155406038.jpg (606×400)

接下来的示例会假设已经包含了以下样式,并且以逐步添加样式的方式提供不同的特性。
  在可视区域内居中

  想要使内容区在可视区域内居中么?设置position: fixed样式,并设置一个较高的z-index值,就可以做到。
 

CSS Code复制内容到剪贴板
  1. .Absolute-Center.is-Fixed {   
  2.   positionfixed;   
  3.   z-index: 999;   
  4. }  

2015715155502404.jpg (373×290)

移动版Safari的说明:如果外面没有一层设置position: relative的容器,内容区会以整个文档的高度的中心点为基准居中,而不是以可视区域的高度中心点为基准居中。
  偏移值

  如果需要添加固定的标题,或者其他带偏移样式的元素,可以直接把类似top: 70px; 的样式写进内容区域的样式中。一旦声明了margin: auto; 的样式,内容块的top left bottom right的属性值也会同时计算进去。

  如果想让内容块在贴近侧边的过程中保持水平居中,可以使用right: 0; left: auto; 让内容贴在右侧,或者使用left: 0; right: auto; 使内容贴在左侧。

 

CSS Code复制内容到剪贴板
  1. .Absolute-Center.is-Fixed {   
  2.   positionfixed;   
  3.   z-index: 999;   
  4. }  

2015715155530004.jpg (608×401)

带响应式

  使用absolute的最大好处就是可以完美地使用带百分比的宽高样式!就算是min-width/max-width或者min-height/max-height也能够有如预期般的表现。

  再进一步加上padding样式的话,absolute式的完全居中也丝毫不会破坏!
 

CSS Code复制内容到剪贴板
  1. .Absolute-Center.is-Responsive {   
  2.   width: 60%;    
  3.   height: 60%;   
  4.   min-width200px;   
  5.   max-width400px;   
  6.   padding40px;   
  7. }  

2015715155603781.jpg (608×402)

带溢出内容

  内容区高度大于可视区域或者一个position: relative的容器,其内容可能会溢出容器,或被容器截断。只要内容区域没有超出容器(没有给内容容器预留padding的话,可以设置max-height: 100%;的

相关内容

-六神源码网