简体   繁体   English

DIV的CSS位置(相对位置的绝对值)

[英]CSS positioning of DIVs (absolute within relative)

In class we learned that if I have two divs: a wrapper div (let's call it div A ) that's defined as position: relative; 在课堂上,我们了解到如果我有两个div:包装器div(我们将其称为div A ),其定义为position: relative; and another div, div B that's inside div A with position: absolute; 另一个div,div B位于div Aposition: absolute; .

What will happen is that now the position of div B is dependent on the position of div A . 将会发生的是,现在div B的位置取决于div A的位置。 Meaning that now the point 0,0 of div B is not the point 0,0 of the browser, but of div A . 这意味着现在div B的点0,0不是浏览器的0,0点,而是div A So, if I was to move div A say 20 pixels to the right, and div B 30 pixels to the right, div B would be 50 pixels to the right of the browser's point 0,0; 所以,如果我是移动的div A说20个像素的权利,和DIV B 30像素的权利,DIV B将50个像素到浏览器的点0,0的权利;

Now, my question is: what if I have 3 divs. 现在,我的问题是:如果我有3个div,该怎么办? Div A that's position: relative; 部门A的position: relative; , within it div B that's position: absolute , and within div B , another div (div C ) with position: absolute; ,它在div B中的position: absolute ,在div B ,是另一个div(div C ),其position: absolute; . Now, how will div C behave? 现在,div C的表现如何? Will its position 0,0 be that of div A or div B ? 它的位置0,0是div A还是div B

Thanks in advance. 提前致谢。

code: 码:

<style type = "text/css">
#a {
position: relative;
left: 20px;
}

#b {
position:absolute;
left: 20px
}

#c {
left: 20px
position:absolute;
}
</style>
<div id = "a">
    <div id = "b">
        <div id = "c">
        test
        </div>
    </div>
</div>

从该JSFiddle中可以看到,“ C” div的位置相对于其父“ B”具有

position: absolute;

I don't have much to add to both these great answers, but here's an article that helped me understand the concept. 在这两个很好的答案中,我没有太多要添加的内容,但这是一篇帮助我理解这一概念的文章。 http://alistapart.com/article/css-positioning-101 http://alistapart.com/article/css-positioning-101

EDIT: 编辑:

That article covers that a div with position absolute is positioned on the inner grid of its closest ancestor(parent, grandparent, etc.) that is fixed, relative, or absolute. 该文章涵盖了具有绝对位置的div位于其最接近祖先(父代,祖父母等)的内部网格上,该祖先是固定的,相对的或绝对的。 If there is none it is relative to the html document, but note it still behaves differently than fixed. 如果没有,则相对于html文档,但请注意,它的行为与固定的有所不同。 It also covers the key differences between the three position types as well as the static position type. 它还涵盖了三种位置类型以及静态位置类型之间的主要区别。

static - this is the default position it creates no grids for children absolute positioned divs. 静态 -这是默认位置,它不为绝对位置的子级div子级创建任何网格。 You can't use the css properties top left right or bottom . 您不能使用的CSS属性top left rightbottom

relative - creates a grid for descendent(children, grandchildren, etc.) absolute positioned divs. relative-为绝对位置div的子代(子代,孙子代等)创建一个网格。 You can use top, left, right and bottom, but they move it 'relative' to where it was previously at. 您可以使用顶部,左侧,右侧和底部,但它们会将其“相对”移动到之前的位置。 When using top, left, right, and bottom other elements still go around where it was previously at. 使用顶部,左侧,右侧和底部时,其他元素仍会绕过其原来的位置。

absolute - creates a grid for descendent(children, grandchildren, etc.) absolute positioned divs. absolute-为后代(子代,孙代等)的绝对位置的div创建一个网格。 You can use top, left, right and bottom, but they move it relative to the closest ancestor(parent, grandparent, etc.) grid. 您可以使用顶部,左侧,右侧和底部,但是它们相对于最接近的祖先(父,祖父母等)网格进行移动。 Remember the grids are created by fixed, absolute, and relative elements. 请记住,网格是由固定,绝对和相对元素创建的。 When using top, left, right, and bottom the element goes out of the flow of the document. 当使用顶部,左侧,右侧和底部时,该元素将从文档流中消失。 (this means other items go through it.) (这意味着其他项目也会通过。)

fixed - creates a grid for children absolute positioned divs. 固定 -为绝对定位的div子级创建网格。 You can use top, left, right and bottom but they move it relative to the browser. 您可以使用顶部,左侧,右侧和底部,但是它们相对于浏览器进行移动。 When using top, left, right and bottom goes out of the flow of the document. 使用上,下,左,右和下时会超出文档流。 (this means other items go through it.) (这意味着其他项目也会通过。)

Div B-任何绝对定位的元素都根据其最接近的位置(绝对,相对或固定)parent定位

这是个人喜好问题,但是与AListApart相比,这篇文章为我带来的好处更多: http ://learnlayout.com/position.html

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

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