Flex items with fixed flex-basis and incorrect spacing between them

By using flex: 1 on flex items, the Flexbox feature will automatically adjust the sizes of your items, even if you include margins (gaps) or not. All items will maintain the same width. However, a scenario may arise where a flex item has the same width as in a different flexbox, but this item is nested within another flexbox with fewer children. In this case, simply using flex: 1 may not work. You will need to specify a max-width or flex-basis to match the width of an item in the flexbox where all items use flex: 1.

The closest solution I found is by using flex: 0 1 <width>% or max-width: <width>% (which yield the same result).

It seems that the issue arises when margins are added, as without them, there is no problem. However, knowing the size of the margin and how many will be used can help in replicating the width of the other flexbox's items.

Take a look at this example:

ul {
  display: flex;
}

ul li {
  flex: 1;
  margin-right: 25px;
}

ul li:last-child {
  margin-right: 0;
}

ul li.fourth {
  flex: 0 1 25%;
}

/* The irrelavent part starts here */
ul {
  list-style:none;
  margin: 0;
  padding: 0;
  font-size: 0;
  line-height: 0;
}
ul li {
  height: 100px;
}
ul:nth-child(odd) li:nth-child(even),
ul:nth-child(even) li:nth-child(odd) {
  background-color: red;
}
ul:nth-child(odd) li:nth-child(odd),
ul:nth-child(even) li:nth-child(even) {
  background-color: blue;
}
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
<ul>
  <li></li>
  <li class="fourth"></li>
</ul>

Answer №1

Forget about it, I've figured out a solution!

Using max-width along with calc() and the provided formula:

(100% * 1/$items) - (($gap * ($items - 1)) / $items)

In this scenario, when variable $gap is set as 25px, the value of variable $items in the formula becomes 4 for the class .fourth, and 5 for the class .fifth.

This results in the following CSS:

ul {
  display: flex;
}
ul li {
  flex: 1;
  margin-right: 25px;
}
ul li:last-child {
  margin-right: 0;
}
ul li.fourth {
  max-width: calc(25% - 18.75px);
}
ul li.fifth {
  max-width: calc(20% - 20px);
}

/* The irrelevant part starts here */
ul {
  list-style:none;
  margin: 0;
  padding: 0;
  font-size: 0;
  line-height: 0;
}
ul li {
  height: 100px;
}
ul:nth-child(odd) li:nth-child(even),
ul:nth-child(even) li:nth-child(odd) {
  background-color: red;
}
ul:nth-child(odd) li:nth-child(odd),
ul:nth-child(even) li:nth-child(even) {
  background-color: blue;
}
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
<ul>
  <li></li>
  <li class="fourth"></li>
</ul>
<p><hr></p>
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
<ul>
  <li></li>
  <li class="fifth"></li>
</ul>

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

"PHP MySQL news error" - An unexpected error occurred

Just incorporated a news system using MySQL/PHP, it functions flawlessly; however, encounters errors when inserting HTML. For instance, inserting a YouTube video results in the addition of / or \ Can someone advise on what changes need to be made in ...

Dynamic Label Editing on ASP Page Using Master Page

I am working on an ASP page with a Masterpage element on my website. The specific functionality I am trying to implement involves having multiple labels with an edit hyperlink next to each one. When the user clicks on the Edit hyperlink, I want the label t ...

Tips for sending images as properties in an array of objects in React

I've been experimenting with various methods to display a background image underneath the "box" in styled components. How can I pass an image as a prop into the background image of the box with the image stored in the array of objects? I'm unsure ...

Using CSS to style the last element in a set of dynamic content

I am currently working on targeting the last element in a dynamically generated content set. For instance, here is an example of how the generated content appears: <div class="block"> <div class="block-inner"> <div class="box"> ...

Closing the modal by simply clicking outside of it's boundaries

