Arrange elements in a vertical layout and align them to the left when they exceed the boundaries of their container

I am seeking a solution for laying out my design, similar to the one shown in this image

Essentially, I have a container with a fixed height of 300px. I want to display a list vertically with each item taking up a width of 33%. If the list becomes too long, the overflowing items should float to the left.

.container {
  width: 940px;
  height: 300px;
  margin: auto;
  background: #dedede;
  margin-top: 30px;
}
.item {
  width: 31.2%;
  background-color: #B49AD9;
  margin-bottom: 10px;
  padding: 10px;
}
.item > div {
  display: inline-block;
  vertical-align: top;
}
.item ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
.alpha {
  font-weight: bold;
  font-size: 2em;
}
<h2>The C container should float to left and place next to A</h2>
<div class="container">
  <div class="item">
    <div class="alpha">A</div>
    <div class="words">
      <ul>
        <li>Alll</li>
        <li>Aoooo</li>
        <li>Auuuuu</li>
      </ul>
    </div>
  </div>
  <div class="item">
    <div class="alpha">B</div>
    <div class="words">
      <ul>
        <li>Blll</li>
        <li>Boooo</li>
        <li>Buuu</li>
        <li>Baaaaa</li>
        <li>Boppoe</li>
        <li>Buuuuu</li>

      </ul>
    </div>
  </div>
  <div class="item">
    <div class="alpha">C</div>
    <div class="words">
      <ul>
        <li>Clll</li>
        <li>Coooo</li>
        <li>Cuuu</li>
        <li>Coppoe</li>
        <li>Cuuuuu</li>

      </ul>
    </div>
  </div>
</div>

The purple box labeled C needs to be positioned beside A

Any suggestions on how to achieve this layout? CSS and JS solutions are both welcome :)

PS: Compatibility with at least IE9 is required... ...

Answer №1

One solution is to utilize flexbox:

.container {
  display: flex;          /* Enabling magic */
  flex-flow: column wrap; /* Creating a multiline column layout */
}

.container {
  width: 100%;
  max-width: 940px;
  height: 300px;
  margin: auto;
  background: #dedede;
  margin-top: 30px;
  display: flex;          /* Enabling magic */
  flex-flow: column wrap; /* Creating a multiline column layout */
}
.item {
  width: 31.2%;
  background-color: #B49AD9;
  margin-bottom: 10px;
  padding: 10px;
}
.item > div {
  display: inline-block;
  vertical-align: top;
}
.item ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
.alpha {
  font-weight: bold;
  font-size: 2em;
}
<h2>The C container should float to left and place next to A</h2>
<div class="container">
  <div class="item">
    <div class="alpha">A</div>
    <div class="words">
      <ul>
        <li>Alll</li>
        <li>Aoooo</li>
        <li>Auuuuu</li>
      </ul>
    </div>
  </div>
  <div class="item">
    <div class="alpha">B</div>
    <div class="words">
      <ul>
        <li>Blll</li>
        <li>Boooo</li>
        <li>Buuu</li>
        <li>Baaaaa</li>
        <li>Boppoe</li>
        <li>Buuuuu</li>
      </ul>
    </div>
  </div>
  <div class="item">
    <div class="alpha">C</div>
    <div class="words">
      <ul>
        <li>Clll</li>
        <li>Coooo</li>
        <li>Cuuu</li>
        <li>Coppoe</li>
        <li>Cuuuuu</li>
      </ul>
    </div>
  </div>
</div>

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

A guide on restricting overflow in React Spring Parallax 'pages'

Currently, I have integrated React Spring Parallax () into my project found at this link: https://codesandbox.io/s/parallax-sticky-scroll-2zd58?file=/src/App.js Upon clicking the button to navigate to the next section, it is noticeable that the image over ...

Comparing the impact of class and element selectors on CSS performance

Considering that CSS is read from right to left, I am pondering the balance between performance and readability when it comes to adding classes to HTML in order to more efficiently target elements in CSS. Additionally, I am curious about how using a gener ...

Tips for enhancing the flexibility of the owl carousel

I've been trying to make four items fit on a medium screen and two on a mobile device, but no matter what I do - adjusting widths, heights, columns, and responsive classes like col-6, col-md-3, col-lg-3 - nothing seems to work well. I could really use ...

Is there a way to use javascript to ensure that CSS variables stick even when overwritten by onclick events?

