简体   繁体   English

将中心 div 与 flex 对齐

[英]align center div with flex

I`ve been struggling with some css code, specifycally with flex.我一直在努力处理一些 css 代码,特别是 flex。 First i had to figure it out how to stick footer at bottom but now i cant center my divs.首先,我必须弄清楚如何将页脚粘贴在底部,但现在我无法将我的 div 居中。 Here is the html:这是 html:

<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta
         name="viewport"
         content="width=device-width, initial-scale=1.0"
         />
      <link rel="stylesheet" href="style.css" />
   </head>
   <div class="page-container">
      <div class="content-wrap">
         <div class="optional-content-wrap">
            <h1>Write, edit and run HTML, CSS and JavaScript code online.</h1>
            <p>
               Our HTML editor updates the webview automatically in real-time as
               you write code.
            </p>
         </div>
      </div>
      <div>Footer</div>
   </div>
</html>
</div>

and the css:和 css:

.page-container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.optional-content-wrap {
display: flex;
flex-direction: column;
align-items: center;
align-content: center;
}
.content-wrap {
flex: 1 1;
}

You want the children of the flex to be centered.您希望 flex 的子级居中。 So I set .content-wrap display to flex, and aligned items within.所以我将.content-wrap显示设置为 flex,并在其中对齐项目。 also text-align center.也是文本对齐中心。

 .page-container { display: flex; flex-direction: column; min-height: 100vh; }.content-wrap { flex: 1 1; }.content-wrap { display: flex; flex-direction: column; justify-content: center; text-align: center; }
 <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="page-container"> <div class="content-wrap"> <div class="optional-content-wrap"> <h1>Write, edit and run HTML, CSS and JavaScript code online.</h1> <p> Our HTML editor updates the webview automatically in real-time as you write code. </p> </div> </div> <div>Footer</div> </div> </body> </html>

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

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