简体   繁体   English

垂直居中高度未知的div

[英]Vertically center a div with unknown height

Ok, i know it has been asked several times but i didn't find a cross browser solution and don't get the hang of it. 好吧,我知道有几次被问过,但我没有找到一个跨浏览器的解决方案而且没有得到它的支持。 :-( :-(

I have this code to scale a video to 100% browser width: 我有这个代码将视频缩放到100%浏览器宽度:

<div id="flashposition"><div id="flashvimeo"class="vimeo">HereIsTheVideo</div></div>

and this css: 这个css:

#flashposition {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 0px;
height: 0;
z-index:30;
display: block;
overflow:hidden;
}

#flashvimeo object, embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

It works, but cuts off the excess height at the bottom, but i would like it to be centered, so excess height of the video is cut of half at the top and bottom. 它可以工作,但会切断底部多余的高度,但我希望它能够居中,因此视频的多余高度在顶部和底部被切成两半。

Any ideas that are safe and also work in IE7? 任何安全的想法也适用于IE7? Or is the only way to do it in js/jquery and recalculating height/width due to browsers window size like here http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php ? 或者是js / jquery中的唯一方法,并且由于浏览器窗口大小而重新计算高度/宽度,如http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php

Unfortunately there isn't an elegant cross browser, backward compatible solution to this without script. 不幸的是,没有一个优雅的跨浏览器,向后兼容的解决方案没有脚本。

However CSS3 supports box-flex with box-orient: horizontal; 然而,CSS3支持box-flex, box-orient: horizontal; which is designed to do exactly what you want. 这是为了做你想要的。 Browser support for CSS3 is still a bit thin on the ground so if you want a solution that works for everyone you'll need to use the window resize event and layout with script. 对CSS3的浏览器支持仍然有点薄,所以如果你想要一个适合每个人的解决方案,你需要使用窗口调整大小事件和脚本布局。

HTML 5 Rocks - flexbox HTML 5 Rocks - flexbox

This answer may be disgusting but Ive found that the easier and cross browser way to vertical centering any element in html/css, is to use table elements. 这个答案可能令人作呕,但我发现使用表格元素更容易和跨浏览器的方式垂直居中html / css中的任何元素。 Other way is using JS to calculate the container height and then positioning. 其他方法是使用JS计算容器高度然后定位。

This trick is pretty good: http://css-tricks.com/centering-in-the-unknown/ 这个技巧非常好: http//css-tricks.com/centering-in-the-unknown/

/* This parent can be any width and height */
.block {
  text-align: center;
}

/* The ghost, nudged to maintain perfect centering */
.block:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */
}

/* The element to be centered, can also be of any width and height */ 
.centered {
  display: inline-block;
  vertical-align: middle;
  width: 300px;
}

This method over at CSS-tricks works in all browsers and IE8+, it is the closest you're going to get to a method with good browser support outside of tables. CSS-tricks上的这种方法适用于所有浏览器和IE8 +,它是最接近表格之外具有良好浏览器支持的方法。

Demo here: http://jsfiddle.net/YqKMH/show/ 演示: http//jsfiddle.net/YqKMH/show/

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

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