CSS Vertical align does not work with float

后端 未结 3 1624
轮回少年
轮回少年 2020-11-30 06:36

How can I use the vertical-align as well as float in the div properties? The vertical-align works fine if I do not use th

相关标签:
3条回答
  • 2020-11-30 06:44

    You need to set line-height.

    <div style="border: 1px solid red;">
    <span style="font-size: 38px; vertical-align:middle; float:left; line-height: 38px">Hejsan</span>
    <span style="font-size: 13px; vertical-align:middle; float:right; line-height: 38px">svejsan</span>
    <div style="clear: both;"></div>
    

    http://jsfiddle.net/VBR5J/

    0 讨论(0)
  • 2020-11-30 06:51

    Vertical alignment doesn't work with floated elements, indeed. That's because float lifts the element from the normal flow of the document. You might want to use other vertical aligning techniques, like the ones based on transform, display: table, absolute positioning, line-height, js (last resort maybe) or even the plain old html table (maybe the first choice if the content is actually tabular). You'll find that there's a heated debate on this issue.

    However, this is how you can vertically align YOUR 3 divs:

    .wrap{
        width: 500px;
        overflow:hidden;    
        background: pink;
    }
    
    .left {
        width: 150px;       
        margin-right: 10px;
        background: yellow;  
        display:inline-block;
        vertical-align: middle; 
    }
    
    .left2 {
        width: 150px;    
        margin-right: 10px;
        background: aqua; 
        display:inline-block;
        vertical-align: middle; 
    }
    
    .right{
        width: 150px;
        background: orange;
        display:inline-block;
        vertical-align: middle; 
    }
    

    Not sure why you needed both fixed width, display: inline-block and floating.

    0 讨论(0)
  • 2020-11-30 07:08

    Edited:

    The vertical-align CSS property specifies the vertical alignment of an inline, inline-block or table-cell element.

    Read this article for Understanding vertical-align

    0 讨论(0)
提交回复
热议问题