Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Responsive Flex box

I have a loop, and this loop saves everything I write, and returns it to me, as a list with one item under the other, but I need to show 2 items per line, using Responsive Flex box, but I don't know how to do it!

I imagine it's very simple, but I'm a beginner, can someone help me?

I'm doing it this way, but is wrong, i need to complete the screen

<ul class="flex flex-wrap gap-6" style="width: 50%; flex-basis: 100%">
     @foreach($notes as $note)
        <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg mb-4">
            <div class="p-6 bg-white border-b border-gray-200" style="width: 100%">
                   {{ $note->actual_date }}
                   <div class="prose prose-sm mt-4">
                   {{ $note->getNoteAsHTML() }}
                   </div>
             </div>
         </div>
       @endforeach
   </ul> 
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I need to show 2 items per line, using Responsive Flex box

I think I know what you are looking for, let me know if I am wrong and I can remove this answer or edit it to embellish further what your intent may be.

This can be achieved using a calculated min-width on your flex-children along with flex-grow.

For example if you have reset your margin and padding and know there is no hidden of either to deal with then you can calculate a gap or margin of separation between elements along with a percentage on the flex child elements. Below I use a Javascript driven array to iterate over and simulate your issue of a dynamic amount of strings. I add a flex-wrap: wrap; along with a gap of 1em on the parent element => flex parent, and then add a flex-grow of 1, along with a min-width: calc(50% - 1em). So the flex-grow: 1 tells css how much of the remaining space in the flex container should be assigned to the item, then min-width: calc(50% - 1em) tells css that the minimum width of the element should be 50% of the parents width minus the gap size of 1em. So basically this will only allow two elements to take up the space of each flex parents row, then the parents flex-wrap: wrap takes over and wraps your next element to the next row or line.

const parent = document.querySelectorAll('.parent')

const target = parent[0];

const arr = ['first list item', 'second list item', 'third list item', 'fourth list item', 'fifth list item', 'sixth list item', 'seventh list item', 'eigth list item', 'ninth list item']
arr.forEach(val => {
  let child = document.createElement('DIV')
  child.classList.add('children')
  child.textContent = val
  target.append(child)
})
* {
   padding: 0;
   margin: 0;
}

.parent {
  display: flex;
  flex-wrap: wrap;
  gap: 1em;
}

.children {
  display: flex;
  justify-content: center;
  flex-grow: 1;
  min-width: calc(50% - 1em);
  background-color: green;
  height: 2em;
}
<div class="parent">
</div>

over 4 years ago · Santiago Trujillo Report

0

Use flex-basis to make sure you get two flex items per flex line, then use flex-grow to let them fill the remaining available space.

flex-basis is the property that lets the flex items “tell” the flex container how big they’d like to be. You can specify it as a fixed px value, or you can specify a percentage. The percentage refers to the flex container’s inner size.

The flex container (your ul element) will take each element, look at its flex-basis, and place them on a flex line if they can still fit. For example, if you set flex-basis: 50%, you can fit two items per line, and the item after those two will have to go on its own line.

The challenge is that if you use gap in your flex container to have some spacing between the flex items, that might push your flex items onto a new line. For example, your first flex item is flex-basis: 50%, then you have 20px gap, and the second flex item with flex-basis: 50% won’t fit anymore.

Two possible solutions:

  • manually account for the gap when you set your flex-basis. For example, if you have gap: 10px, you could set flex-basis: calc(50% - 10px)
  • give your flex items a flex-basis that guarantees only two flex items per flex line, but leaves enough room for the gap. Then let the flex items grow to fill the remaining space in the flex line. For example, your two flex items with flex-basis: 35% will add up to 70%, then your gap will add a bit, and the remaining ~30% is almost definitely enough to accommodate the gap, unless it’s really huge

Here’s a working solution using that second idea:

ul {
  list-style-type: none;
  padding: 0;
}

.flex-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.flex-item {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: 35%;
}

.flex-item:nth-child(odd) {
  background-color: whitesmoke;
}

.flex-item:nth-child(even) {
  background-color: burlywood;
}
<ul class="flex-container">
  <li class="flex-item">Item 1</li>
  <li class="flex-item">Item 2</li>
  <li class="flex-item">Item 3</li>
  <li class="flex-item">Item 4</li>
  <li class="flex-item">Item 5</li>
  <li class="flex-item">Item 6</li>
  <li class="flex-item">Item 7</li>
  <li class="flex-item">Item 8</li>
  <li class="flex-item">Item 9</li>
  <li class="flex-item">Item 10</li>
</ul>

over 4 years ago · Santiago Trujillo Report

0

<ul class="flex flex-wrap gap-6" style="width: 50%; flex-basis: 100%">
 @foreach($notes as $note)
    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg mb-4">
        <div class="flex p-6 bg-white border-b border-gray-200" style="width: 100%">
               <div class="box">
               {{ $note->actual_date }}
               </div>
               <div class="box">
               {{ $note->getNoteAsHTML() }}
               </div>
         </div>
     </div>
   @endforeach
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!