Using CSS to divide HTML lists into evenly sized portions

My list consists of 8 items, formatted as follows:

<ul>
  <li>List Item 01</li>
  <li>List Item 02</li>
  <li>List Item 03</li>
  <li>List Item 04</li>
  <li>List Item 05</li>
  <li>List Item 06</li>
  <li>List Item 07</li>
  <li>List Item 08</li>
</ul>

In this list, I am looking to maintain a fixed width space between item 04 and 05. The first four items should be aligned to the right of that space, while the last four should be aligned to the left, similar to the image I have provided below.

https://i.sstatic.net/UQTzQ.png

I attempted a solution using the following code snippet:

ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
}

ul li:nth-child(4) {
  margin-right: 10em;
}

Although it appears to work, there is an issue when the list contains 4 or 6 items as they should be grouped in 2s or 3s respectively.

If anyone has a CSS-only solution for this problem, I would greatly appreciate your input.

Thank you.

Answer №1

It's fascinating!

Discovering the middle one to perform a task without using JavaScript is quite intriguing.

To achieve this, I utilized odd and even numbers to split the elements in half, then employed a pseudo-element to create the middle element.

Check out the code snippet below.

    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    ul {
      list-style: none;
      padding: 0;
      margin: 0;
      display: flex;
      justify-content: center;
      width: 60%;
      margin: auto;
    }
    li{
      width: 10em;
      border: 1px solid #000;
    }
    ul li:nth-child(odd) {
      order: 0;
    }
    ul li:nth-child(even) {
      order: 2;
    }
    ul::after {
      content: '';
      display: block;
      width: 10rem;
      order: 1;
    }
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
    <li>item4</li>
    <li>item5</li>
    <li>item6</li>
    <li>item7</li>
    <li>item8</li>
  </ul>

=============================================================

JavaScript Implementation

    let num = document.getElementsByTagName('li');
    let half = num.length / 2;
    let li = document.createElement('li');
    num[half].parentNode.insertBefore(li, num[half])
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    ul {
      list-style: none;
      padding: 0;
      margin: 0;
      display: flex;
      justify-content: center;
      width: 60%;
      margin: auto;
    }
    li{
      width: 10em;
      border: 1px solid #000;
    }
    li:empty{
      border: none;
    }
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
    <li>item4</li>
    <li>item5</li>
    <li>item6</li>
    <li>item7</li>
    <li>item8</li>
  </ul>

================================================

jQuery Approach

var li = $('ul li');
    $($('<li>')).insertBefore(li[li.length / 2])
* {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    ul {
      list-style: none;
      padding: 0;
      margin: 0;
      display: flex;
      justify-content: center;
      width: 60%;
      margin: auto;
    }

    li {
      width: 10em;
      border: 1px solid #000;
    }

    li:empty {
      border: none;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
    <li>item4</li>
    <li>item5</li>
    <li>item6</li>
    <li>item7</li>
    <li>item8</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

Empty space under the banner in Wordpress theme Avada

I can't seem to locate the CSS class for a blank space that appears below the header on one of my pages. This space is situated between the header and "SERVICIOS 24 HORAS". Any ideas on how I can remove this mysterious gap? My website is built wit ...

Make sure to always target a specific DIV element, regardless of whether any of its child divs are

I have a div named "box" nested inside another div called "container." When I click on the box, I am able to retrieve its ID perfectly. However, when I click on the container, I want to obtain the ID of the box instead of the container. Is there a way fo ...

Combine two columns into a single table column using HTML/CSS and JavaScript with the help of Datatables

I utilize the DataTables library from https://datatables.net/ to create sortable and searchable tables on my website. However, my table becomes too wide due to the numerous columns it contains. I am looking for a way to condense the width by merging relate ...

The background image fails to display properly on the server in Next.js

Hello, I'm currently using NextJs and I have a challenge. I want to set a background image on the body element that is rendered server-side. Below you'll find my code snippets: First, here is my App.js: import BodyStyle from '@components/Bo ...

Using JavaScript to manipulate an HTML canvas

I currently have an index.hTML file that is set up to display a canvas using the following code: <body> <canvas> </canvas> <script src="./JS/index.js" type="module"></script> </body> Within my JavaScript fi ...

Ways to initiate a new animation once another one has concluded

I am looking to initiate a jQuery animation once another one has completed. Initially, there is a slide down effect towards the second navigation bar: $(".navigation-bar1").click(function() { $('html,body').animate({ scrollTop: $(". ...

Jquery Not Responding to HasChanged Function

I am a beginner and looking for some guidance. I am attempting to develop a single page app using Jquery and AJAX, but unfortunately nothing seems to be working; no errors or any other indication. There are three hyperlinks with hrefs: #/main, #/second, # ...

Having trouble passing data between view controllers

In my AngularJS application, I have a table column in main.html that is clickable. When clicked, it should redirect to a new customer page with the value of the column cell as the customer's name. There is a corresponding service defined for the app m ...

Tips for resizing a browser window using an HTML element

I decided to add a unique feature to my website - a handle in the bottom right corner that users can grab and drag to the left to see how the design adapts to different browser window sizes (Responsive Design). To implement this, I included the following ...

Determine the width of invisible images using JavaScript/jQuery

Please take a look at the jsFiddle Every time I run it, I am trying to retrieve the width of the images, but the values I get are as follows (check in the JS console) 400 0 575 533 0 ... Some values are zero, and I can't figure out why. It seem ...

Is there a way to position the drop-down menu in front of the slider?

Hey there, I'm currently experiencing some issues with the drop-down menu on my index page. The drop-down items are hidden below my image slider, which is directly underneath my navigation bar. I would really appreciate any help in fixing this so that ...

Animating slides with CSS Keyframes and React, incorporating toggle slide fade out and slide fade (back) in

I am seeking a solution for toggling a box (div) with slide-out animation and then sliding back in animation (in reverse) upon button press. My requirement is to have no animations during the initial page render. While I can successfully slide the box to ...

Is there a way to fake a delayed reload when the webpage contains an audio element?

I have included an audio on my page which is causing it to load slowly. Below is the code snippet I am using: <div style="margin-left: 160px;"> <audio controls id="audio"> <source src="" type=&q ...

Unable to adjust the padding of the material UI Select component

I'm having trouble adjusting the padding of the Select component in order to align it with the size of my other text fields. Despite knowing that I need to make changes to nested components, I have yet to discover a viable solution. <div className ...

Learn the process of using calc to rotate images on hover with CSS and Jquery

Is there a way to implement the rotate on hover function for images, similar to what is seen on this website under 'our Latest Publications'? I attempted using calc and skew, however, I was unsuccessful in achieving the desired hovering effect o ...

When the text starts to overlap the column completely at approximately 700 pixels wide

Previously, everything was working fine until it suddenly broke. The column system switches to a 100% width single column at 1500px, but for some reason, the text overflows off the screen around 700px and I can't figure out why. The text at the bottom ...

I encountered an issue while iterating through Object HTMLDivElement and applying ChileNode style z-index, resulting in an undefined output in both Firefox and Chrome browsers

function adjustPosition(currentDiv) { try { for (var i = 0; i < document.getElementById("parentContainer").childNodes.length; i++) { document.getElementById("parentContainer").childNodes[i].style.zIndex = 0; ...

Arrange one <div> to the left and one <span> to the right with justified spacing in between

I am currently working on aligning the label tag, text, and submit input types to the left side of the page, while also ensuring that the 17 total orders are aligned to the right. The goal is to have them vertically aligned with the Date label and horizont ...

Utilizing a CSS/HTML div grid to mirror a 2D array in JavaScript

Currently, I am working on a personal project that involves creating a grid of divs in HTML corresponding to a 2D array in JavaScript. However, I am uncertain about the most effective way to accomplish this task. Specifically, what I aim to achieve is tha ...

Using either the <h1> or <p> tag within my <div> element disrupts the entire layout

Whenever I add a heading tag or paragraph tag within my div, it causes the content to drop down the screen significantly. I am unsure of the cause and feel a bit overwhelmed by it. I attempted to wrap the <h1> tag in a <div>, as shown below: & ...