相对于其容器定位元素

我正在尝试使用HTML和CSS创建一个水平的100%堆积条形图。我想使用DIVs创建条形,其背景颜色和百分比宽度取决于我想要绘制的值。我还想有一个网格线,以标记沿图形的任意位置。

在我的实验中,我已经通过指定CSS属性__abc0来使条形水平堆叠。然而,我想避免这种情况,因为它似乎真的以令人困惑的方式扰乱了布局。此外,当杆浮动时,网格线似乎不能很好地工作。

我认为CSS定位应该能够处理这个问题,但我还不知道如何做到这一点。我希望能够指定几个元素相对于其容器左上角的位置。我经常遇到这类问题(甚至在这个特定的图形项目之外),所以我想要一个方法:

  1. 跨浏览器(理想情况下没有太多的浏览器黑客)
  2. 在特殊模式下运行
  3. 尽可能清晰/干净,以便于定制
  4. 如果可能的话,不使用JavaScript.
247869 次浏览

You have to explicitly set the position of the parent container along with the position of the child container. The typical way to do that is something like this:

div.parent{
position: relative;
left: 0px;  /* stick it wherever it was positioned by default */
top: 0px;
}


div.child{
position: absolute;
left: 10px;
top: 10px;
}

Absolute positioning positions an element relative to its nearest positioned ancestor. So put position: relative on the container, then for child elements, top and left will be relative to the top-left of the container so long as the child elements have position: absolute. More information is available in the CSS 2.1 specification.

You are right that CSS positioning is the way to go. Here's a quick run down:

position: relative will layout an element relative to itself. In other words, the elements is laid out in normal flow, then it is removed from normal flow and offset by whatever values you have specified (top, right, bottom, left). It's important to note that because it's removed from flow, other elements around it will not shift with it (use negative margins instead if you want this behaviour).

However, you're most likely interested in position: absolute which will position an element relative to a container. By default, the container is the browser window, but if a parent element either has position: relative or position: absolute set on it, then it will act as the parent for positioning coordinates for its children.

To demonstrate:

#container {
position: relative;
border: 1px solid red;
height: 100px;
}


#box {
position: absolute;
top: 50px;
left: 20px;
}
<div id="container">
<div id="box">absolute</div>
</div>

In that example, the top left corner of #box would be 100px down and 50px left of the top left corner of #container. If #container did not have position: relative set, the coordinates of #box would be relative to the top left corner of the browser view port.

I know I am late but hope this helps.

Following are the values for the position property.

  • static
  • fixed
  • relative
  • absolute

position : static

This is default. It means the element will occur at a position that it normally would.

#myelem {
position : static;
}

position : fixed

This will set the position of an element with respect to the browser window (viewport). A fixed positioned element will remain in its position even when the page scrolls.

(Ideal if you want scroll-to-top button at the bottom right corner of the page).

#myelem {
position : fixed;
bottom : 30px;
right : 30px;
}

position : relative

To place an element at a new location relative to its original position.

#myelem {
position : relative;
left : 30px;
top : 30px;
}

The above CSS will move the #myelem element 30px to the left and 30px from the top of its actual location.

position : absolute

If we want an element to be placed at an exact position in the page.

#myelem {
position : absolute;
top : 30px;
left : 300px;
}

The above CSS will position #myelem element at a position 30px from top and 300px from the left in the page and it will scroll with the page.

And finally...

position relative + absolute

We can set the position property of a parent element to relative and then set the position property of the child element to absolute. This way we can position the child relative to the parent at an absolute position.

#container {
position : relative;
}


#div-2 {
position : absolute;
top : 0;
right : 0;
}

Absolute position of a child element w.r.t. a Relative positioned parent element.

We can see in the above image the #div-2 element is positioned at the top-right corner inside the #container element.

GitHub: You can find the HTML of the above image here and CSS here.

Hope this tutorial helps.

If you need to position an element relative to its containing element first you need to add position: relative to the container element. The child element you want to position relatively to the parent has to have position: absolute. The way that absolute positioning works is that it is done relative to the first relatively (or absolutely) positioned parent element. In case there is no relatively positioned parent, the element will be positioned relative to the root element (directly to the HTML element).

So if you want to position your child element to the top left of the parent container, you should do this:

.parent {
position: relative;
}


.child {
position: absolute;
top: 0;
left: 0;
}

You will benefit greatly from reading this article. Hope this helps!