Can you create an HTML list that displays multiple columns with a fixed number of items in each column?

Searching through different websites, I found a solution for creating a multi-column list at . It worked perfectly.

My question is this: Is it possible to display a dynamic list in two columns, with the first column always containing eight items (if there are more than eight) and the remaining items in the second column? Can this be achieved using CSS/HTML alone or would JavaScript/jQuery be necessary? I'd prefer not to use multiple plugins if possible.

Any advice or guidance on this matter would be greatly appreciated. Thank you.

Answer №1

Consider this example of an HTML page content:

<ul>
    <li class="item1">A</li>
    <li class="item2">B</li>
    <li class="item3">C</li>
    <li class="item4">D</li>
    <li class="item5">E</li>
    <li class="item6">F</li>
    <li class="item7">G</li>
    <li class="item8">H</li>
    <li class="item9">I</li>
    <li class="item10">J</li>
    <li class="item11">K</li>
    <li class="item12">L</li>
    <li class="item13">M</li>
    <li class="item14">N</li>
    <li class="item15">O</li>
</ul>

Now, utilize JavaScript to dynamically generate the necessary CSS for layout styling:

/*
    unorderedList: the ul element in the HTML
    columnSize:    number of items per column
    columnGap:     space between columns in ems
    offset:        initial margin left offset in ems
*/

function createMultiColumnLayout(unorderedList, columnSize, columnGap, offset) {
    var items = unorderedList.getElementsByTagName("li");

    for (var i = 0, l = items.length; l--; i++) {
        var itemStyle = items[i].style;
        itemStyle.lineHeight = "1.2em";

        if (!i && i % columnSize == 0) {       // current column
            itemStyle.marginLeft = offset + "em";
        } else {                          // new column
            offset += columnGap;
            itemStyle.marginLeft = offset + "em";
            itemStyle.marginTop = -columnSize + "em";
        }
    }
}

window.onload = function () {
    var items = document.getElementsByClassName("item1");

    for (var i = 0, l = items.length; l--; i++) {
        createMultiColumnLayout(items[i].parentElement, 8, 10, 5);
    }
};

Answer №2

Have you considered utilizing a table for this task? It's possible to generate the table using Javascript and manipulate the DOM as needed.

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

Numerous reflections of a singular variable

My question is about a variable called $comments. Currently, every time I submit a new comment, it just replaces the old one. How can I make it so that each time I click submit, it creates a new line for the new comment instead of overwriting the previous ...

Utilize the identical mathematical equation across various jQuery values

I have a JavaScript code that calculates the size of circles on the screen based on multiple variables. Currently, I am repeating the same equation for each circle by numbering all the variables accordingly. Is there a way to simplify this process and run ...

Guidance on sending Ajax response to JavaScript function in OctoberCMS

In the HTML file of my component, I have created a form. When I submit the form, it triggers an AJAX call to an action defined in the component. Currently, my code is functioning properly and I am receiving a response. However, I now need this response to ...

Is there a way to trigger a Random Number function once the <div> element returns to its initial position?

For this single serving site, the concept involves swiping a card over a specific area to receive different messages depending on interaction with the droppable space. I am looking to implement an if else function that utilizes a random number variable to ...

In CSS and HTML, links located behind a divider are unable to be clicked

I am currently facing an issue with my code where the anchors on the left and right (behind the middle anchor) are not clickable. I have attempted to use z-index to bring only the anchors in front while leaving the divider in the back so that the middle an ...

Is it possible to include a shared helper text for two textfields that have been imported from MUI in a React.js project?

This snippet contains the code for my password container: .password-form-container { width: 85%; height: 7%; position: relative; top: 230px; left: 40px; display: flex; justify-content: space-between; border: 1px solid red; } <div clas ...

Define the format for the output file name in TypeScript

I am trying to change the filename of a TypeScript generated js file. How can I accomplish this? For instance, I currently have MyCompany.ClassA.ts The default output filename is MyCompany.ClassA.js However, I would like the output filename to be MyComp ...

Preventing an object from exceeding the boundaries of its designated space

My DIV is filled with user-generated content, which sometimes begins with an item that has a margin-top that overflows the container, creating a gap between it and preceding elements. I've discovered that changing the display property to either inline ...

Determining the appropriate generic type in Typescript

In my code, there is a method designed to extend an existing key-value map with objects of the same type. This can be useful when working with database query results. export function extendWith< T extends { id: string | number }, O = | (T[" ...

Utilizing jQuery for animating SVG elements with dynamic color changes and scaling effects upon hover

Seeking assistance from coding experts! I have created an icon and am attempting to modify it so that the color changes when hovered over. Additionally, I want the white square to scale down by 50% starting from the top-left corner of its current position. ...

Issue with LavaLamp Menu in Internet Explorer 8 - Loosely displayed vertical and horizontal guidelines surrounding the translucent gray oval

Kindly view the provided link in both Internet Explorer 8 and Firefox. LavaLamp Plugin - jQuery The orange lavalamp display works fine in Firefox. However, in Internet Explorer 8, there are certain guidelines (vertical and horizontal guidelines around t ...

Accessing the parent element within a hover declaration without explicitly naming it

I am facing a challenge with three nested divs, where I want to change the color of the children when the parent is hovered. However, I prefer not to assign a specific class name to the parent element. The issue arises because the children have their own ...

In the realm of Typescript Angular, transferring the value of an object's property to another property within the

I'm working with a large TypeScript object and I am hoping to automate certain parts of it to streamline my workflow. myObject = [ { id: 0, price: 100, isBought: false, click: () => this.buyItem(100, 0) } buyItem (it ...

Can data be transferred from one website to another without the use of an API?

I am a newcomer to the world of web development with basic knowledge of JS, Node, Express, Mongoose, and MongoDB. I have an exciting idea for a web application that would function like a spreadsheet, gathering user inputs to generate an output. The unique ...

HTML div feedback container with directional pointer

I've been working on creating a comment box with an arrow using CSS, but I'm facing a particular challenge. My comment box has a white background, and in order to make it stand out, I need to add a border. However, I'm struggling with addin ...

After one iteration of the loop, the jQuery variable is undefined

Currently, I am executing a loop that examines the content of div elements. These divs contain small puzzle pieces with data retrieved from XML files. I have a variable that iterates through div #1A to #4A. Subsequently, I use the same variable to enter ea ...

How can I specifically target and count children from a certain class using CSS selectors?

Consider this HTML structure with items arranged at the same level: <div class="h2">Title: Colors</div> <div class="pair">Hello world (1)</div> <div class="pair">Hello world (2)</div> <div class="pair">Hello wo ...

Attempting to implement a single toggle for numerous div elements

Concerning the answer provided in this query: How to slide down a div then .fadeIn() the content and vice versa? In the js fiddle shared, the jQuery code is specifically designed for one of the divs. If there are multiple divs, how can the jQuery code be ...

Is there a way for me to add a clickable link within a tooltip?

In my title, I want to insert a clickable link like 'Link' or 'a'. The title is ready for any string you input. <MaterialSwitch title={CLICKABLE STRING HERE} /> ...

Implementing Google APIs with Next.js: Managing arrays without keys

Recently, I've started using next.js and came across the getStaticProps function. In this function, I'm loading data from a Google Sheet table into a constant. The result is an array of arrays, just like what the Google API returns shown below: [ ...