Vertically centering a div in body?

后端 未结 5 2117
灰色年华
灰色年华 2020-12-14 02:26

I have tried to center this vertically in body (not horizontally). Also, I do not want to specify heights or anything like that. I tried adding a wrapper with a

5条回答
  •  半阙折子戏
    2020-12-14 03:10

    If you have flexbox available, you can do it without using display: table;

    Code example:

    html,
    body {
      height: 100%;
      width: 100%;
    }
    
    .container {
      align-items: center;
      display: flex;
      justify-content: center;
      height: 100%;
      width: 100%;
    }
    
      
        
    This div will be centered

    Ta-da! Vertically and horizontally centered content div. JSFiddle: https://jsfiddle.net/z0g0htm5/.

    Taken mostly from https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/ and https://css-tricks.com/snippets/css/a-guide-to-flexbox/

提交回复
热议问题