CSS vertically align floating divs

后端 未结 6 2079
终归单人心
终归单人心 2020-12-04 10:48

I have a div (#wrapper) containing 2 divs standing side by side.

I would like the right-div to be vertically aligned. I tried vertical-align:middle on my main wrapp

6条回答
  •  离开以前
    2020-12-04 11:10

    I realize this is an ancient question however I thought it would be useful to post a solution to the float vertical alignment issue.

    By creating a wrapper around the content you want floated, you can then use the ::after or ::before pseudo selectors to vertically align your content within the wrapper. You can adjust the size of that content all you want without it affecting the alignment. The only catch is that the wrapper must fill 100% height of its container.

    http://jsfiddle.net/jmdrury/J53SJ/

    HTML

    floated

    some text

    CSS

    div {
        border:1px solid red;
        height:100px;
        width:100%;
        vertical-align:middle;
        display:inline-block;
        box-sizing: border-box;
    }
    .floater {
        float:right;
        display:inline-block;
        height:100%;
        box-sizing: border-box;
    }
    .centered {
        border:1px solid blue;
        height: 30px;
        vertical-align:middle;
        display:inline-block;
        box-sizing: border-box;
    }
    h1 {
        margin:0;
        vertical-align:middle;
        display:inline-block;
        box-sizing: border-box;
    }
    .container:after, .floater:after, .centered:after, h1:after {
        height:100%;
        content:'';
        font-size:0;
        vertical-align:middle;
        display:inline-block;
        box-sizing: border-box;
    }
    

提交回复
热议问题