Centering 2 Divs inside another vertically

前端 未结 2 788
挽巷
挽巷 2021-02-14 00:45

I have 2 divs that I want to centre vertically inside another div. At the moment I have:

http://jsfiddle.net/5vpA3/1/

Now I understand what is going on here, but

相关标签:
2条回答
  • 2021-02-14 01:01

    Flexbox solution to center vertically:

    #container {
        display: flex;
        align-items: center;
    }
    

    OP's original fiddle modified: http://jsfiddle.net/o3pmyb8c/

    If you would like to also center horizontally you can add justify-content: center;

    0 讨论(0)
  • 2021-02-14 01:14

    Live Demo

    • Remove float: left from #left and #right.
    • Instead, use display: inline-block:

      #left, #right {
          display: inline-block;
          vertical-align: middle;
      }
      
    • Due to using display: inline-block, you have to deal with the whitespace issue. I chose to remove the whitespace in the HTML between </div> and <div id="right">. See here for what happens if you don't do that. Removing the whitespace really is the easiest fix, but there are other ways.
    0 讨论(0)
提交回复
热议问题