简体   繁体   English

如何使用CSS或CSS + jquery在页面上垂直和水平对齐div容器?

[英]How do I align a div container both vertically and horizontally on a page using CSS or CSS + jquery?

I'm sure this has been asked before, but all the questions I can find are far too complex. 我敢肯定这已经被问过了,但是我能找到的所有问题都太复杂了。 Really simply, how do I align a div container to the center and middle of a page both horizontally and vertically, so that it also stays there when the page is resized as well. 简而言之,如何将div容器水平和垂直地对齐到页面的中心和中间,以便在调整页面大小时也可以将其保持在该位置。

All I need is a blank browser window with a div that stays in the center of the screen when the window is resized, no matter what. 我需要的是一个空白的浏览器窗口,该窗口的div大小无论如何都将保持在屏幕中央。 Horizontal align is quite easy using margin:auto but it's the vertical align that seems to be troublesome. 使用margin:auto可以很容易地进行水平对齐,但是看起来似乎是麻烦的是垂直对齐。

A lot of 'solutions' require that there first be a set height that the div is floating in. I'm sure there's got to be a simpler way... 许多“解决方案”都要求首先将div浮动到一个设定的高度。我敢肯定必须有一种更简单的方法...

There are definitely a lot of way you can achieve this. 绝对有很多方法可以实现这一目标。 Here is one of the way. 这是一种方法。 I am using position:absolute . 我正在使用position:absolute

 .center
{
  position: absolute;
  top:0;
  right:0;
  bottom:0;
  left:0;
  margin:auto;
  border:5px solid black;
  width:100px;
  height:100px;
} 

DEMO 演示

The flexbox way: flexbox方式:

FIDDLE (Resize browser window)) FIDDLE (调整浏览器窗口大小)

Markup 标记

<div class="container">
    <div class="child"></div>
</div>

CSS 的CSS

.container {
    width: 80vw;
    height: 80vh;
    display:flex;
    justify-content: center; /* align horizontal */
    align-items: center; /* align vertical */
    background: aqua;
}
.child {
    width: 50%;
    height: 50%;
    background: crimson;
}

You can use display: table-cell and vertical-align: middle to achieve this. 您可以使用display: table-cellvertical-align: middle实现此目的。

HTML: HTML:

<div class="container">
    <div class="content">
        blah blah
    </div>
</div>

CSS: CSS:

.container {
    display: table-cell;
    height: 100vh;
    text-align: center;
    vertical-align: middle;
    width: 100vw;
}

.content {
    display: inline-block;
}

Here's a fiddle . 这是一个小提琴

there's no 1 line css way to centered div vertically, so you need a workaround, try this simplest one. 没有1行CSS方式可以垂直居中div,因此您需要一种解决方法,尝试这种最简单的方法。

body {
   position:relative;
}
div {
   position:absolute;
   top: 50%;
}

with jquery 用jQuery

$(document).ready(function(){
   $('div').css('margin-top', -$(this).height()/2);
});

note: the margin top of the div should be minus half of its height, so in this example, its -50px because its height is 100px. 注意:div的边距顶部应为其高度的一半减去,因此在此示例中,其-50px的高度是100px。

set margin: 0 auto; 设置边距:0自动; to the container for horizontal alignment.For vertical adjustment you can use margin-top property.If you require a vertical align than adjustment you can use javascript or jquery. 到容器以进行水平对齐。要进行垂直调整,可以使用margin-top属性。如果需要垂直对齐而不是调整,则可以使用javascript或jquery。 First find the height of the browser window using jquery.Then adjust the margin-top(In a ratio) css property as the window height is changed.You can set an event for detecting the changes of window size. 首先使用jquery查找浏览器窗口的高度,然后随着窗口高度的变化调整margin-top(以比例)css属性。您可以设置一个事件来检测窗口大小的变化。

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

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