简体   繁体   English

绝对定位

[英]Absolute positioning

I'm trying to understand the basics of positioning, and I came across this example on the internet that displays specific properties. 我试图了解定位的基本知识,并且在显示特定属性的互联网上遇到了这个示例。 Essentially, they created two boxes with CSS: 本质上,他们使用CSS创建了两个框:

#box_1 { 
width: 200px;
height: 200px;
background: #ee3e64;
}
#box_2 { 
position: absolute;
left: 100px;
width: 200px;
height: 200px;
background: #44accf;
}

Because absolute positioning places the container in relation to its first parent element, shouldn't the second box be placed at the top of the browser, and then left 100 pixels? 因为绝对定位使容器相对于其第一个父元素放置,所以不应该将第二个框放置在浏览器的顶部,然后保留100个像素吗? With this definition of absolute positioning, shouldn't the two boxes overlap? 用绝对定位的这个定义,两个盒子不应该重叠吗? Instead, the second box ends up below the first box. 而是,第二个框结束于第一个框的下方。 I thought that absolute positioning removes an object from the normal flow of a page? 我以为绝对定位会从页面的正常流程中删除对象? Can anyone explain this to me? 有人可以向我解释吗?

Here's what I'm talking about: http://jsfiddle.net/WScGM/ 这是我在说的: http : //jsfiddle.net/WScGM/

W3C says : W3C说

Absolutely positioned boxes are taken out of the normal flow. 绝对定位的盒子将从正常流程中取出。

and

The box's position ... is specified with the 'top', 'right', 'bottom', and 'left' properties 框的位置...由'top','right','bottom'和'left'属性指定

In your example, you haven't defined a value for "top" or "bottom" (the vertical position). 在您的示例中,您尚未为“顶部”或“底部”(垂直位置)定义值。 The initial value of top/left/right/bottom is "auto". 顶部/左侧/右侧/底部的初始值为“自动”。 So I believe this is the reason the element keeps its vertical position. 因此,我相信这是元素保持其垂直位置的原因。 The actually calculated styling would look like: 实际计算的样式如下所示:

position:absolute;
left:100px;
right:auto;
top:auto;
bottom:auto;

That means it behaves like if it was in the normal flow but it's 100px more right. 这意味着它的行为就像是在正常流程中一样,但它的正确性高了100px。 But if you put a third box without absolute positioning in the document, the absolute one will overlap it. 但是,如果您在文档中没有绝对位置的情况下放置了第三个框,则绝对框将与之重叠。

To position it 100px from the left upper edge, add 要将其距左上边缘100像素,请添加

top:0;

to your CSS. 到您的CSS。

Absolute positioning is a very powerful type of positioning that allows you to literally place any page element exactly where you want it. 绝对定位是一种非常强大的定位类型,它使您可以将任何页面元素确切地放置在所需的位置。

You can use the positioning attributes top, left bottom and right to set the location. 您可以使用顶部,左侧,底部和右侧的定位属性来设置位置。 Remember that these values will be relative to the next parent element with relative (or absolute) positioning. 请记住,这些值将相对于具有相对(或绝对)定位的下一个父元素。

If there is no such parent, it will default all the way back up to the element itself meaning it will be placed relatively to the page itself. 如果没有这样的父级,它将默认一直返回到元素本身,这意味着它将相对于页面本身放置。

The trade-off, and most important thing to remember, about absolute positioning is that these elements are removed from the flow of elements on the page. 关于绝对定位的权衡和要记住的最重要的事情是,这些元素已从页面上的元素流中删除。

An element with this type of positioning is not affected by other elements and it doesn't affect other elements. 具有这种定位类型的元素不会受到其他元素的影响,也不会影响其他元素。 This is a serious thing to consider every time you use absolute positioning. 每次您使用绝对定位时,都要考虑这一点。

Now comes your question: shouldn't the second box be placed at the top of the browser, and then left 100 pixels? 现在出现了您的问题: 第二个框是否不应该放在浏览器的顶部,然后再留100个像素? With this definition of absolute positioning, shouldn't the two boxes overlap? 用绝对定位的这个定义,两个盒子不应该重叠吗?

yes of course, by the definition of absolute positioning, the two boxes should overlap, if both are in absolute positioning. 是的,当然,根据绝对位置的定义,如果两个框都处于绝对位置,则两个框应重叠。

This is because by default one of them is in in static positioning. 这是因为默认情况下,其中之一处于静态位置。

Now I have made both in absolute position as following: 现在,我将两者都置于absolute位置,如下所示:

#box_1 { 
    width: 200px;
    height: 200px;
    background: #ee3e64;
    position:absolute;
}
#box_2 { 
    position: absolute;
    left: 100px;
    width: 200px;
    height: 200px;
    background: #44accf;
}

and so, now they are overlapping.See here: 因此,现在它们是重叠的。请参见此处:

FIDDLE 小提琴

您需要为第二个框添加“ top:0”以进行重叠。

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

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