Stop the padding of list items from wrapping when displayed in a horizontal list

My goal is to create a horizontal unordered list (<ul>) with four columns in each row.
The number of list items (<li>) may vary and change during runtime.

Existing Code and Outcome

ul {
  -webkit-columns: 4; /* Chrome, Safari, Opera */
  -moz-columns: 4; /* Firefox */
  columns: 4;
  display: block;
}

ul li {
  margin: 20px ;
  padding: 10px;
  text-align: center;
  color: white;
  display: list-item;
} 

But the current setup produces this result: https://i.sstatic.net/0TWQU.png

The list appears somewhat horizontally, yet the second element (Green) is positioned below the first one (Blue) and beside it. However, for my purpose, the order of elements doesn't really matter.

The issue lies in the fact that the padding of the element extends into the next column.

Any recommendations on how to address this?

View JSfiddle link here

Answer №1

Check out the code snippet below for a solution: Demo

<ul id = "list">
 <li style="background-color: rgb(51, 70, 115);">1</li>
 <li style="background-color: rgb(156, 78, 129);">2</li>
 <li style="background-color: rgb(121, 159, 59);">3</li>
 <li style="background-color: rgb(51, 70, 115);">4</li>    
 <li style="background-color: rgb(51, 70, 115);">5</li>
 <li style="background-color: rgb(121, 159, 59);">6</li>
 <li style="background-color: rgb(156, 78, 129);">7</li>
 <li style="background-color: rgb(156, 78, 129);">8</li>
</ul>

 #list {
    -webkit-column-count: 4;
       -moz-column-count: 4;
            column-count: 4; /*3 is just placeholder -- can be anything*/
}
#list li {
    display: inline-block;
    margin-bottom: 20px;  
        padding:10px 80px;
}

Answer №2

To achieve this effect, you can utilize the break-inside property in your CSS.

    ul {
     -webkit-columns: 4;
     /* Chrome, Safari, Opera */
     -moz-columns: 4;
     /* Firefox */
     columns: 4;
     display: block;
     list-style: none;
      

   }
   
  ul li {
     margin: 20px;
     padding: 10px;
     text-align: center;
     color: white;
     min-height: 50px;
     display: list-element;
     -webkit-column-break-inside:avoid; 
     -moz-column-break-inside:avoid; 
     -o-column-break-inside:avoid; 
     -ms-column-break-inside:avoid; 
      column-break-inside:avoid;
   }
<ul>
  <li style="background-color: rgb(51, 70, 115);">1</li>
  <li style="background-color: rgb(121, 159, 59);">2</li>
  <li style="background-color: rgb(133, 50, 104);">3</li>
  <li style="background-color: rgb(75, 93, 135);">4</li>
  <li style="background-color: rgb(151, 187, 93);">5</li>
  <li style="background-color: rgb(156, 78, 129);">6</li>
</ul>

Answer №3

Here's another way to approach the problem using JavaScript and Flexbox. First, group every 4 li elements within a wrapper element, then utilize Flexbox to create a layout with 4 columns.

$('ul').each(function(){
    var divs = $('li', this);
    console.log(divs)
    console.log(divs.slice(0,4));
    for(var i = 0; i < divs.length; i+=4) {
        console.log(i)
        divs.slice(i, i+4).wrapAll('<li class="wrapper"><ul></ul></li>');
    }
});
ul {
  padding: 0;
  margin: 0;
}
   
.wrapper ul {
  display: flex;
  flex-direction: row;
}

