Place elements side by side if they fit, else make them block elements

duhaime

I have a bunch of pairs of elements. Each pair has two divs. I'd like each pair to sit side-by-side if they can fit on the same line, else I'd like them to sit one on top of the other. Here's the situation:

.pair {
  display: flex;
  flex-direction: row;
}

div {
  margin: 0 5px;
}
<div class="pair">
  <div>Yeet a deet</div>
  <div>Make me display:block when the container can't fit the divs side-by-side</div>
</div>

Without setting a breakpoint or using JS, is there a way to make these divs display: block when they can no longer fit side-by-side without wrapping? Any pointers would be very helpful!

ray hatfield

Would flex-wrap not solve it?

.pair {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.pair > * {
  box-shadow: 0 0 0 1px blue;
  flex: 1 1 auto;
}

div {
  margin: 0 5px;
}
<div class="pair">
  <div>Yeet a deet</div>
  <div>Make me display:block when the container can't fit the divs side-by-side</div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to place two elements side by side in HTML

Two inline-block elements, each 50% wide, do not fit side by side in a single row

displaying elements side by side

Place 2 elements per div side-by-side in React

Twitter Bootstrap: Place elements side by side in small resolution?

How to place <h3> and <button> elements side to side

I want to place html elements in one row, side by side

Aligning two elements side by side and forcing them to stay on level

How to make these elements all sit side by side with 100% width

How to make two elements in a react project side by side

Android : 3 elements side by side

Align div elements side by side

Align <div> elements side by side

Align span elements side by side

Attach Elements to side page

How to place many text elements side by side, that you see no difference between the text?

Divs laid out side by side with inline-block change layout when child elements added

Android: How to get 3 elements side by side

Css for displaying two elements side by side

Side by side <td> elements to have the same height

float multiple elements side by side in html

How to align form elements side to side on html

How can I align these elements side by side?

Adding two elements side by side, responsive

css: How to make elements not to overlap + automatically place them in a relevant position

list menu element place them side-by-side

I have 2 elements in a div side by side, will hiding one make the other expand to fill the space of the div they're in?

Make two side-by-side html elements adapt their widths so (text-)content gets same height

Sequence contains no elements server side