What is the best way to design a list that expands across two columns on desktop screens and one column on mobile devices?

Can a layout like the one shown below be achieved using flexbox, or is CSS grid required?

I would like the list to display in two columns by default, but switch to a single column when the window is resized.

https://i.sstatic.net/KpL6T.png

Answer №1

To achieve element wrapping based on certain conditions, you can utilize the flex-wrap property in CSS. For example:

ul {
    display: flex;
    flex-wrap: wrap;
}
ul li {
    width: 100%;
    max-width: 300px;
}

In this scenario, the li elements will wrap when there is insufficient 300px space available, dynamically adjusting columns without the need for media queries.

By applying flex-direction: column and setting a specific height or max-height to the parent container, you can control how elements are wrapped based on the height of their container:

ul {
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
    max-height: 100px;
}

This setup will cause items to rearrange vertically as they reach the specified height limit:

 - One - Six
 - Two - Seven
 - Three
 - Four
 - Five

If you encounter issues with responsiveness, especially on smaller screens, you may need to adjust the max-height within a media query:

@media only screen and (max-width: 576px) {
    ul {
        max-height: none;
    }
}

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

struggles with concealing Div using CSS and Vue

I'm attempting to use CSS to hide a Div, but unfortunately, my attempts have been unsuccessful. This is the code I have been working on: Here is my VUE code: <div class="scroll-wrapper scrollbar-outer" style="position: relative;&qu ...

Empty array returned when using wrapper.classes() with Vue-test-utils and CSS modules

Recently I started using Vue Test Utils along with CSS Modules. My Vue component is all set up with CSS Modules and it functions perfectly in the app as well as in Storybook. Take my BasicButton for example: <template> <button :class="[ ...

Is it possible that the div's height is not being set to 0 pixels

<div style="height:0px;max-height:0px"> </div> It appears that setting the height of a div to 0 pixels is not having the desired effect. The div is still expanding to display its contents, so how can we stop this from occurring? ...

Switching Up Poster Images in Chrome with Afterglow HTML5 Video on Hover

I have been experimenting with the html5 video player from AfterGlow. In my sample video, I included a poster attribute. However, I noticed an issue when viewing the video in Chrome - whenever I hover over it, the poster image flickers, creating an undesir ...

Ways to execute a function when clicking on a date using HTML 5 datepicker input type

How can I call a function in angular js when a date is selected from an html5 date-time picker using a mouse click? I need help with this. Here is the HTML code: <div class="col-lg-4"> <input class="form-control" type="datetime" date-time auto ...

Automatically populate login credentials on a webpage using Javascript scripting

Can anyone provide insights on why the input credentials on a webpage cannot be autofilled with this code? Is there a solution to make it functional? Trying to automate login process using JavaScript: function Test() { var name = document.getElementBy ...

The issue lies in the alignment of the navbar at the top

After retrieving this script: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <nav class="navbar bg-light navbar-expand-sm justify-content-center"> <a class="navbar-brand" ...

I seem to be overlooking something. The calculation is not displaying in the designated field

Having trouble with my area calculator - the area field is not updating as expected when I enter numbers in each field. Any advice on what might be missing? function calculateArea() { var form = document.getElementById("calc"); var sLength = parseFl ...

I am looking to optimize my CSS and jQuery code for a dropdown menu

I have the following code that is currently functioning properly for a dropdown menu, but I need to make some modifications to the CSS and jQuery. I am looking to increase the size of the main box and decrease the width of the first row of icons. Howev ...

Bootstrap - aligning arrays of columns with varying widths

Struggling to align 5 varied-width Bootstrap columns in the center of the page? The new flex/grid features should make it possible, but something seems to be missing. Take a look at this image from the FF inspector for reference: https://i.sstatic.net/q4ut ...

Is it possible to reverse the effects of @media queries for min/max width and height?

I rely on http://www.w3.org/TR/css3-mediaqueries/ to customize my design based on the available viewport size. When I need to adjust the breakpoints, I struggle with negating @media (min-width: 500px), which overlaps with (max-width: 500px). To counter th ...

Please verify before submitting the form

I have developed a PHP page that is responsible for deleting data from my database. The PHP page is triggered upon form submission, which occurs when the user clicks on an "X" sign image. How can I prompt the user to confirm the deletion process before pr ...

Make sure the font size on your iPhone matches the font size on other web browsers

Once upon a time, I came across an insightful article detailing a method to size text using CSS that is compatible with multiple browsers. The approach outlined in the article includes: body { font-size:100%; line-height:1.125em; /* 16×1.125=18 * ...

What could be causing my WebMatrix project to continue using the previous favicon.ico file?

I recently encountered an issue with the default favicon.ico included in a WebMatrix product. In order to personalize it, I converted a jpg using an online image converter at . After renaming the original favicon.ico to YourMotherWearsCombatBoots.ico, and ...

What is the best way to adjust the placement of images and text within bootstrap 5's HTML code?

Hey everyone, I'm new to using bootstrap 5 and I've come across a little issue. If you take a look at this image https://i.sstatic.net/777BS.jpg In my webpage, the Google icon and all the text are currently aligned to the left, just like in th ...

What is the best way to find the longest element with the same class name using jQuery?

How can I dynamically find elements with the same length of class names? var className = $('.floor-4').attr('class').split(' ')[1]; var classLength = $('.' + className).length; Here is the HTML code: <div id="d ...

Swap the Text for a Button

I've been searching for a solution to this problem, but all I seem to find online is advice on how to "replace button text." What I'm trying to achieve is to have text change into a button when hovered over. I've attempted using fadeIn/fade ...

Unable to decipher Material UI class names

I am experimenting with a React app using Material UI to explore different features. I'm attempting to customize the components following the original documentation, but making some changes as I am using classes instead of hooks. However, I keep encou ...

Switching the arrow direction in Bootstrap 4 menu toggles

I'm a beginner with Bootstrap 4. My navigation menu includes a dropdown item with the standard caret pointing down, added automatically by Bootstrap. Now, I want to make the caret point up after opening the dropdown menu for the nav item. Despite ext ...

Implementing template loading on page load with state in AngularJS

When my application loads, I want the nested view list-patents.htm to be displayed. The main navigation is on my index.htm, featuring two nav items: transactions and patents. If you click on patents, two sub-menu items will appear: list patents and add p ...