3

I have .left-navbar and .columns-wrapper in which are two columns, .left-column and .right-column. The first column and second one has 50% of .columns-wrapper until I add position: fixed to .right-column and .right-column takes 50% of browser width not .columns-wrapper.

.page-wrapper {
  width: 100vw;
  height: 100%;
  display: flex;
  background: red;
}

.page-wrapper .left-navbar {
  width: 88px;
  height: 100%;
  background: yellow;
}

.page-wrapper .columns-wrapper {
  width: calc(100% - 88px);
  height: 100%;
  background: orange;
  display: flex;
}

.page-wrapper .columns-wrapper .left-column {
  width: 50%;
  height: 100%;
  background: red;
}

.page-wrapper .columns-wrapper .right-column {
  width: 50%;
  height: 100%;
  background: green;
}
<div class="page-wrapper">
  <div class="left-navbar">

  </div>
  <div class="columns-wrapper">
    <div class="left-column"></div>
    <div class="right-column"></div>
  </div>
</div>
wangdev87
  • 8,611
  • 3
  • 8
  • 31
  • please, can you edit your question adding a https://stackoverflow.com/help/minimal-reproducible-example (minimal reproducible example) – Giuppox Nov 20 '20 at 07:52
  • `The fixed element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to the initial containing block established by the viewport` from [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/position) – Sfili_81 Nov 20 '20 at 07:53
  • so what I should change out – sales.bzielinski Nov 20 '20 at 07:53

2 Answers2

1

Set columns-wrapper position to relative, and left-column and right-column position to absolute. Absolute position is related to the ancestors container which has the position value of relative.

position: fixed is related to the whole browser position, rather than its parent containers.

.page-wrapper .columns-wrapper {
  position: relative;
  ...
}

.page-wrapper .columns-wrapper .left-column {
  position: absolute;
  ...
}

.page-wrapper .columns-wrapper .right-column {
  position: absolute;
  ...
}
wangdev87
  • 8,611
  • 3
  • 8
  • 31
0

That behavior is completly normal as when you use a fixed position the element is removed from the normal content flow.

#wrapper {
    width: 500px;
    background-color: cyan;
}

.w-1\/2 {
    width: 50%;
    background-color: red;
    height: 100px;
}

.fixed {
    position: fixed;
    width: 50%;
    height: 50px;
    background: yellow;
}
<div id="wrapper">
  <div class="w-1/2">
  </div>
  
  <div class="fixed">
  </div>
</div>

As you can see in example bellow even if I set it width to 50% It doesn't take the 1/2 space of the #wrapper but for the body element.

#wrapper {
    width: 500px;
    background-color: cyan;
}

.w-1\/2 {
    width: 50%;
    background-color: red;
    height: 100px;
}

.fixed {
    position: fixed;
    top: 0;
    left: 50%;
    width: 50%;
    height: 50px;
    background: yellow;
}
<div id="wrapper">
  <div class="w-1/2">
  </div>
  
  <div class="fixed">
  </div>
</div>
Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31