Increase the spacing for list items by applying a margin using flexbox styling

I am currently utilizing the power of flexbox to structure my elements. Within my layout, I have 5 lis (with room for growth) each set with a width of flex-basis:calc(100%/3). This configuration allows for 3 li's in each row within the wrapper (ul) that is sized at 70%.

All seems well up to this point. However, an issue arises when I introduce margins to the li's. The addition of a margin causes only 2 li's to display per row with excess spacing whereas I require 3.

In light of this dilemma, I've explored two potential solutions, yet each presents its own set of complications:

  • One approach involves applying a margin to the li's, e.g., 10px, then compensating by reducing the height and width of each li by 20px (10 * 2). Unfortunately, this strategy isn't feasible as it would result in undersized li's contrary to my defined dimensions.

  • The other option entails setting the justify-content property to either space-around or space-between. Yet, this adjustment disrupts the desired chart-like arrangement of the lis, deviating from their intended positioning in columns and rows.

Is there a method to introduce a margin to the li without compromising other properties?

(I am open to incorporating JavaScript/JQuery if deemed necessary.)

JSFiddle

Below is a snippet of the code:

body, html {
    height:100%;
    margin: 0;
    padding:0;
}
#flexWrapper {
    display: flex;
    justify-content: center;
    height: 100%;
    align-items: center;
    overflow: auto;
}
#flexContainer {
    width: 70%;
    background-color:yellow;
    list-style-type: none;
    padding: 0;
    margin: auto;

    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: center;
    align-content:flex-start;
}
li {
    background-color: tomato; 
    border: 1px solid black; 
    height:50px;
    box-sizing: border-box;
    flex-basis:calc(100%/3);
    margin:10px;
}
<div id="flexWrapper">
    <ul id="flexContainer">
        <li class="flex-item">1</li>
        <li class="flex-item">2</li>
        <li class="flex-item">3</li>
        <li class="flex-item">4</li>
        <li class="flex-item">5</li>
    </ul>
</div>

Answer №1

Finally, I have found a solution that works! Credit goes to @Syahrul for sparking the idea in the comments.

Here's the plan: To add a 1.5% margin on both sides of each "li" element, we will adjust the width of the wrapper ('#flexContainer') by (1.5 * 2) * 3. This gives us a total width of 79% for #flexContainer.

Then, we'll decrease the width of each "li" element by 3% (the margin size). This can be achieved with 'flex-basis:calc(100%/3 - 3%)'.

And there you have it! Check out the updated JSFiddle

body, html {
    height:100%; margin: 0; padding:0;
}
#flexWrapper {
    display: flex;
    justify-content: center;
    height: 100%;
    align-items: center;
    overflow: auto;
}
#flexContainer {
    width: 79%;
    background-color:yellow;
list-style-type: none;
padding: 0;
    margin: auto;

display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-items: center;
align-content:flex-start;
}
li {
    background-color: tomato; border: 1px solid black; height:100px;
    box-sizing: border-box;
    flex-basis:calc(100%/3 - 3%);
    margin:1.5%;
}
<div id="flexWrapper">
    <ul id="flexContainer">
        <li class="flex-item">1</li>
        <li class="flex-item">2</li>
        <li class="flex-item">3</li>
        <li class="flex-item">4</li>
        <li class="flex-item">5</li>
    </ul>
</div>

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

Issue with Semantic-UI Special PopUp not displaying on screen

I'm currently working on creating a unique pop-up feature using custom HTML, with the intention of adding content to it later on. My console is displaying the following message: Popup: No visible position could be found for the popup. $(document) ...

Leveraging configuration files for HTML pages without any server-side code

I am currently working on developing an app using phonegap. At the moment, I have the reference to a script in every page like this: <script language="javascript" src="http://someServer/js/myscript.js"></script> If the server 'someServer ...

Creating a connection between a secondary jQuery anything slider and a distinct CSS style on a webpage

Currently, I am attempting to incorporate two anything sliders within the same webpage. My goal is to apply unique styling to each slider. Despite my efforts, I am encountering difficulties in calling upon a second style sheet without it completely overr ...

Enhance the user experience with a personalized video player interface

