How to use vertical align in bootstrap

后端 未结 8 2177
北恋
北恋 2020-11-29 22:29

Simple problem: How do I vertically align a col within a col using bootstrap? Example here (I want to vertically align child1a and child1b):

http://bootply.com/7366

8条回答
  •  我在风中等你
    2020-11-29 22:36

    http://jsfiddle.net/b9chris/zN39r/

    HTML:

    This is some text.

    This is some more.

    CSS:

    div.item div h4 {
        height: 60px;
        vertical-align: middle;    
        display: table-cell;
    }
    

    Important notes:

    • The vertical-align: middle; display: table-cell; must be applied to a tag that has no Bootstrap classes applied; it cannot be a col-*, a row, etc.
    • This can't be done without this extra, possibly pointless tag in your HTML, unfortunately.
    • The backgrounds are unnecessary - they're just there for demo purposes. So, you don't need to apply any special rules to the row or col-* tags.
    • It is important to notice the inner tag does not stretch to 100% of the width of its parent; in our scenario this didn't matter but it may to you. If it does, you end up with something closer to some of the other answers here:

    http://jsfiddle.net/b9chris/zN39r/1/

    CSS:

    div.item div {
        background: #fdd;
        table-layout: fixed;
        display: table;
    }
    div.item div h4 {
        height: 60px;
        vertical-align: middle;    
        display: table-cell;
        background: #eee;
    }
    

    Notice the added table-layout and display properties on the col-* tags. This must be applied to the tag(s) that have col-* applied; it won't help on other tags.

提交回复
热议问题