首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当另一个元素居中对齐时,使用Flexbox将页脚与页面底部对齐

问当另一个元素居中对齐时,使用Flexbox将页脚与页面底部对齐
EN

Stack Overflow用户
提问于 2021-03-27 16:23:08
回答 2查看 131关注 0票数 1

我找不到如何使用flexbox将div元素垂直居中对齐,并将页脚元素与页面底部对齐。

HTML和CSS

​

代码语言:javascript
复制
.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;
}
代码语言:javascript
复制
<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的情况下使用几乎所有的选项-所有元素现在都水平和垂直居中,但是如何将页脚与底部对齐并将所有其他元素居中?

EN

回答 2

Stack Overflow用户

发布于 2021-03-27 17:00:48

​

代码语言:javascript
复制
<!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。

票数 1
EN

Stack Overflow用户

发布于 2021-03-27 17:35:14

​

代码语言:javascript
复制
*{
    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;
}
代码语言:javascript
复制
<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>

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66829343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档