简体   繁体   English

垂直/水平居中水平块元素

[英]Vertically / Horizontally Center Block Level Elements

My requirement is that that the dimensions of both the element and parent element are liquid and fluctuate. 我的要求是,元素和父元素的尺寸都是液体并且会波动。 Thus, basically placing a block inside a block, where their respective dimensions are completely unknown. 因此,基本上将一个块放置在一个块中,而它们各自的尺寸是完全未知的。

I have been using JavaScript to calculate them at run time, considering only JavaScript can look in the DOM at run time and calculate this. 考虑到只有JavaScript可以在运行时查看DOM并进行计算,因此我一直在使用JavaScript在运行时计算它们。

@Shane, @Shane,

CSS3 introduced the Flexible box model, maybe you can use it depending the audience of your website... CSS3引入了弹性框模型,也许您可​​以根据您的网站的受众来使用它...

So to Vertically & Horizontally center block Level elements in the body element, you'd just have to write this CSS declaration: 因此,要在body元素中垂直和水平居中放置Level元素,只需编写以下CSS声明即可:

body {
  display: box;
  box-orient: horizontal;
  /* horizontally centered */
  box-pack: center;
  /* vertically centered */
  box-align: center;
  width: 100%;
  height : 100%;
}

http://hacks.mozilla.org/2010/04/the-css-3-flexible-box-model/ http://hacks.mozilla.org/2010/04/the-css-3-flexible-box-model/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM