Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make flex element ignore child element

Tags:

html

css

flexbox

Is it possible to make a flex element ignore a child element so it's size does not affect the other elements?

For example, I have a wrapper with display: flex. It has a header, content, and footer.

<div class="wrapper">     <header></header>     <article></article>     <footer></footer> </div> 

I want the wrapper to ignore the header tag (the header will be fixed to the top of the window). The article will be set to flex: 1 so it takes up the rest of the space, forcing the footer to the bottom of the page. Here is some sample CSS:

.wrapper {     display: flex;     flex-direction: column;     height: 100%;     padding-top: 50px; /* Accounts for header */ }  header {     height: 50px;     position: fixed;     top: 0;     width: 100%; }  article {     flex: 1; }  footer {     height: 50px; } 

I know I could just move the header outside of the wrapper but I have a lot of existing code that will make that a bit more difficult. Is what I am asking even possible?

like image 454
Sean Avatar asked Oct 03 '16 20:10

Sean


People also ask

Which flex property will you use to prevent child element?

Heights equalised within a row.

Does display flex apply to children?

When you enable the flex display setting for a parent element, the children align left and stack horizontally by default. Flex containers will not affect or change the layout of the children within their direct, child elements.

Do children inherit display flex?

The display property is not inherited, so display: flex is not either.

What element do you target Flexbox on to parent or child?

It's all about the children Flexbox is different from many other CSS properties: instead of targeting an element and acting directly upon it, we target the parent element and that controls the layout of its children. We're making a flex container and all the children of that container become flex items.


2 Answers

In your .wrapper declare flex-wrap: wrap. Then for your header, you can add the style flex-basis: 100% which will force everything else down below the header.

like image 105
Adrian Barsby Avatar answered Sep 26 '22 06:09

Adrian Barsby


Use position: absolute for the child element to exclude it from the flex calculations

like image 36
Ayan Avatar answered Sep 26 '22 06:09

Ayan