简体   繁体   English

在“浏览器”窗口中将Div垂直对齐(-不在其他元素内)

[英]Vertically align a Div in the Browser window (-not- within another element)

I have a div which i want vertically aligned to 50% of the height of the browser window at all times. 我有一个div,我想一直垂直对齐浏览器窗口高度的50%。

I don't know what the height of the browser window is going to be at all times, should the user scale this window. 我不知道浏览器窗口的高度在任何时候都应该是什么,用户是否应该缩放该窗口。 If placing it within another element is necessary, great, but as just specified, I have no idea how tall the viewport is going to be at any one time. 如果将它放置在另一个元素中是必要的,那很好,但是正如刚刚指定的那样,我不知道任何时候视口有多高。

I'm not going to be using javascript either. 我也不会使用javascript。

I have read through the site, i have gone hunting for a solution, but I really want to throw this out there (again) as I have yet to find a solution that does exactly this, either by hook or by crook. 我已经阅读了该站点,我一直在寻找解决方案,但是我真的想(再次)将其扔在那里,因为我还没有找到可以完全做到这一点的解决方案,无论是通过钩子还是通过弯曲。

Thanks. 谢谢。

You don't specify if the has a fixed height or not? 您没有指定高度是否固定? If so then you can do this with one element, just add the following example CSS: 如果是这样,那么您可以使用一个元素执行此操作,只需添加以下示例CSS:

.centered {
  height: 100px;
  width: 100%;
  background: red;
  position: absolute;
  top: 50%;
  left: 0;
  margin-top: -50px; /* half the height of the element */
}

You could use a number of techniques, depends on how you exactly want to implement it. 您可以使用多种技术,具体取决于您要如何实现它。 Some (older) but still relevant reading here . 一些(旧的),但仍然相关阅读这里

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

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