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.
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.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With