I am facing difficulty in creating a responsive video with custom controls. While I understand that using a <figure></figure> element and setting the width to 100% makes the video responsive, I am struggling with making the progress bar also r ...

Is it possible to include two functions within a single HTML script?

On my webpage, there are multiple tabs that display different sets of data. Below is the code for handling the functionality of these tabs: function openTab(event, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassNa ...

Chosen option - Selection Menu - Relational Database Language

I am currently working on a dropdown list that needs to be filled from a database: <TH> <FORM> <p>Department</p> <SELECT size="1" id="depart"> <OPTION> <?php ...

Ways to change the default blue dropdown color in the Chrome browser

When you click on the drop-down menu, a blue color appears when hovered over. I want it to be red instead, but for some reason, it's not working in Chrome browser. Here is the CSS class that I have used: option:checked { box-shadow: 0 0 10px 100p ...

Best practices for hiding table rows in PHP when cells are empty

I am working with htmltopdf code provided by developers that includes a table with 'Question' and 'Answer' sections. My task is to hide any rows where the answer field is empty. Below is an excerpt from the code snippet: $pdfHTML = &a ...

Having issues with AngularJS ng-if when implemented within a Form

Is there a way to hide my form after it has been submitted using ng-if? I am facing an issue where clicking the 'See' button toggles the form on and off, but the same functionality does not work with the 'Add' button. Any insights on wh ...

Organizing Content with Bootstrap - Utilizing Sections and Divs

Currently utilizing a Bootstrap template for my website development, I am encountering two challenges that have me stumped: 1) Upon placing my HTML code within a <section>, an automatic top margin/padding seems to be applied to the section. Despite ...

Obtaining text from HTML code

Currently, I am faced with a challenge. My task involves extracting HTML text and saving it as an Anchor object. Here is an example: <html> <head> <title>{dynamihead/}</title> </head> <body> {repeatinghtml} <p>{r ...

Modify the style of a div using my JavaScript code

Looking for assistance in updating my code to meet my requirements. Here is what I am looking for: You can find my full code here: http://jsfiddle.net/2urmku1h/ Specifically, I need to make a change to this section: <div style="display:none;">Chan ...

What is the best way to customize the appearance of the initial row in each tr cluster?

Looking to add a button for expanding child rows within a datatable, with each group in the table having the same child elements.... The provided CSS currently displays the icon on every row throughout the entire table. td.details-control { backg ...

Encountering an issue when passing a response using coverage.js

I am utilizing coverage.js to showcase data. When I pass my variable containing the coverage response into an HTML file, similar to how it's done in Angular to display expressions, I encounter a syntax error: <div class="container" style="margin- ...

jQuery's query yields a NULL result

y.html: <h4> Modified: <span id="link-modified"></span></h4> --> Date is not displayed jQuery: var modified_date = new Date((data.modified)*1000); --> data.modified is in UNIX timestamp $('#link-modified').text(mo ...

Use jQuery to dynamically assign a unique div id to a specific class element

<div class="leaflet-marker-icon"></div> <div class="leaflet-marker-icon"></div> <div class="leaflet-marker-icon"></div> <div class="leaflet-marker-icon"></div> The map generates these divs, all with the s ...

The text box in HTML5 is too wide due to excessive padding

I need to update my input field to be padded from the inside as I transition to HTML5. It was working fine in HTML 4.01 Transitional, but now it overflows outside of the table frame in HTML5. Can someone help me fix this for HTML? Snippet of HTML Code &l ...

What is a method to shift an element to the side without actually deleting it from the DOM (perhaps using a simulated slide

For nearly a month, I've been battling with a challenging issue and so far have had no success. Imagine a scenario where there's a button along with an element in the DOM (such as an image or something similar). Upon clicking this button, the e ...

Create an image of a static map from Google

I have incorporated the Google Static Map API into my Angularjs Application to display images of Google maps. Browse here for a glimpse Everything operates smoothly when there are fewer map coordinates. However, when I increase the number of map coordina ...

Steps for adding an onclick function to a collection of div elements

My current project involves utilizing the Flickr API and I am interested in creating a popup div that opens when clicking on each image. The goal is to display the image in a larger form, similar to how it's done using Fancybox. I'm looking for ...