.wrapper ul li  {
  flex: 1;
  margin: 10px;
  padding: 10px;
  color: white;
  min-height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li style="background-color: rgb(51, 70, 115);">1</li>
  <li style="background-color: rgb(121, 159, 59);">2</li>
  <li style="background-color: rgb(133, 50, 104);">3</li>
  <li style="background-color: rgb(75, 93, 135);">4</li>
  <li style="background-color: rgb(151, 187, 93);">5</li>
  <li style="background-color: rgb(156, 78, 129);">6</li>
  <li style="background-color: rgb(75, 93, 135);">4</li>
  <li style="background-color: rgb(151, 187, 93);">5</li>
  <li style="background-color: rgb(156, 78, 129);">6</li>
  <li style="background-color: rgb(51, 70, 115);">1</li>
  <li style="background-color: rgb(121, 159, 59);">2</li>
  <li style="background-color: rgb(133, 50, 104);">3</li>
  <li style="background-color: rgb(75, 93, 135);">4</li>
  <li style="background-color: rgb(151, 187, 93);">5</li>
  <li style="background-color: rgb(156, 78, 129);">6</li>
  <li style="background-color: rgb(75, 93, 135);">4</li>
</ul>

Answer №4

By incorporating the min-height property into the styling of the <li> element, you may find a resolution to your issue:

ul li
{
      margin: 20px ;
      padding: 10px;
      text-align: center;
      color: white;
      min-height:20px;
} 

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

Guide on using bootstrap collapse feature with two buttons sharing the same data-target, ensuring that the collapsible element does not collapse if it is already

I have successfully implemented two buttons that collapse and show the same card. However, I am now seeking a solution to prevent the card from collapsing if it is already shown by one button. When the card is visible, both buttons should be able to show ...

The variable "input" was intended to hold the value entered into the input field, however, it is currently storing

Could you please take a look at the code provided below? The issue I am facing is that "The variable input is supposed to hold the value of an input field, but instead it ends up storing 'undefined'." var input = $("#input-item").val(); var coun ...

Eliminating the standard borders on a pop-up window

When I utilize window.open to open a POPUP Window, the default title blue color borders appear rounded around the window as shown in the image here. Is there a way to remove these borders using CSS or any other option without having to use Light box? ...

Adding input fields that change dynamically along with their corresponding radio buttons using AngularJS

Hello, I am struggling with implementing a feature in my HTML and AngularJS files (using Bootstrap ui). I want to create a new input field (td) along with three corresponding radio buttons when the user clicks on an add button. I have attempted to modify t ...

Incorporated new code into the website, functioning properly, although it appears to keep scrolling endlessly below the footer

My website is experiencing a strange issue where the page seems to keep extending beyond the footer due to some JS code. I am not very familiar with JavaScript and have tried different solutions without success. I have searched extensively online for a so ...

Preventing click events with pointer-events property in CSS while still allowing scrolling

Is there a way to use pointer-events: none to disable click events on a specific container while still allowing scroll functionality and an overlay? You can view my fiddle here: https://jsfiddle.net/1eu6d3sq/1/ Currently, when I apply pointer-events: non ...

Steps for adding hover color to a div with linear gradient border

My current challenge involves adding a hover color to a specific div, but I'm facing obstacles due to the gradient background that was implemented to achieve border-radius functionality. This task is being carried out within a React Component using c ...

SwipeJS experiencing technical difficulties

My Swipe.Js version : "^7.0.2" Today, I attempted to use Swipe.Js and encountered an issue with my import code. import { Swiper, SwiperSlide } from 'swiper/react'; as described on https://swiperjs.com/react#installation. However, when ...

A mobile phone screen cannot be fully filled by an image

I'm working on a way for an image to cover the entire screen of a mobile phone. However, when I preview it on an iPhone through a laptop, the image only covers a small portion of the iPhone's screen rather than filling it completely. Below is the ...

When extracting a value from an HTML textarea that contains multiple lines of text inputted by the user, the JSON becomes invalid

Here is the JSON format I am working with: { questionID: int studentAnswer: "String" } The issue I am facing is that the studentAnswer field is currently only capable of holding input from a single line in an HTML textarea. If I input text that spa ...

Does the file extension hold significance in relation to a MIME type? Are there other implications for a server?

How crucial is the file extension? As an example, I have a CSS style sheet named "hi.js" and then use it while linking with its MIME type as text/CSS. Would this be effective? Is it considered against any rules? Edit: It was just a harmless query, no int ...

How can I restrict input to only numbers and one decimal point using JavaScript?

The following code I have is not functioning as expected: function validateInputValue(element) { var regex = /\D$/i; // Important: avoid white spaces in password if(regex.test(element.value)) { alert("Please check your input format"); e ...

Repeated information displayed in modal pop-ups

HTML <a class="btn" data-popup-open="popup-1" href="#">More Details</a> <div class="popup" data-popup="popup-1"> <div class="popup-inner"> <h2>Unbelievable! Check this Out! (Popup #1)</h2> ...

Cannot see the toggler button in Bootstrap 4 Navbar

I recently implemented the toggler button (hamburger icon) code in my HTML, but I'm facing an issue where the hamburger icon doesn't appear and looks blank when I resize my screen or view it on a smaller viewport. Here's the snippet of my H ...

Guide on adjusting padding for jQuery Flot graph for improved styling

I am utilizing jQuery Flot to generate a basic graph. I do not want the axes to display on either side, but this is causing the points to be slightly cut off. Any suggestions on how to resolve this issue? $.plot('#'+id, [ { data: data.values ...

Add the attribute's value to an array

$(document).ready(function() { function randomColor() { return 'rgb(' + Math.round(Math.random() * 255) + ', ' + Math.round(Math.random() * 255) + ', ' + Math.round(Math.random() * 255) + ')' ...

Enable the expansion of a div by dragging and dropping an image onto it

Take a look at this fiddle. In this fiddle, I am able to drag and drop images onto the .drop-zone. However, I would like to enhance it so that when I drag an image onto it, the .drop-zone div expands to where I place the image. It's okay if the expand ...

Creating visually appealing tables in React.js

Having trouble with table rendering in React. The buttons in the table cell are not styled properly and do not center within their div. Additionally, the table border is cut off where there are buttons or empty headers. Need help figuring out what's g ...

modify the inherent CSS property

I am working with a component that I have inherited, including its CSS style, and I need to modify one of its properties. Current CSS: .captcha-main-image { width: 100%; height: auto; } <img class ="captcha-main-image" id="captchaImage" src= ...

Issue with Fancybox Buttons not appearing on parent page upon opening

I previously asked for help on how to open fancybox from an iframe to the parent page. The solution worked perfectly and is still functioning well. Now, I've been requested to add left and right arrow buttons, but it seems that using the code to open ...