简体   繁体   English

将div居中于左,右和绝对位置

[英]Centring a div with left, right, and absolute positioning

I have a div with position:absolute and I need to get it in the centre of the document. 我有一个带有position:absolute的div,我需要将其放在文档的中心。 What I thought would work is using left:50%; top:50% 我认为可行的方法是使用left:50%; top:50% left:50%; top:50% , but this moves the div 50% from the top and left according to its sides, so it's more to the right and bottom than I'd want. left:50%; top:50% ,但这会将div根据其侧面从顶部和左侧移动50%,因此它比我想要的更靠近右侧和底部。 Is there a way to move the div using the centre of it as the focus point? 有没有办法将div的中心作为焦点来移动div?

The height and width both have to be 300px and the div must always be in the centre of the document regardless of the resolution - which is why I can't use static values and have to use percentages 高度和宽度都必须为300px,并且div必须始终位于文档的中心,而不考虑分辨率,这就是为什么我不能使用静态值而必须使用百分比的原因

I didn't understood what you meant by but this moves the document 50% from the top and left so you should target the div and not the container element. 我不明白您的意思, 但这会将文档从顶部和左侧移出50%,因此您应该定位div而不是容器元素。

Demo 演示版

div {
    height: 100px;
    width: 100px;
    background: tomato;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -50px;
    margin-top: -50px;
}

Above am using all the properties which you are using, but apart from that am using margin-left and margin-top with negative values which are equivalent to 1/2 of the elements height and width 上面的代码使用了您正在使用的所有属性,但除此之外,还使用了带有负值的margin-leftmargin-top值,这些值等于元素heightwidth 1/2


Now that's the solution for a fixed width element, if your element width and height are variable, you will have to set your container element to display: table-cell; 现在这是固定width元素的解决方案,如果元素的widthheight是可变的,则必须将容器元素设置为display: table-cell; and than use vertical-align: middle; 然后使用vertical-align: middle; to align the element to vertically center, whereas margin: auto; 将元素对齐到垂直居中,而margin: auto; will take care of the horizontally center alignment. 将注意水平居中对齐。

Demo 演示版


Note: Am using display: table; 注意:正在使用display: table; for body which is kinda dirty way, so consider using a wrapper element for that and then assign display: table-cell; 对于有某种肮脏方式的body ,请考虑为此使用包装元素,然后分配display: table-cell; to that, also, make sure you have see all the parent element height to 100% 为此,还要确保已将所有父元素height100%

why not try this, its simpler and more general than messing with pixels: 为什么不试试呢,它比弄乱像素更简单,更通用:

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

}

Based on Mr.Alien's fiddle, i would suggest you to use calc for exact middle alignment for fixed dimension width 根据Alien先生的小提琴,我建议您使用calc进行固定尺寸宽度的精确中间对齐

The idea is to use calc(50% - 150px); 这个想法是使用calc(50% - 150px); ie, (50% - half the dimension of the block) (50%- 方块尺寸的一半)

This way, you can avoid the hacks of : 这样,您可以避免以下行为:

margin-left: -xxpx;
margin-top: -xxpx;

CSS 的CSS

div {
    height: 300px;
    width: 300px;
    background: tomato;
    position: absolute;
    top: calc(50% - 150px);
    top: -moz-calc(50% - 150px);
    top: -webkit-calc(50% - 150px);
    left: calc(50% - 150px);
    left: -moz-calc(50% - 150px);
    left: -webkit-calc(50% - 150px);
}

working fiddle 工作小提琴

user2211216 got what I wanted - just use user2211216得到了我想要的-只需使用

margin: auto;
top: 0; left: 0; bottom: 0; right: 0;
position: absolute;

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

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