Create a dynamic flex box layout to achieve the "space-between" effect with variable-sized items

I am trying to achieve the same effect as justify-content and align-items with the value "space-between" on my flex box, but I am facing a challenge because there is no static width/height for the flexbox. Using margins adds space to all sides, whereas I only want space on the sides where flex items meet.

My ultimate goal is to align the very left and very right sides of the flex box's content with the division above it, while still having some space between the items. How can I accomplish this?

Since more flex items will be added/removed dynamically, I need a solution that does not limit support on just the three boxes used in the demonstration below:

div {
  text-align: center;
  border: 1px solid black;
}

#container {
  display: flex;
  flex-wrap: wrap;
}

#container div {
  flex: 1 1 300px;
  margin: 5px;
}
<div>Flex item sides should align with this division's sides</div>
<br>
<div id="container">
  <div>Box 1</div>
  <div>Box 2</div>
  <div>Box 3</div>
</div>

Answer №1

There are essentially only two ways to achieve that effect: one is using margins or borders along with an additional wrapper to fill the gap.

Snippet:

div {
  text-align: center;
  border: 1px solid black;
}

.wrapper {
  display: flex;
  flex-wrap: wrap;
  border: none;
  margin: -5px 0 0 -5px;      /*  filling the gap  */
}

#container .wrapper div {
  flex: 1 1 300px;
  margin: 5px 0 0 5px;        /*  5px spacing between items  */
}

#container2 .wrapper div {
  flex: 1 1 100px;
  margin: 5px 0 0 5px;        /*  5px spacing between items  */
}
<div>Flex item sides should align with this division's sides</div>
<br>
<div id="container">
  <div class="wrapper">
    <div>Box 1</div>
    <div>Box 2</div>
    <div>Box 3</div>
    <div>Box 4</div>
  </div>
</div>

<br>
<div id="container2">
  <div class="wrapper">
    <div>Box 1</div>
    <div>Box 2</div>
    <div>Box 3</div>
    <div>Box 4</div>
    <div>Box 5</div>
    <div>Box 6</div>
    <div>Box 7</div>
    <div>Box 8</div>
  </div>
</div>

Answer №2

Are you looking for this specific layout?

#container {
        display: flex;
        flex-wrap: wrap;
        border: 1px solid black;
      }
      
      #container div {
        flex: 1 1 300px;
        margin: 5px 0;
        padding: 10px;
        border: 1px solid black;
        text-align: center;
      }
      
      #container div:first-of-type{
        margin-top:0;
      }
      
      #container div:last-of-type{
        margin-bottom:0;
      }
<div id="container">
      <div>
        Box 1
      </div>
      <div>
        Box 2
      </div>
      <div>
        Box 3
      </div>
    </div>

If this is what you need, simply remove the margin-top from the first flex item (targeted with first-of-type) and the margin-bottom from the last item (targeted with last-of-type).

Answer №3

Remove the padding from the #container div and instead apply it to the inner div using #container div.

Ensure that you set the margin-bottom for the inner elements, and don't forget to remove the margin-bottom from the last inner element using :last-child.

Custom Snippet Example

div {
  text-align: center;
  border: 1px solid black;
}

#container {
  display: flex;
  flex-wrap: wrap;
}

#container div {
  flex: 1 1 300px;
  margin: 0 0 15px;
  padding: 10px;
}

#container div:last-child {
  margin: 0;
}
<div id="container">
  <div>Box 1</div>
  <div>Box 2</div>
  <div>Box 3</div>
</div>

Answer №4

Does this meet your requirements?

div {
  text-align: center;
  padding: 10px;
  border: 1px solid black;
}

#container {
  display: flex;
}

#container div {
  flex: 1 1 300px;
  margin-left: 5px;
}
<div id="container">
  <div>
    Box 1
  </div>
  <div>
    Box 2
  </div>
  <div>
    Box 3
  </div>
</div>

If this solution does not address your question, kindly provide us with a revised query.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

div containing various images of varying sizes

I am working with a layout that requires 4 images to be displayed together within a container. One image should be larger and positioned on the left, while the other three should be uniform in size and placed next to it on the right. Despite my efforts t ...

What is the best way to adjust the margins of my div based on the width of the webpage?

I have a website where I use two columns with modules to display information like this: https://i.sstatic.net/Jp97V.png However, I work on vertical monitors and when I switch to a more landscape format, the layout looks like this: https://i.sstatic.net/D ...

Exploring ways to traverse a foreach loop based on a specific array size

Currently, I am utilizing two distinct for-each loops which are outlined below: <div id="filterarea" data-bind="foreach: { data: filters, as: 'filtercategory' } "> <ul data-bind="foreach: { data: Refiners, as: 'refiner' ...

