简体   繁体   English

如何使用页眉和页脚在页面中间水平和垂直对齐两个div?

[英]How to align two div horizontally and vertically in middle of page with header and footer?

I am designing a new page using Bootstrap 4. There are 4 elements in the page header, footer and two content boxes(divs). 我正在使用Bootstrap 4设计一个新页面。页面页眉,页脚和两个内容框(div)中包含4个元素。 Header and footer will remain at top and bottom respectively (using default bootstrap classes for them) but i want both content boxes should appear in middle of the page both horizontally and vertically regardless of the screen size. 页眉和页脚将分别保留在顶部和底部(使用默认的引导程序类),但无论屏幕大小如何,我都希望两个内容框都应水平和垂直出现在页面中间。 One content box beneath the another with space between them. 一个内容框位于另一个下方,并在它们之间留有空间。 You can refer attached picture for same. 您可以参考附带的图片。

I have tried using flex class of bootstrap but it didn't work out much. 我尝试使用bootstrap的flex类,但是效果不佳。 在此处输入图片说明

There's a lot of approaches to get this solution. 有很多方法可以获取此解决方案。 That's a simple one: 这很简单:

HTML 的HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <link
          rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
          crossorigin="anonymous"
        />
        <link rel="stylesheet" href="css/style.css" />
        <title>Document</title>
    </head>
    <body>
        <header class="container-fluid">HEADER</header>

        <main>
            <div class="center">
                <div id="container-1" class="">DIV 1</div>
                <div id="container-2" class="">DIV 2</div>
            </div>
        </main>
        <footer class="container-fluid">FOOTER</footer>
    </body>
</html>

CSS 的CSS

header {
    background-color: red;
}

body {
    margin-bottom: 60px; /* Margin bottom by footer height */
}

footer {
    background-color: green;
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 60px; /* Set the fixed height of the footer here */
    line-height: 60px; /* Vertically center the text there */
}

#container-1 {
    padding: 100px;
    margin-bottom: 150px;
    background-color: yellow;
}

#container-2 {
    padding: 100px;
    background-color: blue;
}

.center {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

Result 结果

Divs centered 以Divs为中心

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

相关问题 在页面中间水平和垂直居中div,页眉和页脚粘在页面的顶部和底部 - Horizontally and vertically center div in the middle of page with header and footer stuck to top and bottom of page 在页面中间水平和垂直居中div - Horizontally and vertically center div in the middle of page 如何居中并垂直对齐到页面内部的div? - How to center and vertically align to middle a div inside a page? 如何水平和垂直对齐固定位置的div? - How to align horizontally and vertically to a fixed positioned div? 如何在div中垂直和水平对齐img? - How to vertically AND horizontally align img inside div? 如何在bootstrap中div的中间水平对齐表单 - How to align the form horizontally in the middle of the div in bootstrap 在中心垂直和水平对齐DIV,而页眉和页脚不重叠 - Aligning a DIV Vertically and Horizontally in the center, without overlapping the header and footer 如何在响应式页面上的 div 内垂直和水平居中两个图像 - How to vertically and horizontally center two images inside a div on a responsive page 如何水平和垂直对齐两个项目 - How to align two items horizontally and vertically CSS 问题:如何对齐<div>元素(<div> 、页眉、页脚和正文)垂直与响应式网页设计? - CSS issue: How to align <div> elements (<div>, header, footer, and body) vertically with responsive web design?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM