What causes the difference in behavior between Firefox and Chrome when it comes to multiple column rendering and the use of overflow

Recently, I came across an interesting issue in Firefox related to lists inside a div with columns:2 set.

In Firefox, when overflow:hidden is applied to the list, it stops rendering in 2 columns. However, in Chrome, the list continues to render in 2 columns regardless of the overflow setting.

Let's take a look at a simple example:

$('button').on('click', (e) => {
  $('ul').toggleClass('overflow')
})
div {
  columns:2;
}

ul.overflow {
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>toggle overflow</button>
<div>
    <ul>
        <li>Lorem ipsum dolor.</li>
        <li>Consequuntur, ab nostrum.</li>
        <li>Quibusdam, iure, fuga.</li>
        <li>Suscipit quisquam, vel.</li>
        <li>Assumenda architecto, adipisci!</li>
        <li>Molestias, nostrum ratione.</li>
        <li>Quaerat, eveniet, in.</li>
        <li>Illum, debitis, dicta.</li>
        <li>Tempore, placeat, ea.</li>
        <li>Amet dignissimos, maiores.</li>
        <li>Odio, eos, ullam.</li>
        <li>Modi libero, quis!</li>
        <li>Aliquid, commodi, voluptates.</li>
        <li>Aperiam, magni, vel.</li>
        <li>Vitae, minima dolorum!</li>
        <li>Quidem, corporis, dolorum.</li>
        <li>Autem, minima, sit.</li>
        <li>Adipisci, odio, numquam.</li>
        <li>At, dicta, hic!</li>
        <li>Odit, blanditiis voluptate.</li>
    </ul>
</div>

You can view the CodePen example here: https://codepen.io/BugHunter2k/pen/rNaVpGG

I'm curious - which rendering do you think is correct? And is there any workaround to make Firefox render the list in 2 columns even with overflow:hidden set?

Answer №1

Thierry's response offers a solution to rectify an issue, but inadvertently addresses the core problem you're encountering and incorrectly attributes blame to the wrong browser. Let's delve deeper into this:

To clarify, the functionality is actually correct in Firefox but erroneous in Chrome.

The scenario involves a div serving as a multi-column container, triggered by using columns: 2. This CSS property is essentially shorthand for the following:

div {
    column-count: 2;
    column-width: auto;
}

A multi-column container creates a new block formatting context, causing descendant elements' properties to be applied relative to the div rather than the webpage in this instance.

It's important to note that multi-column layouts possess a certain level of flexibility; they attempt to adhere to specified values while adjusting the columns if necessary to accommodate content or reduce them if there's extra space available. An example highlighting this behavior will be discussed later on in my explanation.

Within the div, you have an unordered list typically displaying items vertically. However, due to the previous application of columns: 2, the <ul> splits automatically into two columns to best fit the layout. Since no height or width properties are declared anywhere, the split occurs evenly resulting in a visual misalignment between both halves of the list due to default margin-top pushing down the first set of list items by 16px (the standard margin size for <ul>).

The reason why implementing columns: 2 within ul {} instead of div {} resolves your issue is because it segregates the <ul> itself into two columns instead of trying to force-fit a normal <ul> into a multi-column container. This adjustment ensures consistent formatting at the start of each column.

A default <div> dynamically expands based on its container's width and content height. Adding more items to the <ul> causes both sets of list items to grow in height accordingly, especially visible with a background-color set for the div.

In terms of flexibility, limiting the div's height to 150px results in the columns expanding beyond the div's bounds to accommodate all list items:

div {
    columns:2;
    column-rule: thin solid red;
    height: 150px;
    background: grey;
}

ul.overflow {
    overflow: hidden;
}
<div>
    <ul>
        <li>Lorem ipsum dolor.</li>
        <li>Consequuntur, ab nostrum.</li>
        <li>Quibusdam, iure, fuga.</li>
        <li>Suscipit quisquam, vel.</li>
        <li>Assumenda architecto, adipisci!</li>
        <li>Molestias, nostrum ratione.</li>
        <li>Quaerat, eveniet, in.</li>
        <li>Illum, debitis, dicta.</li>
        <li>Tempore, placeat, ea.</li>
        <li>Amet dignissimos, maiores.</li>
        <li>Odio, eos, ullam.</li>
        <li>Modi libero, quis!</li>
        <li>Aliquid, commodi, voluptates.</li>
        <li>Aperiam, magni, vel.</li>
        <li>Vitae, minima dolorum!</li>
        <li>Quidem, corporis, dolorum.</li>
        <li>Autem, minima, sit.</li>
        <li>Adipisci, odio, numquam.</li>
        <li>At, dicta, hic!</li>
        <li>Odit, blanditiis voluptate.</li>
    </ul>
</div>

This demonstration showcases the div auto-adjusting to container size and splitting into three columns as required by the number of list items present. The secret behind the toggle button's effect lies in the use of overflow: hidden;.

When overflow: hidden; is applied to an element, it generates a new block formatting context similar to declaring "reset my layout computation from scratch here; don't factor in what the containing block was doing." In the provided case, it instructs to ignore the CSS column layout from the preceding block formatting context and display the <ul> normally. Although Chrome fails to implement this feature correctly, it should ideally comply. Such discrepancies often result from flawed/incomplete implementations or decisions made by Chrome developers catering to perceived user preferences.

Furthermore, Thierry’s recommendation to resolve the uneven vertical alignment of the two segments in the <ul> involved applying column properties directly to the <ul>. While this specific objective could also be achieved by removing the margin-top property or setting it to zero, adopting this method accomplishes additional tasks:

  • Evaluating proper multi-column layout configurations for the <ul>, ensuring that the browser handles the list and its items appropriately within a multi-column layout context instead of forcing them into a multi-column container.
  • Implementing the columns property on the <ul> maintains consistency when creating a new block formatting context via overflow: hidden;, preserving the applied multi-column layout within the fresh block context established.

This revised approach ultimately aligns with your intentions, especially if the goal solely revolves around dividing an unordered list into multiple columns. If the aim encompasses various element types or multiple elements necessitating consideration for a multi-column layout, retaining the property on the parent <div> remains imperative.

Answer №2

Seems like a glitch with Firefox.

A workaround could be to set the columns: 2; property on the ul element instead of the div. This should also fix the issue of the second column not aligning properly with the first one.

Remember to include browser prefixes in your code as well. It often helps prevent potential bugs from occurring.

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

Attempting to create a JavaScript calculator from scratch

After spending hours typing and debugging my code for a school assignment, I have created a calculator that still isn't working. I'm hoping someone can help me find the errors in my code. function myStory() { window.alert ("Very doge"); } // ...

What is the best approach to implement pagination in the UI-Bootstrap Typeahead Directive?

In my current Angular Project, I am utilizing the UI-Bootstrap's Typeahead Directive for a specific purpose. However, I am encountering an issue when dealing with a large amount of similar data using this directive. It seems that displaying only the ...

Setting a fixed data value within a div for subsequent retrieval through a function

I found a helpful example that demonstrates how to convert numbers into words. You can check it out here. The function for converting numbers into words is implemented in the following HTML code: <input type="text" name="number" placeholder="Number OR ...

My webpage lacks responsiveness

I've recently put together an HTML page, but unfortunately, it's not responsive. My Code <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> ...

Avoiding Socket IO from timing out when the browser page is inactive or not the focused tab on Google Chrome, Mozilla Firefox, and Safari mobile browsers

I have developed a basic chat application using socket io. The functionality is quite similar to the socket io chat official demo. However, I am facing an issue where Socket io times out on all mobile browsers when the user minimizes the page, opens anothe ...

Improve the Popup to seamlessly elevate

In my project, I have implemented a pop-up dialog box that rises up from the left bottom corner as the user scrolls down the page. You can view it live at this link- However, I am facing an issue where the initial lift up of the dialog box is not smooth, ...

Managing the "Accept Cookies" notification using Selenium in JavaScript

As I use Selenium to log in and send messages on a specific website, I am faced with a cookie popup that appears each time. Unfortunately, clicking the accept button to proceed seems to be quite challenging for me. Here is an image of the cookie popup Th ...

What is the best way to vertically align a Material UI component to occupy the remaining space within a container

Issue with Material UI/Grids for Login Page As a newcomer to Material UI/Grids, I am currently working on creating a login page where the layout is split into two parts. The left side occupies 5 column spaces while the right side takes up 7 column spaces. ...

Is it possible for you to pinpoint the mistake in the code below?

I've been trying to locate an error in the function but have been unable to do so. As a beginner in this area, I would appreciate your patience. <html> <head><title>print names</title> <script langua ...

Is there an easy method to verify if the local storage is devoid of any data?

Query is named aptly; seeking to verify in a conditional statement. ...

highcharts-ng expands in flexbox without contracting

I've encountered an issue while trying to incorporate a highchart within a flexbox container. The chart expands along with the container without any problems, but it fails to contract when the container size decreases. My current setup involves angul ...

Showing formatted JSON in the view using ASP.NET MVC

Is there a method to modify JSON for presentation in the interface? I am looking for a way to automatically update my API documentation when adding new properties. It would be great if I could also apply CSS styling to certain elements. This feature is som ...

Attempting to align a banner image in the middle using CSS styling

I'm having some trouble getting the banner image to be centered on all screen sizes. It looks good on smaller screens, but on larger ones it seems to be shifting to the left. Here's what I've attempted so far: .banner { padding-top: ...

The right column in a relatively positioned layout descends as an element in the left column is revealed

Currently in the process of constructing a registration form for our website, everything is going smoothly with one minor hiccup. The form consists of two columns where users enter their information. Initially, when designing elements for the first (left) ...

Encountering an inexplicable "undefined" error in HTML after receiving an

(apologies for any misspelled words) Hey everyone, I hope you're having a great time. This is my first experience with ajax, and I'm trying to incorporate some elements (messages sent and received) into HTML using ajax. However, all I see is an u ...

"Triggering CSS changes with the click of a

I am developing a basic calendar application in PHP and I would like to dynamically change the style of the <td> block based on user interactions. Specifically, I want to implement a behavior where once the "menuclick" class is active, the other cl ...

Tips for positioning text on the left and right sides of a div in HTML styling

I am struggling with positioning two pieces of text within a div. Despite having some styling already in place, the text is currently displaying one after the other on the left-hand side. I want to position one piece of text to the left and another to the ...

What is the best way to extract content between <span> and the following <p> tag?

I am currently working on scraping information from a webpage using Selenium. I am looking to extract the id value (text) from the <span id='text'> tag, as well as extract the <p> element within the same div. This is what my attempt ...

Tips for designing the microphone appearance on the Google Chrome speech input interface

Google Chrome features an input control for speech recognition. If you want to know how to use it, check out this link. I'm looking to enlarge the microphone icon and possibly use a customized image for it. Increasing the width and height of the inp ...

Trouble with webpage design persists following recent modifications

Today, I decided to make some alterations on the header.php file of my WordPress website in order to tweak the flag icons displayed at the top of the page. However, when I refreshed the page, everything looked completely chaotic. Even after undoing all th ...