简体   繁体   English

CSS居中对齐div垂直

[英]CSS center align div vertically

I want to center align a div veritcally how can I do this?我想垂直居中对齐 div 我该怎么做?

<!DOCTYPE html>
<html>
<head>
    <title>HTML/CSS</title>
    <style type="text/css">
        #box {
            display: flex;
            width: 200px;
            height: 120px;
            background: red;
            margin: auto;
            vertical-align: center;
        }
    </style>
</head>
<body>
    <div id="box">Hello World!</div>
</body>
</html> 

I referred to this tutorial but no gain.我参考了这个教程,但没有收获。

DEMO演示

It's all about the container一切都与容器有关

.container{

  display: flex;

  height: 100%;
  border: 1px solid red;
  justify-content: center;
  align-items: center;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM