What are some ways to employ ul and li elements to create columns that are consistently aligned, even when some columns may be empty?

In my HTML document, I have multiple sections with unordered lists of items. In a specific ul section, the list looks like this:

   <ul>
     <li>item 1</li>
     <li>item 2</li>
     <li>ck-item 2</li>
     <li>item 3</li>
     <li>item 4</li>
     <li>ck-item 4</li>
     <li>item 5</li>
     <li>ck-item 5</li>
   </ul>

I want to use CSS classes to format the list into two columns like this:

item 1
item 2     ck-item2
item 3
item 4     ck-item4
item 5     ck-item5

I have been struggling to achieve this for hours and any help would be appreciated.

EDIT: Another approach results in uneven alignment based on text length:

   <ul>
      <li>item 1</li>
      <li>item 2     ck-item 2</li>
      <li>item 3</li>
      <li>item 4     ck-item 4</li>
      <li>item 5     ck-item 5</li>
   </ul>

This method leads to messy output depending on text length, which is not ideal. My goal is to create evenly spaced columns regardless of text length.

Answer №1

If you're looking to create a grid layout, consider using CSS Grid.

Take a look at the code snippet and example below:

.ul-table {
  display: grid;
  grid-template-columns: max-content max-content;
  list-style-type: none;
  padding: 0;
  column-gap: 20px;
}

.ul-table li {
  border: dashed 1px red;
  white-space: nowrap;
}

.ul-table li.start-new-row {
  grid-column-start: 1;
}
<ul class="ul-table">
  <li class="start-new-row">item 1</li>
  <li class="start-new-row">long item 2</li>
  <li>ck-item 2</li>
  <li class="start-new-row">very very long item 3</li>
  <li class="start-new-row">item 4</li>
  <li>ck-item 4</li>
  <li class="start-new-row">item 5</li>
  <li>ck-item 5</li>
</ul>

Here's an explanation of the .ul-table style:

  • display: grid; sets up the element as a grid container.
  • grid-template-columns: max-content max-content;
    defines two columns based on their content width.
  • column-gap: 20px specifies the gap between columns.

And for the .start-new-row style:

  • grid-column-start: 1; indicates the start of a new row within the grid.

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

Fill in input field based on choice from the drop-down menu - JavaScript

I am facing an issue where I cannot add text inside the text box based on the dropdown selection. For example, if I select option2 from the dropdown, the textbox should be populated with option2. (function() { 'use strict'; setInterva ...

CSS for Centering Text and ImageAchieving the perfect alignment

I have a footer positioned at the bottom of my webpage containing some text along with a few PNG images. I am attempting to center everything within the footer. I tried using margin: 0 auto, but it didn't work due to the footer not being a block elem ...

Transforming a div into image data with the power of jQuery

I am facing a challenge with my HTML page. I have implemented a button that is supposed to convert the entire HTML page into an image when clicked. So far, I have used html2canvas for this purpose as shown below: html2canvas([document.getElementById(&apos ...

Guidelines on receiving a user's color input and showcasing it with JavaScript

/* Take a color input from the user and apply it to the variable called "message". */ <script type="text/javascript"> var textcol, message = "Great choice of color!"; textcol=window.prompt("Please enter a color:&quo ...

Enable vertical overflow to adjust automatically without needing to set a specific height

I have a unique table that consists of 3 main parts: the header the content the footer Using flexbox, I managed to make the "content" section occupy all available view height. However, this was not explicitly done and now when there are too many items in ...

The CSS property for restricting white-space to nowrap is not functioning as

Having a div with multiple child divs floating left can be tricky. To prevent them from breaking, setting them to display:inline-block and white-space:nowrap seems like the solution. However, in some cases this may not work as expected. If your goal is to ...

Malfunctioning jQuery Plugin

Currently attempting to implement a jQuery plugin to achieve a pagination effect similar to The plugin I came across is linked at Although the usage seems straightforward, I am encountering difficulties making it function correctly. Displayed below is t ...

Locator for finding compound text within a div class using Selenium WebDriver

I am struggling to select a specific button in the UI that shares similarities with other elements. Below is the code snippet for the button in question: <div class="ui green ok inverted button"> <i class="checkmark icon"></i> Yes </d ...

An unknown browserLink expression was encountered

I encountered an issue with " browserLink " that you can see below <div ng-show="optList.editing && optList.BANKRUPT_FLG != 'Y' && (optList.OPT != 'TF' || (optList.OPT == 'TF' && optLi ...

Embedding Google+ Sharing in an iframe

I'm currently working on a web application that needs to be compatible with PC, tablets, and mobile phones. I'm interested in integrating Google+ Sharing into the app. However, it appears that when using the url , there are issues with blocking ...

Issue with the app not redirecting properly at launch

Upon starting the app, I'm attempting to redirect the page if the user is already logged in. if (!isLoggedIn()) { mainView.router.loadPage({ url: 'index.html', ignoreCache: true, reload: false }); } else { $('#txtUserName').html(l ...

Submit the form using the GET method and update the URL

<form method=\"GET\" action=\"http://www.$domain/search/\"> <input type=\"search\" id=\"search\" name=\"search\" value=\"terms\"> </form> This form will be directed to the follo ...

Vue component retrieval on the client side

I am attempting to retrieve a component using Vue with cdn in order to dynamically re-render from a public project that contains a file named cash-sale.vue with the .vue extension. <script> export default { data(){ return { "rows&quo ...

What is the counterpart of `width: max(100%, fit-content)`?

In order for my div to adjust its size based on the content if the parent div is smaller than the content, and match the size of the parent otherwise, I have been trying to achieve the first scenario by using width: fit-content, and the second scenar ...

Adding a data attribute to a Material-UI Button: A step-by-step guide

I'm trying to include a custom data attribute in the Material-UI Button Component as shown here. Here's what I attempted: <Button inputProps={{ 'data-testid'='clickButton' }} > Click </Button However, this approach ...

How do I stop the Navbar style from resetting every time I switch between pages on an ASP.Net Core Web App?

I'm experiencing an issue with the styling refreshing on my navbar when I click a link to navigate to a new page. As a beginner in using ASP.NET Core webapp, I'm looking for a solution to fix this. Here is a video demonstrating the problem: Video ...

Incorporating a new text tag

Is there a way to add a text label for every circle that can be rotated and have its color changed? I'm looking for a solution that doesn't involve JSON data, but instead retrieves the necessary information from somewhere else. var w = 3 ...

Changes in browser size will not affect the height

Is there a way to smoothly change the height of the navigation bar when resizing the browser? Currently, it snaps to the new height when the width goes below or above 1000px. Any suggestions? nav.cust-navbar { -webkit-transition: height 2s; tran ...

CSS: positioning controls at opposite ends of a table cell

I need help with styling a button and textbox inside a table cell. Currently, the button is stuck to the textbox, but I want them positioned at opposite ends of the cell. How can I achieve this? <td style= "width:300px"> <asp:TextBox ID="Tex ...

The leaflet map has been shifted away from the div container

I've been attempting to display a leaflet map within a div tag, but unfortunately it keeps getting displaced from the tag. https://i.sstatic.net/4fNkj.png Below is my angular directive implementation: import L from 'leaflet'; import esri ...