Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have a position: fixed; behaviour for a flexbox sized element?

Tags:

css

flexbox

I have a div called .side-el which I would like to have in a position: fixed; behavior, but as soon as I apply position fixed the width alternates from the right one. The right width would be the one set by flexbox. How can I achieve this goal?

.container {    -webkit-align-content: flex-start;    align-content: flex-start;    -webkit-align-items: flex-start;    align-items: flex-start;    display: -webkit-flex;    display: flex;    -webkit-flex-direction: row;    flex-direction: row;    -webkit-flex-wrap: wrap;    flex-wrap: wrap;    -webkit-justify-content: flex-start;    justify-content: flex-start;        * {      box-sizing: border-box;      -webkit-flex-grow: 1;      flex-grow: 1;      -webkit-flex-shrink: 0;      flex-shrink: 0;    }  }      .main-el {    box-sizing: border-box;    padding:0 2em;    width: 70%;  }    .side-el {    box-sizing: border-box;    width: 30%;  }
<div class="container" style="background-color: blue; height: 100px;">    <div class="main-el">      <div  style="background-color: red; height: 1000px;">content</div>    </div>    <div class="side-el" >      <div style="background-color: red; height: 100px;">content</div>    </div>  </div>
like image 224
Daniel Schmidt Avatar asked Mar 13 '15 15:03

Daniel Schmidt


People also ask

Does position fixed work with flexbox?

position:fixed makes flexbox not workable, since it overrides the position of <p>Hello</p> Try to remove it and see the difference. If you want align-items:center works; try to set height: 100vh; since the body height of webview is not set to screen height by default.

How do I keep my div position fixed?

A pinned-down menu. The interesting rule here is the ' position: fixed ', that makes the DIV stay fixed on the screen. The ' top: 50% ' and ' right: 0 ' determine where the DIV is displayed, in this case: 50% down from the top of the window, and a constant 0px from the right.

How do you set a flex position in CSS?

Justify ContentFlex Start positions element at the start of the page. Flex End sets the element to the end of the page. Space Around arranges the items evenly but with spaces between them. The spaces will be equal among all the elements inside a flex-box container, but not outside them.


2 Answers

Here's a way to do this inspired by bootstrap:

.fixed-top {   display: flex;   position: fixed;   top: 0;   left: 0;   right: 0; } 

This gives your flex-box room to breathe and do it's flex-box thing. If your flex-direction is column, you could use top, left, bottom instead.

This works because when you give an element a fixed position and a left and right of 0 or a top and bottom of 0, the element is stretched to fill the space from left to right, or top to bottom. That in turn allows a flex-box to use the amount of space you would expect without position fixed.

like image 200
Wylliam Judd Avatar answered Oct 14 '22 05:10

Wylliam Judd


You can't.

As explained by the CSS2.1 spec:

Absolutely positioned boxes are taken out of the normal flow.

And the Flexible Box Layout spec confirms that:

An absolutely-positioned child of a flex container does not participate in flex layout. However, it does participate in the reordering step (see order), which has an effect in their painting order.

(Emphasis mine)

like image 43
Oriol Avatar answered Oct 14 '22 03:10

Oriol