Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox equivalent for fr (fraction) unit

What would be the best analogy for fr(fraction) unit in a flexbox layout? I was thinking of flex: 1 but not sure if it is the best match in terms of its growing/shrinking behavior.

What I'm trying to do is to make a fallback for grid layout so that it works in IE11. I have a grid statement grid-template-columns: 11.25rem 1fr; that I can't make work in IE (even with Auto-prefixer that adds -ms-grid-columns columns still stack on top of each other).

So I was thinking to maybe just implement it in flexbox for IE using something like this:

.container {
  max-width: 46rem;

  .parent {
    display: flex;

    & :first-child {
      inline-size: 11.25rem;
    }

    & :last-child {
      flex: 1;
    }
  }
}
like image 961
nick722 Avatar asked Jun 09 '20 06:06

nick722


People also ask

How do you use FR on flex?

The <flex> data type is specified as a <number> followed by the unit fr . The fr unit represents a fraction of the leftover space in the grid container. As with all CSS dimensions, there is no space between the unit and the number.

What unit is fr?

Fr is a fractional unit and 1fr is for 1 part of the available space. The following are a few examples of the fr unit at work. The grid items in these examples are placed onto the grid with grid areas. The 4 columns each take up the same amount of space.

How many pixels is 1FR?

Now we know that each fr unit represents 225px.

How is Fr calculated CSS?

Fr units calculates based on available space. First it will deduct the grid gap from total width and then calculate the proportion. grid-gap: 20px; To avoid this we need to deduct the size of grid-gap from the columns.


1 Answers

Turned out flex: 1 works for my needs.

It's a shorthand for flex: 1 1 0px; so

flex-grow: 1 lets it grow when there is extra space. flex-shrink: 1 lets it shrink when there is not enough space. flex-basis: 0px allows it to have width defined by its content with respect to its container's width.

like image 126
nick722 Avatar answered Oct 18 '22 21:10

nick722