cssalignmentcss-multicolumn-layout

CSS multi-column layout of list items doesn't align properly in Chrome


I am building a menu system presented to the user in multi-column format. The column-count property in CSS3 gets me 90% of the way there, but I'm having difficulties with alignment under Chrome.

The menu is relatively simple:

The alignment issue I'm having is most noticeable with a top-border and some background coloring on each list item. In Firefox, the list items are always aligned cleanly across each column, never bleeding into the previous/next column. In Chrome, alignment is a crapshoot, varying with how many list items are present and any padding/margin properties.

I've posted the code for a simple test case here: http://pastebin.com/Ede3JwdG

The problem should be immediately evident: in Chrome, the first list item in the second column bleeds back into the first column. As you remove list items (click on them), you can see that alignment breaks down further.

I've tried tweaking the padding/margin for the list items to no avail: Chrome appears to have a flawed algorithm for how it flows content across a multi-column layout.

The primary reason I haven't ditched column-count altogether (in favor of manual generation/Columnizer/etc.) is that the menu system also involves drag-and-drop functionality across multiple sub-menus, and having the menu data laid out as a cohesive list-based hierarchy makes for clean code.

Is there a way to fix the alignment issue in Chrome or should I just give up on column-count for now?

ADDED:


Solution

  • You need each item in the column to be displayed as "inline-block". That will solve your problem without needing to use jQuery.

    Additionally, each element can be specified to have width: 100% in order to get the them to use the full width of the rows.

    Here is a working example:

    $(document).ready(function() {
        for( var i = 0; i < 24; i++ ) {
            $("ul.newslist").append("<li><a href='#'>Item</a></li>");
        }
        $("ul.newslist > li").click(function() {
            $(this).remove();
        })
    });
    ul.newslist {
        columns: 5;
        background-color: #ccc;
        padding: 16px 0;
        list-style: none;
    }
    
    ul.newslist > li {
        display: inline-block;
        width: 100%;
        border-top: 1px solid #000;
    }
    
    ul.newslist > li > a {
        display: block;
        padding: 4px;
        background-color: #f6b;
        text-decoration: none;
        color: inherit;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <ul class="newslist"></ul>