Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox with one fixed column width [duplicate]

Tags:

css

flexbox

I am trying to acheive a flexbox with two columns, the left that is of a fixed width and the right that scales as the page size is altered. For example:

<style>     .flex_container {         display: flex;         width: 100%;     }      .flex_col_left {         width: 200px;     }     .flex_col_right {         width: 85%;     } </style>  <div class = "flex_container">     <div class = "flex_col_left">         .....     </div>     <div class = "flex_col_right">         .....     </div> <div> 

This works to an extent but it does not feel right as I am mixing px and %. Is it incorrect to do it this way and if so what might be a better solution?

EDIT Classes naming issue was a typo and now fixed.

like image 507
RGriffiths Avatar asked Jan 10 '17 11:01

RGriffiths


People also ask

How do I fix the width of my flex column?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.

Does flexbox work with position fixed?

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.


2 Answers

Instead of setting width in % on right column you can just use flex: 1 and it will take rest of free width in a row.

.flex_container {    display: flex;    width: 100%;  }  .flex_item_left {    width: 200px;    background: lightgreen;  }  .flex_item_right {    flex: 1;    background: lightblue;  }
<div class="flex_container">    <div class="flex_item_left">      .....    </div>    <div class="flex_item_right">      .....    </div>    <div>
like image 148
Nenad Vracar Avatar answered Sep 19 '22 12:09

Nenad Vracar


You can use flex-grow: 1 on the non-fixed-width element to allow it to grow (and no width setting).

.flex_container {    display: flex;    width: 100%;  }  .flex_item_left {    width: 200px;    background: #fa0;  }  .flex_item_right {    background: #0fa;    flex-grow: 1;  }
<div class="flex_container">    <div class="flex_item_left">      .....    </div>    <div class="flex_item_right">      .....    </div>    <div>
like image 22
Johannes Avatar answered Sep 21 '22 12:09

Johannes