Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a fixed width column with CSS flexbox

Tags:

html

css

flexbox

CodePen: http://codepen.io/anon/pen/RPNpaP.

I want the red box to be only 25 em wide when it's in the side-by-side view - I'm trying to achieve this by setting the CSS inside this media query:

@media all and (min-width: 811px) {...}

to:

.flexbox .red {
  width: 25em;
}

But when I do that, this happens:

http://i.imgur.com/niFBrwt.png

Any idea what I'm doing wrong?

like image 572
Science Avatar asked Oct 15 '22 08:10

Science


People also ask

How do I set fixed width in CSS?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.

How do I set columns in Flex?

Approach: To create a two-column layout, first we create a <div> element with property display: flex, it makes that a div flexbox and then add flex-direction: row, to make the layout column-wise. Then add the required div inside the above div with require width and they all will come as columns.

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 you make a flex column responsive?

To change the row into a column, all we have to do is to use flex-direction property and set its value to column . So the div elements inside the section block sit below instead of beside each other. That's it for mobile layout!


2 Answers

You should use the flex or flex-basis property rather than width. Read more on MDN.

.flexbox .red {
  flex: 0 0 25em;
}

The flex CSS property is a shorthand property specifying the ability of a flex item to alter its dimensions to fill available space. It contains:

flex-grow: 0;     /* do not grow   - initial value: 0 */
flex-shrink: 0;   /* do not shrink - initial value: 1 */
flex-basis: 25em; /* width/height  - initial value: auto */

A simple demo shows how to set the first column to 50px fixed width.

.flexbox {
  display: flex;
}
.red {
  background: red;
  flex: 0 0 50px;
}
.green {
  background: green;
  flex: 1;
}
.blue {
  background: blue;
  flex: 1;
}
<div class="flexbox">
  <div class="red">1</div>
  <div class="green">2</div>
  <div class="blue">3</div>
</div>

See the updated codepen based on your code.

like image 570
Stickers Avatar answered Oct 16 '22 20:10

Stickers


In case anyone wants to have a responsive flexbox with percentages (%) it is much easier for media queries.

flex-basis: 25%;

This will be a lot smoother when testing.

// VARIABLES
$screen-xs:                                         480px;
$screen-sm:                                         768px;
$screen-md:                                         992px;
$screen-lg:                                         1200px;
$screen-xl:                                         1400px;
$screen-xxl:                                        1600px;

// QUERIES
@media screen (max-width: $screen-lg) {
    flex-basis: 25%;
}

@media screen (max-width: $screen-md) {
    flex-basis: 33.33%;
}
like image 12
chris_r Avatar answered Oct 16 '22 21:10

chris_r