Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic width flexboxes that wrap yet keep column structure? [duplicate]

Tags:

html

css

flexbox

I'm looking for cards that have dynamic width to fill their container, yet be able to wrap if the card gets too small. I got it to do that. Yet I notice that the last card fills up the space that's left. I don't want it to do that. I want it to keep the same column structure. is this possible? I don't have to use flexboxes if there's another way to do it let me know.

enter image description here

enter image description here

.container {
    padding: 20px;
    display : flex;
   flex-flow: row wrap;
   background:white;
}
.container > div {
    border: 1px solid black;
    background: #ececec;
    margin:5px;
    min-width:200px;
    padding: 10px;
    margin-left: 10px;
    flex: 1;
}
<div class="container">
    <div>Div 1</div>
    <div>Div 2</div>
    <div>Div 3</div>
    <div>Div 4</div>
</div>

Here's my JSFiddle which is presently doing what the first picture is showing (which is not what I want). Is there a way to do this?

https://jsfiddle.net/foreyez/0b5dm2t2/

like image 578
Shai UI Avatar asked Jan 02 '18 17:01

Shai UI


People also ask

Which property specifies whether Flex items should wrap or not?

Css flex-wrap property The flex-wrap property specifies whether flex items should wrap or not.

Is flex-wrap responsive?

The flex-wrap property is a quick way to make parent elements more responsive on various screen sizes. As with flexbox in general, it simplifies page layouts so you don't have to manually set breakpoints or manage the page overflow yourself.

Which property is used to manage layout of wrapped flex items?

The flex-wrap CSS property sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, it sets the direction that lines are stacked.


2 Answers

You can do this with CSS grid layout, using grid-template-columns. With minmax(200px, 1fr) you can set min-width of each column to 200px and max-width to one track of grid layout. You also need to use auto-fit to make tracks take full width in case there is available space.

.container {
  padding: 20px;
  display: grid;
  background: white;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

.container > div {
  border: 1px solid black;
  background: #ececec;
  margin: 5px;
  padding: 10px;
  margin-left: 10px;
}
<div class="container">
  <div>Div 1</div>
  <div>Div 2</div>
  <div>Div 3</div>
  <div>Div 4</div>
</div>
like image 190
Nenad Vracar Avatar answered Sep 28 '22 16:09

Nenad Vracar


A late answer, but here is another trick using pseudo-element that you make exactly the same as your div and you put at the end by adjusting order.

No need to add extra HTML

No need to use media query

No need to alter your current CSS and flex layout

.container {
  padding: 20px;
  display: flex;
  flex-flow: row wrap;
  background: white;
}

.container>div {
  border: 1px solid black;
  background: #ececec;
  margin: 5px;
  min-width: 200px;
  padding: 10px;
  margin-left: 10px;
  flex: 1;
}

.container:after,
.container:before {
  content: "";
  margin: 5px;
  min-width: 200px;
  padding: 10px;
  margin-left: 10px;
  flex: 1;
  order: 2;
}
<div class="container">
  <div>Div 1</div>
  <div>Div 2</div>
  <div>Div 3</div>
  <div>Div 4</div>
</div>
like image 37
Temani Afif Avatar answered Sep 28 '22 16:09

Temani Afif