我找不到如何使用flexbox将div元素垂直居中对齐,并将页脚元素与页面底部对齐。
HTML和CSS
.section {
height: 100vh;
display: flex;
align-items: center;
}
.container {
width: 100%;
max-width: 1400px;
padding: 0 15px;
margin: 0 auto;
}
.section__header {
text-align: center;
}
.footer {
margin: auto;
max-width: 848px;
}<section class="section contact">
<div class="container">
<div class="section__header">...</div>
<div class="contact__form">...</div>
<footer class="footer"></footer>
</div>
</section>
我尝试在没有success...all的情况下使用几乎所有的选项-所有元素现在都水平和垂直居中,但是如何将页脚与底部对齐并将所有其他元素居中?
发布于 2021-03-27 17:00:48
<!DOCTYPE html>
<html lang="en">
<head>
<!-- ========== Meta Tags ========== -->
<meta charset="UTF-8">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- ========== Title ========== -->
<title>stack</title>
<style type="text/css">
*,
*:before,
*:after {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
-ms-box-sizing: border-box;
}
.section {
height: 100vh;
width: 100%;
background: green;
}
.container {
width: 100%;
height: 100%;
/*max-width: 1400px;*/
padding: 0 15px;
/*margin: 0 auto;*/
display: flex;
flex-direction: column;
justify-content: space-between;
background: blue;
}
.contentContainer {
height: 80%;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
background: green;
}
.section__header {
/*text-align: center;*/
background: grey;
}
.contact__form {
background: violet;
}
.footer {
height: 20%;
width: 100%;
background: yellow;
/*margin: auto;*/
/*max-width: 848px;*/
}
</style>
</head>
<body>
<section class="section">
<div class="container">
<div class="contentContainer">
<div class="section__header">header</div>
<div class="contact__form">form</div>
</div>
<footer class="footer">footer</footer>
</div>
</section>
</body>
</html>
为内容创建一个单独的容器,并使用space-between。
发布于 2021-03-27 17:35:14
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.section {
display: flex;
height: 100vh;
justify-content: space-between;
flex-direction:column;
border:1px solid sienna;
}
.container {
display:flex;
justify-content:center;
flex-direction:column;
width: 100%;
max-width: 1400px;
padding: 0 15px;
margin: 0 auto;
border:1px solid black;
}
.section__header {
text-align: center;
border:1px solid maroon;
}
.contact__form{
border:1px solid blue;
}
.footer {
border:1px solid red;
}<section class="section contact">
<div class="container">
<div class="section__header">header</div>
<div class="contact__form">contact</div>
</div>
<footer class="footer">footer</footer>
</section>
https://stackoverflow.com/questions/66829343
复制相似问题