简体   繁体   English

如何将3个div对齐到页面的顶部中间和底部?

[英]How to align 3 divs to the top middle and bottom of a page?

I need to align 3 divs to the top, middle and bottom of a webpage Below is my current code. 我需要将3个div对齐到网页的顶部,中间和底部以下是我当前的代码。

JS Fiddle JS小提琴

<div class="container">
    <div class="logo">
        My Logo
    </div>
    <div class="nav">
        My Nav
    </div>
    <div class="base">
       Base
    </div>   
</div>

I have tried using table/table-cell method but each section then sits side by side. 我尝试过使用table / table-cell方法,但每个部分并排放置。

The container needs to be fixed and the nav should be vertically aligned. 需要修复容器,导航应垂直对齐。

Any ideas to make is so the logo div is at the top, the nav div is in the middle and the base div is at the bottom? 任何想法都是这样,logo div位于顶部,nav div位于中间,base div位于底部?

You could do it with CSS table, I added a <div> to each block. 你可以用CSS表做,我为每个块添加了一个<div>

jsfiddle 的jsfiddle

 html, body { height: 100%; margin: 0; } .container { background: gold; width: 100%; height: 100%; display: table; } .container .row { display: table-row; } .container .cell { display: table-cell; } .logo .cell { vertical-align: top; } .nav .cell { vertical-align: middle; } .base .cell { vertical-align: bottom; } 
 <div class="container"> <div class="row logo"> <div class="cell">My Logo</div> </div> <div class="row nav"> <div class="cell">My Nav</div> </div> <div class="row base"> <div class="cell">Base</div> </div> </div> 

You could also make it with flexbox layout. 您也可以使用flexbox布局。

jsFiddle 的jsfiddle

 body { margin: 0; } .container { background: gold; height: 100vh; display: flex; flex-direction: column; justify-content: space-between; } 
 <div class="container"> <div class="row logo">Logo</div> <div class="row nav">Nav</div> <div class="row base">Base</div> </div> 

I have created your solution I believe. 我相信我创造了你的解决方案。

HTML: HTML:

<div class="container">
<div class="logo">
    My Logo
</div>
<div class="nav">
    My Nav
</div>
<div class="base">
    Base
</div>   

CSS: CSS:

.container{
    width: 200px;
    height: 100%;
    background: gold;
    position: fixed;
}

.logo{
    background: blue;
    width:100%;
}

.nav{
    background: tan;
    width: 200px;
    position: fixed;
    top: 50%;
    transform: translateY(-50%); /*nudge the div back to the center*/
}

.base{
    background: aqua;
    width:200px;
    position: fixed;
    bottom: 0;
}

JSFiddle view of the output JSFiddle输出视图

I created fixed positions for the base and nav to give vertical alignment. 我为基础和导航创建了固定位置以进行垂直对齐。

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

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