简体   繁体   English

垂直居中对齐容器div

[英]Vertically center align container div

I'm creating a toolbar style navigation menu, where the content is added dynamically into the container. 我正在创建一个工具栏样式的导航菜单,其中的内容被动态添加到容器中。

I want to align the container div's center vertically to the center of the screen. 我想将容器div的中心垂直对齐到屏幕的中心。

Here is my approach http://cssdeck.com/labs/cmwvyjud 这是我的方法http://cssdeck.com/labs/cmwvyjud

I know that this is not how it should be, but I'm unable to find alternative ways of doing this vertical alignment. 我知道这不是应该的方式,但是我找不到其他方法来进行这种垂直对齐。 Any help is appreciated. 任何帮助表示赞赏。

If you know the height of the content that needs to be centered you can take half of that height and use something like margin-top:-(contentHeight/2)px 如果您知道需要居中放置的内容的高度,则可以取一半的高度,然后使用margin-top:-(contentHeight/2)px

See example at : http://cssdeck.com/labs/atwispr6 , here I know the content is 300px, so when I take half I have 150px. 参见示例: http : //cssdeck.com/labs/atwispr6 ,这里我知道内容是300px,所以当我取一半时,我有150px。 So margin-top:-150px 所以margin-top:-150px

Edit I have updated the example, to make it dynamic 编辑我已经更新了示例,使其变得动态

    $(function(){
        var $container = $("#container")    
        $container.css("margin-top", "-" + ($container.height()/2) + "px");
    });

If you're looking for a pure CSS solution, you have 2 options. 如果您正在寻找纯CSS解决方案,则有两种选择。 If you're willing to add an additional container, you could use display: table . 如果您愿意添加其他容器,则可以使用display: table If the markup cannot change, then Flexbox is what you're looking for. 如果标记无法更改,那么您要寻找的是Flexbox。

Display: table 显示:表

http://cssdeck.com/labs/jdzltkla http://cssdeck.com/labs/jdzltkla

body{
    background-color:black;
}

#container{
    position: fixed;
    display: table;
    top: 0;
    bottom: 0;
    left: 10px;
}

.foo {
    display: table-cell;
    vertical-align: middle;
}

#container .foo > * {
    width: 50px;
    height: 50px;
    background-color:white;
    margin-bottom: 10px;
}

<div id="container">
    <div class="foo">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</div>

This works in just about everything, including IE8+ 这适用于几乎所有内容,包括IE8 +

Flexbox 弹性盒

http://codepen.io/cimmanon/pen/sKqkE http://codepen.io/cimmanon/pen/sKqkE

body {
  background-color: black;
}

#container {
  position: fixed;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-orient: vertical;
  -moz-box-orient: vertical;
  -webkit-box-direction: normal;
  -moz-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -moz-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  top: 0;
  bottom: 0;
  left: 10px;
}

#container * {
  width: 50px;
  height: 50px;
  background-color: white;
  margin-bottom: 10px;
}

This works in Chrome, Opera, Firefox, IE10, and Safari. 这适用于Chrome,Opera,Firefox,IE10和Safari。

the content is 300px, screen height is (for Ex 768px), so you must compare your content with screen height, not content itself... 内容为300像素,屏幕高度为(例如768像素),因此您必须将内容与屏幕高度进行比较,而不是内容本身...

so content is about 40% of screen height, and you can use top:25%; 因此内容约为屏幕高度的40%,您可以使用top:25%; for top.. 为顶部..

or in jQuery : 或在jQuery中:

var container_height = parseInt($("#container").css("height"));
$("#container").css("top", (parseInt((screen.height-container_height)/2))+"px");

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

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