The flexibility factor maximizes the expansion or contraction of spacing between elements

I am currently working on creating a navigation menu that consists of two lists aligned on the same line. It is essential for all items within the lists to have spacing between them, with a maximum of 25px.

An issue with flex-grow: When utilizing flex-grow, the elements tend to expand beyond their width + 25px because there is adequate space available.

An issue with margin-right: In scenarios where the container is smaller in size, the margin-right of 25px remains unchanged, leading to items breaking lines instead of adjusting their margins accordingly.

Potential Solution: Introducing a div element between each item and assigning it properties like flex-grow:1 and max-width:25px.

Is there an alternative approach without having to introduce additional HTML elements?

Feel free to explore the issue further through the provided codepen:

Answer №1

One interesting approach is to utilize pseudo elements along with the display:contents property.

ul,
li {
  list-style: none;
  margin: 0;
  padding: 0;
}

ul{
  display: inline-flex;
  max-width:100%;
}

.container {
  border: 1px solid red;
  margin-bottom: 20px;
  display:flex;
  flex-wrap:wrap;
  justify-content: space-between;
}

h2 {
  margin-bottom: 5px;
}

p {
  margin: 0 0 5px 0;
}


.container ul.first{
  margin-right: 30px;
  max-width:calc(100% - 30px);
}

ul li:not(:last-child) {
  display:contents;
}
ul li:not(:last-child)::after {
   content:"";
   width:25px;
}
<div class=container>
  <ul class=first>
    <li>first</li>
    <li>second</li>
    <li>third</li>
    <li>fourth</li>
    <li>fifth</li>
    <li>sixth</li>
  </ul>
  <ul class=second>
    <li>first</li>
    <li>second</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

Is there a way to assign a numerical value to the data attribute of the "p" tag?

As beginners in coding, we are introduced to various value types such as strings ("Hello"), booleans (true), and numbers (1). I am curious, how can I assign a number value to a <p> tag instead of a string? Is there a specific number tag that should ...

issue with arranging content in a two-column layout with headers

Currently working on my first website and running into some CSS issues. I'm aiming for a two-column + header layout that fills the entire screen space of the website. I have the following specifications in mind: Header takes up 20% of the screen he ...

Aligning a element at the center is not functioning properly when using margin: 0 auto;

I'm having trouble aligning elements to the center when applying margin: 0 auto. You can view the code here: https://jsfiddle.net/helloworld123/vhhx1o7n/ The goal is to align the elements in the center of the page (.entry-wrap should be centered) and ...

Use CSS to apply hover effects to multiple sections of text at the same time

Is there a way in CSS to apply the hover state to one part of HTML text when another part is hovered over, both sharing the same class? I have a block of text in HTML that is broken down into individual words, each word linked to a specific CSS class. It ...

Tips on using CSS to hide elements on a webpage with display:none

<div class="span9"> <span class="disabled">&lt;&lt; previous</span><span class="current numbers">1</span> <span class="numbers"><a href="/index/page:2">2</a></span> <span class="num ...

Add a hyperlink alongside every row in the table

My table comprises three columns in each row, and I am looking to include a link next to each row. <table> <th> Name: </th> <th> Price: </th> <th> Description </th> ...

Utilize xdebug and NetBeans 8.2 to troubleshoot PHP code submitted through an HTML form

After successfully setting up the XDebug extension to debug PHP code within NetBeans IDE (ver. 8.2), I encountered an issue. While debugging works fine when setting a breakpoint and running the script in debug mode, I am having trouble debugging PHP code t ...

The CSS attribute selector is unable to target dynamically appended class names

Utilizing React's CSS animations add-on has been a seamless process, with everything running smoothly when using the provided classnames. .quote-enter { opacity: 0.01; transition: opacity .25s ease-in; } .quote-enter.quote-enter-active { opaci ...

Issues with React JS and JavaScript filtering functionality not working correctly

I've been working on implementing a filter feature for a website, using an HTML range slider. However, I've encountered an issue where the values only update correctly when decreasing. For example, if I set the slider to $500, only products that ...

What is the best way to ensure my <h5> element fits perfectly inside its parent div vertically?

Currently facing an issue with finding a solution to my problem. The challenge involves having a header tag nested within multiple divs. Below is the structure: <div class="card"> <div class="layout-left"> <div class="card-header"> ...

What's causing my button to line up horizontally within a flexbox that is positioned vertically?

My button is aligned within the flex-box, shouldn't the elements be stacked in a column? I am working on a timer that features a row with two columns. One column contains an image while the other consists of a heading, a flexbox with four rectangles ...

Steps to directly reference a particular shape within an SVG illustration

I have a webpage with a large SVG image embedded in it. The image is too big to fit in the standard viewport, so there are horizontal and vertical scroll bars on the browser window. Within the SVG image, there are multiple rectangles. My goal is to create ...

Validating Empty Form Fields using Ajax in Codeigniter

Could someone please assist me in validating my form before submission to display an alert message stating 'Your field is empty' when there is no value entered? Your help is appreciated. Thank you. Function load_pagination_content() { ...

The navigation menu displays a flickering effect while transitioning between pages, specifically noticeable on Chrome

I am facing an issue with a website I am currently building. The navigation menu seems to flicker when transitioning from one page to another, but this problem only occurs in Chrome and IE. What's even more puzzling is that the flickering doesn' ...

Submitting a form in a JQuery dialog box

When users encounter a sign-in form, they are faced with two options: submit or cancel. If they choose to submit, they will proceed with the action. If they decide to cancel, a different outcome will occur. There seems to be a minor issue that might not b ...

Android Gmail Application: Implementing 1px horizontal spacing between inline images placed within <td> tags

Have you ever noticed that in the Android Gmail app, there can be a mysterious 1px gap between inline images? This little pixel problem can sometimes push the rightmost image outside the comfy frame of your email. It seems this glitch prefers images of cer ...

Is there a potential problem with unescaped characters in an HTML field within a JSON response?

Imagine an API returns a JSON response as shown below: {"error":"","data":"example_html_code_here"} Instead of showing "example_html_code_here", the actual HTML code or page is returned. Should this HTML code be es ...

The JSP file is not recognizing my CSS file

Having some trouble with my JSP files. The CSS file for my index.jsp seems to be missing, even though it's in the correct location. Check out this screenshot: https://i.sstatic.net/EoQEi.png I noticed that when I view the code in Chrome, the browse ...

Utilizing CSS percentage height when the parent element is constrained by a defined minimum and

It is a common rule that when applying a percentage height to an element, the percentage is calculated based on its parent's height. Consider a scenario where you want a child element to be 40% of its parent's height. The parent element has both ...

Interacting with a card in vuejs won't trigger any action

Is there a way to make an overlay disappear when clicking anywhere on the screen except for a specific card positioned on top of it? The current setup makes the overlay disappear even when the card is clicked. How can this behavior be corrected? <div ...