Is there a way to close the modal by clicking outside of it using pure JS or AngularJS? I'm struggling to figure out how to implement this functionality. Any assistance would be greatly appreciated. View Fiddle .modalDialog { position: fixed; ...

Build a Container Div using DOM/XPATH techniques

I have just initialized a new DOM XPATH OBJECT. After performing several operations, I saved my result using SaveHtml $String[] = $dom->saveHTML(); Next, I placed the content into a file. file_put_contents($filename, $string); The HTML Structure l ...

Can you explain the significance of the characters '& > * + *' in JSX (CSS)?

When using material UI, the progressbar demo includes lines of JSX code like this: '& > * + *': { marginTop: theme.spacing(2), }, Can you explain what the selector '& > * + *' represents in this context? ...

Determining the precise margin within a nested div structure

Hopefully, my description of a "nested div" was accurate. My current situation involves an image within a div structured like this: content -> content-inner -> column_left I'm struggling to start the image at 0 px (the extreme left side of th ...

Guide on aligning a non-fixed child within a fixed parent container

I am working on a project with a fixed container (div#popup) that has some CSS styling. The container is set to position:fixed, with specific dimensions, border radius, and background color. Within this fixed container, there is a form element that I am h ...

Arrange the navbar on top of the following section

I'm seeking a solution that will achieve the desired outcome without using position: fixed on the navbar, as shown in the following image (I want it to disappear when scrolling down): https://i.sstatic.net/lFW0w.png The image serves as the backgroun ...

Voting mechanism - clicking to activate class and subsequently removing it

Looking to implement a feature where users can vote by clicking an up or down arrow. While the Ajax calls are straightforward, I'm facing some challenges on the visual aspect. Here is my HTML setup: <td class="td5"> <a class="vote" href=" ...

Is there a log for keeping track of any CSS errors that occur

When a style is overridden in Google Chrome tools, it gets crossed out and you can check the Computed tab to see which element has taken precedence. Imagine I have CSS code like this: .myClass { position: static; top: 20px; } This is incorrect be ...

Can a font size be adjusted dynamically based on the width of its containing class?

I have encountered this issue before, but the previous solution did not work for me. I am now seeking a viable alternative. Currently, I am developing a website and have an event announcement block that needs to display the event date spanning the entire ...

Displaying a div in Vue.js when a button is clicked and an array is filled with

Hey there! I'm having an issue with displaying weather information for a specific location. I want to show the weather details in a div only when a button is clicked. Despite successfully fetching data from the API, I'm struggling to hide the wea ...

Delete auto-generated list using handlebars JS

I have successfully created a dynamic list using Handlebars.js and its template. However, I am now facing confusion on how to remove or delete items from the list using a function or specific code. As I am new to Handlebars, I would appreciate any help. ...

Ways to decrease the width of a outlined material icon

Is there a way to adjust the appearance of this icon to make it appear thinner? I have tried using font-weight and stroke-width without success. .material-icons-outlined, .material-icons.material-icons--outlined { font-family: 'Material Icons Out ...

Transforming segments into divisions

I recently designed a website using sections instead of divs in order to achieve the desired alignment of the divs within the same container. However, I am facing difficulties and confusion in implementing this approach. Currently, my divs collapse on top ...

Choosing images based on aspect ratio in CSS

I am looking to add a specific class to image elements, but only for those that are in landscape format. Is there a way to target images in my HTML with a width/height ratio greater than 1, similar to how media queries work with the "device-pixel-ratio" f ...

Revamp Button content in HTML and Angular 2

My goal is to dynamically change the button text once it is selected. Initially, the YES button is highlighted and slightly changes upon being clicked. I want the button to switch to NO when clicked, and if hovered over, a disable symbol should be displaye ...

Unable to collapse the bootstrap navbar on a targeted screen size

My intention was to collapse the Bootstrap navbar when the screen width reaches 1280px. However, instead of collapsing, the navbar appears at the top. Here is the code snippet I am using: body { padding-top: 90px; ...