简体   繁体   English

将div中的每个元素垂直居中

[英]Vertically center every element inside div

Here is example demo of my problem 这是我的问题的示例演示

http://www.bootply.com/tkrs6G3GlG http://www.bootply.com/tkrs6G3GlG

Basicly, I've got on the left form groups, and in each I've got large text giving some information and then asking user to select something (in example is only radio, but there are dropdown's and checkboxes as well). 基本上,我在左侧的表单组中,每个表单中都有大文本,提供一些信息,然后要求用户选择某些内容(例如,仅是单选按钮,但还包含下拉菜单和复选框)。 I would like that for every form group, options on the right are vertically align (in the middle) depending on text size. 我希望每个表单组的右边选项根据文本大小垂直对齐(在中间)。 In example above, radio's are on top, but I would like them in the middle of corresponding form-group. 在上面的示例中,收音机位于最上方,但我希望它们位于相应表单组的中间。

I've tried 我试过了

  .vcenter{vertical-align:middle;
     }

but it's not working. 但它不起作用。 I've also tried setting height of div which contains options 我也尝试设置包含选项的div的高度

 .maxHeight{
       height:100%;
 }

but div is not getting larger. 但div不会变大。 I tried using large height and hide overflow, but not working. 我尝试使用较大的高度并隐藏溢出,但无法正常工作。 Only thing that worked so far is using 到目前为止唯一有效的方法是使用

.vcenter{
      position:absolute;
      vertical-align: middle;
 }

but it only looks well on large screen, but after screen resizing, it's overlaping with text. 但它只能在大屏幕上看起来不错,但是在调整屏幕大小后,它与文本重叠。

If your div is inside another div,try 如果您的div在另一个div内,请尝试

#my_div{
       margin-left: auto;
       margin-right: auto; 
       width: /*put_your_width*/;
}

And if it is the parent div 如果它是parent div

   #my_div{
         display: table;
         margin-right: auto;
         margin-left: auto;
   }

There are a few ways to do this. 有几种方法可以做到这一点。 I usually use this method... 我通常使用这种方法

HTML 的HTML

<div> <p>Center this text</p> </div>

CSS 的CSS

div{    
    position: relative;
    height: 500px;
    width: 500px;
    background: green;
    -webkit-transform-style: preserve-3d;
    -moz-transform-style: preserve-3d;
    transform-style: preserve-3d;
p{
    position: absolute;
    top:50%;
    text-align:center;
    display:inline-block;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}

The way I prefer to do it is add margin: auto; 我更喜欢这样做的方法是增加margin: auto; and top, left, right, and bottom 0 like so: 以及顶部,左侧,右侧和底部0,如下所示:

.parent {
    position: relative;
}

.child {
    margin: auto;
    position: absolute;
        top: 0; right: 0; bottom: 0; left: 0;
}

you can use many aproaches. 您可以使用许多方法。

first one is with position 第一个是位置

    .parent_div{
          position:relative;
          width:500px;
          height:500px;
    }
    .children_div{
          position:absolute;
          width:100px;
          height:100px;
          left:0px;
          right:0px;
          top:0px;
          bottom:0px;
          margin:auto;
    }

-But you need to have specified the height and width of the child element.. so it is not good for fluid design. -但是您需要指定子元素的高度和宽度。因此,这对流体设计不利。

Sec. aproach is the best i think it uses display :) 方式是最好的,我认为它使用显示:)

    .parent_div{
          display:table;
          width:500px;
          height:500px;
          text-align:center;
    }
    .children_div{
          display:table-cell;
          width:100%;
          height:100%;
          vertical-align:middle;

    }

    .children_children_div{
          /*What ever... this div will be centered verticaly*/

    }

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

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