Creating a dynamic Bootstrap 4 hover effect for collapsible elements with numerous items

I have come across a few techniques for implementing the collapsible accordion feature in bootstrap. However, I am facing a challenge when it comes to customizing it so that only one panel-collapse is activated upon hovering over the div.panel-heading > ...

How to adjust the font size and font style for the [pageSizeOption] in mat-paginator

I am having trouble adjusting the font-size of the [pageSizeOptions] in my mat-paginator element in the application. <mat-paginator [pageSizeOptions]="[10, 20, 30]"></mat-paginator> The "10" appears too small compared to the text "items per p ...

Unable to load CSS background image and update code

After writing the code in my CSS file, /*The code I initially wrote*/ .wrapper::before { content: ""; position: absolute; bottom: -30%; left: 0; height: 100%; width: 100%; background: url(../img/homeTrans2.svg) no-repe ...

Triggering PHP functionality in JavaScript when a button is clicked

Struggling to run PHP code inside JavaScript with button onclick event. I have a pair of pages named test.php and demo.php, each containing a button. My goal is to disable the button in demo.php when the button in test.php is clicked. Moreover, upon reload ...

Is it possible to add border radius to the scrollbar in Firefox?

I'm trying to make the scroll bar in Chrome compatible with Firefox, but I can't figure out how to give it a border radius. Chrome scroll bar css /* Store selector */ /* width */ .lhh::-webkit-scrollbar { width: 6px; height: 56px !important ...

Ways to showcase flair in PHP code such as this

I currently have this code snippet: <h2 class="index-single">Tech Categories</h2><?php $args2 = array( 'cat' => 11 , 'posts_per_page' => 9 , 'paged' => $paged ); $the_query2 = new WP_Query( $args2 ); ...

Positioning elements on the web page using CSS tiles

Greetings! I am currently working on a website that utilizes tiles similar to this: https://i.sstatic.net/OzKQv.png Everything seems to be in place, except for one tile that is not aligning with the rest. I am struggling to figure out how to position it c ...

What is the best way to trigger the closing of a Bootstrap (version 5.2) Modal using JavaScript

In order to complete my school project, I decided to create a modal using Bootstrap in HTML. Inside the modal, there is a prompt that needs to be checked from JavaScript. How can I close the Modal from JavaScript? This way, if the prompt is valid, I can sa ...

Connect an existing function to JQuery animation

There is a function in place that changes the background image of a vertical website when a navigation menu item is clicked. <script type="text/javascript"> $(function() { $('a.panel-home').click(function(){ $("#bgimg").attr(' ...

The Bootstrap 4 accordion won't stay closed and keeps popping open when I try to close

Having an issue with the example accordion from Bootstrap 4's documentation. After troubleshooting, I discovered that the 'show' class gets removed and added again when trying to close the card. Links to images: https://i.sstatic.net/lxKiK. ...

How can I disable auto-fill for password input fields? Setting autocomplete="off" doesn't seem to be working

Hey there, I'm having some trouble with the autocomplete feature in Google Chrome. Can anyone suggest an alternative way to disable autocomplete for password fields? By the way, my project is using Vue.js. ...

Creating an interactive form with file uploads using JQuery Mobile

I'm currently working on a JQuery Mobile page that is designed to enable video uploads to the Google App Engine Blobstore, complete with title, description, and more. Below is the HTML code for the form: <form class="form-horizontal" action="{{ u ...

Exploring the Interaction of Users with HTML Table Cell <td>

I am currently analyzing the code for a web page. Within this code, users have the ability to double-click on a cell in a table (<td> as shown below) and input a value. Is there a specific attribute or element within this HTML that indicates user in ...

Injecting a full browser-screen DIV into the body of the webpage

Recently, I was tasked with developing a script that could be utilized on any website. As a test, I decided to use the Google search results page. My goal is simple - to have a full-screen semi-transparent div displayed on any site for cookie notification ...

Create a recursive CSS style for an angular template and its container

I am struggling with styling CSS inside an ng-container that is used in a tree recursive call to display a tree hierarchy. <ul> <ng-template #recursiveList let-list> <li *ngFor="let item of list" [selected]="isSelected"> <sp ...

Discover the significance of a data attribute's value for effective comparisons

I have a loop that generates random numbers and positions to create 4 answer choices for users. Now, I am trying to determine the value of the clicked button and compare it to the position of the correct answer. However, whenever I try to do this, it retu ...

The voting system will increase or decrease by 1 to 5 during each round

Recently, I added a voting system to my website inspired by this source. It's functioning well, but there is an issue where each vote can sometimes count for more than one. You can view the source code on the original website and test it out live here ...