简体   繁体   English

垂直和水平居中的HTML5 Javascript

[英]Vertically and horizontally centering HTML5 Javascript

So I have this and want to center it vertically and horizontally: 所以我有这个,想垂直和水平居中:

<div id="hd_hype_container" style="position:relative;overflow:hidden;width:600px;height:405px;">
    <script type="text/javascript" charset="utf-8" src="hd/hd.js?88618"></script>
</div>

I have tried every solution and none seem to work for me. 我已经尝试了所有解决方案,但似乎没有一个适合我。 Do I need to add something into the js? 我需要在js中添加一些内容吗?

I'd appreciate any help. 我将不胜感激。

Thanks. 谢谢。

Try: 尝试:

#hd_type_container {
    position: absolute;
    top: 50%;
    height: 50%;
    width: whatever;
    height: whatever; 
    margin-left: -whatever_width_is / 2;
    margin-top: -whatever_height_is / 2;
}

Remember though that absolute or relatively positioned elements are positioned relative to the "closest" parent container that's also absolute or relatively positioned. 请记住,尽管绝对或相对定位的元素是相对于“绝对”或相对定位的“最近”父容器定位的。

In other words, if your div is a child of some other div that's absolutely positioned, you won't be centering to the screen, but centering to the parent div . 换句话说,如果您的div是绝对定位的其他div的子代,则您不会以屏幕为中心,而是以父div为中心。

Update: Assuming you are trying to make a popup box of some kind, you might also want an overflow: hidden directive too. 更新:假设您试图制作某种弹出框,您可能还需要一个overflow: hidden指令。

<div id="hd_hype_container" style="margin-top:50%;margin:0 auto;width:600px;height:405px;">
    <script type="text/javascript" charset="utf-8" src="hd/hd.js?88618"></script>
</div>

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

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