如何垂直居中对齐位置:相对元素

时间:2021-10-05 20:30:50

I am trying out some basic CSS today and I stumbled across a problem. I hope somebody could help me with it.

我今天正在尝试一些基本的CSS,我偶然发现了一个问题。我希望有人可以帮助我。

How do I vertically center align the parent container to the canvas which has a position:relative to it? The parent container has a child element with position:absolute to it. The child element has been positioned to the center of the parent container.

如何将父容器垂直居中对齐到具有位置的画布:相对于它?父容器有一个子元素,其position:absolute。子元素已定位到父容器的中心。

I have created a JSFiddle for this. http://jsfiddle.net/exmRf/

我为此创建了一个JSFiddle。 http://jsfiddle.net/exmRf/

Cheers, Venn.

2 个解决方案

#1


13  

One solution is to wrap your .container with two wrappers; give the first one display: table; and height: 100%; width: 100%; and the second display: table-cell; and vertical-align: middle;. Also make sure your body and html have full height.

一种解决方案是用两个包装器包装你的.container;给第一个显示:table;和身高:100%;宽度:100%;第二个显示:table-cell;和vertical-align:middle;。还要确保你的身体和html有完整的高度。

Here's a little working demo: little link.

这是一个小工作演示:小链接。

Another method is to apply top: 50%; to your .container and margin-top: -150px; (300px / 2 = 150px). (Note that this method requires you to know the exact height of your container, so it might not be exactly what you want, but it might as well be!). A little working demo of this latter method: another little link.

另一种方法是应用top:50%;到你的.container和margin-top:-150px; (300px / 2 = 150px)。 (请注意,此方法要求您知道容器的确切高度,因此它可能不是您想要的,但也可能是!)。后一种方法的一个小工作演示:另一个小链接。

I hope that helped!

我希望有所帮助!

#2


-1  

Demo

Give to your item left 50% and top 50% and margin-left half of width or margin-top half of top

给你的物品留下50%和前50%和左边缘的宽度或边缘 - 上半部分

Now used to this css

现在习惯了这个css

.container{
    position: relative;
    width: 300px;
    height: 300px;
    background-color: red;
    margin:auto;
}

.item{
    position: absolute;
    width: 100px;
    height: 100px;
    background-color: blue;
    left:50%;
    top:50%;
    margin-left:-50px;
    margin-top:-50px;
}

Demo

if you used don't position than used to this http://jsfiddle.net/exmRf/16/

如果你使用的位置不比习惯这个http://jsfiddle.net/exmRf/16/

#1


13  

One solution is to wrap your .container with two wrappers; give the first one display: table; and height: 100%; width: 100%; and the second display: table-cell; and vertical-align: middle;. Also make sure your body and html have full height.

一种解决方案是用两个包装器包装你的.container;给第一个显示:table;和身高:100%;宽度:100%;第二个显示:table-cell;和vertical-align:middle;。还要确保你的身体和html有完整的高度。

Here's a little working demo: little link.

这是一个小工作演示:小链接。

Another method is to apply top: 50%; to your .container and margin-top: -150px; (300px / 2 = 150px). (Note that this method requires you to know the exact height of your container, so it might not be exactly what you want, but it might as well be!). A little working demo of this latter method: another little link.

另一种方法是应用top:50%;到你的.container和margin-top:-150px; (300px / 2 = 150px)。 (请注意,此方法要求您知道容器的确切高度,因此它可能不是您想要的,但也可能是!)。后一种方法的一个小工作演示:另一个小链接。

I hope that helped!

我希望有所帮助!

#2


-1  

Demo

Give to your item left 50% and top 50% and margin-left half of width or margin-top half of top

给你的物品留下50%和前50%和左边缘的宽度或边缘 - 上半部分

Now used to this css

现在习惯了这个css

.container{
    position: relative;
    width: 300px;
    height: 300px;
    background-color: red;
    margin:auto;
}

.item{
    position: absolute;
    width: 100px;
    height: 100px;
    background-color: blue;
    left:50%;
    top:50%;
    margin-left:-50px;
    margin-top:-50px;
}

Demo

if you used don't position than used to this http://jsfiddle.net/exmRf/16/

如果你使用的位置不比习惯这个http://jsfiddle.net/exmRf/16/