http://stackoverflow.com/questions/10487292/position-absolute-but-relative-to-parent
Hey, so, please check link above. There is first resolving answer, which was resolving my problem. But now I have problem, that DIV isn't displaying well when it is set to position: relative and position: fixed does work correctly. DIV itself has no children, first I am trying to get parent there, I will care about children later.

#div {
    position: relative;
    left: 0;
    top: 0;
    width: 15%;
    height: 100%;
    background-color: rgba(0, 0, 0, 1);
}

There appeears black line in top left, it is 15% width and hmmm, 0,8% high? It looks like it has no height set, but it does.
If I change position: relative to position: fixed (what is actually what I need) it works.

Dani AI

Generated

Short answer: the small black strip is because percentage heights need an explicit height on the containing block — and your snippet is positioning the element itself rather than positioning a child relative to a positioned parent.

is on the right track: a percent height resolves against an ancestor that has a declared height. If no ancestor has a concrete height, height: 100% will not expand the element the way position: fixed (which attaches to the viewport) does. Also remember the usual pattern: make the parent the positioned element (e.g., position: relative) and make the child position: absolute if you intend the child to be placed inside the parent.

Practical fixes:

  • Give the parent an explicit height (px, or set it to fill the viewport with 100vh or min-height: 100vh).
  • Use absolute positioning with anchors (top:0; bottom:0; left:0) so the element fills the vertical space without relying on percent heights.
  • Or use a flex layout so a sidebar stretches to the container height.

Example (adjusts the approach in the thread):

/* parent */
.wrapper { position: relative; min-height: 100vh; }

/* sidebar inside that parent */
.sidebar { position: absolute; top: 0; bottom: 0; left: 0; width: 15%; background: rgba(0,0,0,1); }

Troubleshooting tips: inspect the computed height in DevTools to see which ancestor is the containing block, check for unexpected transforms (a transformed ancestor can change how fixed behaves), and try swapping percent-based height for viewport units or top/bottom anchors to confirm what you want. In short: set the containing block's height or switch to viewport-based or anchored absolute positioning — that will make the sidebar behave like the fixed example without using position: fixed.

height of an relative positoned element in % is caclulated on the basis of its parents. You need to set height of parent. In case parent is body you can add following css to make your dive work:-

html,body{height: 100%;}

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.