I have successfully implemented a method to change CSS variables using advice found here. However, I am running into an issue where the changes do not persist. After clicking on a navigation link, the styles momentarily switch to the new values but quickly ...

Is it possible to activate the identical drop-down or popover with multiple buttons?

Is it possible to activate the same drop-down menu with multiple buttons? For example, I want a button at the top of the page labeled special menu, another in the middle also labeled special menu, and one at the bottom as well labeled special menu. When a ...

Animating or transitioning CSS keyframes to an inline value when the page is initially loaded

In my current project, I am working on creating HTML and CSS-based bar representations of percentage values. The structure of the code is as follows: Using HTML: <div class="chart"> <div class="bar" style="width:43%"></div> </div&g ...

Text Module of the Divi theme

Issue Resolved (After noticing that Divi was adding padding twice to <ul>'s in their footer and Text Module, I created a new class to remove the padding-bottom from the sub-list item.) In a Divi text module, I created an unordered list with a s ...

Creating a hover effect on a specific area of a map is a useful technique

Can someone please help me create a hover effect for areas in maps? Below is the code for the map. Thank you in advance! Here is the image. <img src="[CR_kraje_1_úroveň_v1] (imported)" width="770" height="443" border="0" usemap="#map" /> &l ...

The functionality of my website is currently experiencing difficulties when accessed through the Android UC Browser

My website, , is experiencing issues with product loading and the '+' button in the side menu not working on UC Browser. It works fine on other Android browsers like Chrome and Firefox, but I am confused as to why it is not functioning properly o ...

Place the text below the adaptable div

Could anyone offer guidance on how to properly align text underneath responsive images within divs? The issue I'm facing is that the divs adjust smoothly based on the viewport size, but the text below them doesn't stay centered as intended. Whi ...

What is the method for choosing an Object that includes an Array within its constructor?

Is there a way to retrieve a specific argument in an Object constructor that is an Array and select an index within the array for a calculation (totaling all items for that customer). I have been attempting to access the price value in the Items Object an ...

What causes my words to link together, and what is the term for this phenomenon?

Is there a specific term for the way my font is changing automatically? How can I emulate this effect on other text? And how do I remove it if needed? https://i.sstatic.net/yIe2f.png ...

How can I create a single-page application that adjusts to different screen sizes effortlessly?

I am working on developing a single-page application that fits within one screen with the following structure: There is a fixed header that includes: Two columns (1/3 for left, 2/3 for right) : a logo on the left a breadcrumb on the right. The ...

Step-by-step guide to creating a pop-up div that appears on hover and remains visible when clicked

I'm trying to figure out how to make a popup appear when the mouse hovers over it and then stay visible when clicked on. The issue I'm facing is that currently, the popup disappears when the mouse moves away from it. How can I modify the code so ...

JavaScript - Modify the proposed content prior to inserting it into the input field

In my current project, I have implemented a feature using jQuery UI - v1.11.4, where an HTML textbox utilizes a JavaScript autocomplete functionality. The suggested string for the textbox is created by concatenating several columns (patient_no, patient_nam ...

An intriguing inquiry regarding HTML form intricacies

Looking to enhance the source code by adding a new column to display Client Mobile, Client Office Telephone, and Client E-mail in a separate popup PHP page. My attempt involved adding a form and submit button to generate the new column. However, pressing ...

supplying various color variables to a single class

I am working on a webpage with multiple elements sharing the same CSS class. I want each element to have a unique background color, but I don't want to create a bloated CSS file. Is there a way to achieve this by adding a parameter in my HTML like cla ...

How can I output HTML code using php echo without using quotation marks?

My issue involves printing out HTML code that is not stored as a simple string, requiring me to decode it before echoing. The problem arises when I echo the decoded value and unwanted quotes appear, disrupting the output. Here's an example: <div> ...

Exploring the world of CSS and designing layouts using multiple div elements

Upon reviewing this website, I must admit it is getting close to what I envision. However, being a perfectionist and new to HTML and CSS, I find myself struggling to achieve the desired outcome. The layout consists of two divs named topred and top yellow, ...

Web view that remains fixed regardless of resolution, compatible with both iPhone and iPhone4 devices

Looking to create a webview with one HTML and one CSS file that displays graphics at the same size, but in their native resolution. Currently, my webviews built for 320x480 appear sharp when scaled up (text and border-radius), but images are displayed at ...