简体   繁体   English

如何在CSS中补偿左,上,下,右

[英]How to compensate for left,top,bottom, right in CSS

I've been looking very hard and I see many examples where people will have left or top or something like that at 50%, and I'd expect that would be sufficient to center anything, but that's not the case. 我一直在非常努力地工作,我看到许多例子,人们将以50%的比例离开或离开顶部或类似位置,我希望这足以使任何内容居中,但事实并非如此。

Sometimes when I put left 50% for something, the div looks as if it's slightly more than that (relative to browser). 有时,当我将50%留给某些东西时,div看起来好像比那稍微多一点(相对于浏览器)。 So then they have negative margins and I'm just wondering how do you know what values to put in order to center the elements, and what property for position would I need to put? 因此,它们的边距为负,我想知道您如何知道要放置哪些值以使元素居中,以及我需要放置什么位置属性? I just don't understand why position:relative and left:50% won't make my div go to the center of the page. 我只是不明白为什么position:relative和left:50%不会使我的div转到页面的中心。

When absolute positioning an element using top , right , bottom and left you're moving it a certain distance from that elements edges. 当使用toprightbottomleft绝对定位元素时,您要将其从该元素的边缘移开一定距离。 It will move it in relation to the last positioned element. 它将相对于最后定位的元素移动它。 The last position element is the next ancestor element that has any type of positioning applied to it via CSS. 最后一个position元素是下一个祖先元素,该元素通过CSS应用于任何位置类型。 If no ancestor element as positioning set then the viewport window will be used as a reference. 如果没有祖先元素作为定位集,则视口窗口将用作参考。

I created a quick diagram to show what is going on. 我创建了一个快速图表来显示正在发生的事情。

在此处输入图片说明

left: 50%; moves the element's left edge 50% (half) of the width of the last positioned element's left edge. 将元素的左边缘移动到最后定位的元素的左边缘宽度的50%(一半)。 You're effectively moving elements to the right by adding space between element left edges. 通过在元素左边缘之间添加空间,可以有效地将元素向右移动。

margin-left: <negative value>; is set to half the element's width pulling it back to the left. 设置为元素宽度的一半,将其向左拉。 This fixes the off center issue you're seeing. 这样可以解决您看到的偏心问题。

Today a lot of people will forgo using margin-left with a negative value and opt for transform: translateX( -50% ); 今天,许多人将放弃使用负值的margin-left并选择进行transform: translateX( -50% ); . This allows them to be more flexible as the elements width does not need to be known. 由于不需要知道元素的宽度,因此可以使它们更加灵活。

The CSS for transform: translateX( -50% ); transform: translateX( -50% );的CSS transform: translateX( -50% ); might look like this: 可能看起来像这样:

div {
  position: absolute;
  left: 50%;
  border: 2px dashed red;
  width: 200px;
  height: 100px;
  transform: translateX( -50% );
}

Demo JSFiddle . 演示JSFiddle

If you're looking to simply center something horizontally and you have applied a width ( px , % , etc. work) then you can use margin: 0 auto; width: <width value>; 如果您只是想将某些东西水平放置在中心,并且已经应用​​了宽度( px%等等),那么可以使用margin: 0 auto; width: <width value>; margin: 0 auto; width: <width value>; . A width must be set for margin: 0 auto; 必须为margin: 0 auto;设置宽度margin: 0 auto; to work! 上班!

Example: 例:

div {
    margin: 0 auto;
    width: 25%;
    height: 100px;
    border: 2px dashed red;
}

Demo JSFiddle . 演示JSFiddle

Sometimes when I put left 50% for something, the div looks as if it's slightly more than that(relative to browser). 有时,当我将50%留给某物时,div看上去似乎比那略大(相对于浏览器)。

It positions the left edge at the 50% point. 它将左边缘定位在50%点。

So then they have negative margins and I'm just wondering how do you know what values to put in order to center the elements 所以它们的边距为负,我想知道如何知道要放置哪些值以使元素居中

Half of whatever the width is width一半

what property for position would I need to put? 我需要放置什么财产?

That technique generally assumes absolute positioning 该技术通常假定absolute定位


I just don't understand why position:relative and left:50% won't make my div go to the center of the page. 我只是不明白为什么position:relative和left:50%不会使我的div转到页面的中心。

Relative positioning offsets the element from its static position. 相对定位使元素偏离其静态位置。 Generally, any time you want to try to centre something with relative positioning you would be better off with setting the left and right margins to auto (and leaving the positioning as static ). 通常,任何时候您想要尝试以相对定位来使某内容居中时,最好将左右边距设置为auto (并将定位保持为static )。

That's because it positions the top-left corner at 50%. 那是因为它将左上角定位在50%。 You should use the translate : 您应该使用translate

.centered {
    position: absolute; 
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    // this moves the center of centered item to 50%
}

When you apply left: 50% its in fact the left edge of your element which is centered. 当您向左应用时:实际上是元素的左边缘居中的50%。 Not the middle of your element. 不在元素的中间。 apply margin:auto; 套用保证金:自动; to your element to center it. 使您的元素居中。

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

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