Flexbox: Arrange two items horizontally without setting a maximum width

I'm not sure if it's possible with flexbox, but I am looking to create a list with two items displayed side by side where the width of each item adjusts based on the content. Here is an example:

<div class="items">
  <div class="item">Item 1 with text</div>
  <div class="item">Item 2 with more text</div>
  <div class="item">Item 3 with some text</div>
  <div class="item">Item 4 without text</div>
  <div class="item">Item 5 lorem</div>
  <div class="item">Item 6 ipsum</div>
  <div class="item">Item 7 with lorem</div>
  <div class="item">Item 8 with ipsum</div>
</div>

.items {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.item {
  align-self: flex-end;
  box-sizing: border-box;
  background: #e0ddd5;
  color: #171e42;
  padding: 10px;
}

.item:nth-child(even) {
  background-color: darkgrey;
}

Is there a way to ensure that always two items are displayed side by side regardless of how many list items are present? There are no outer containers limiting the width, and I prefer not to use float. Would using grid template columns offer a flexible solution?

The desired end result should look like this (but aligned to the right):

https://i.sstatic.net/FMIdi.jpg

Answer №1

Along with the solution provided by theAlexandrian, an additional approach is to include an inner wrapper using the inner div element styled as inline-block for customization such as padding.

When working with inline elements, similar to characters, they may have space between them. One technique to eliminate this space is by applying a negative margin. Here are some other alternatives:

  • How can I remove the space between inline-block elements?

It's important to note that since the width of the space is influenced by the font, adjustments to the negative margin might be necessary.


Stack snippet

.items {
  text-align: right;
}

.item {
  display: inline;
  margin-right: -4px;        /*  remove space between inline elements */
}

.item div {
  display: inline-block;
  background: #e0ddd5;
  color: #171e42;
  padding: 10px;
}

.item:nth-child(even) div {
  background-color: darkgrey;
}

.item:nth-child(even)::after {
  content: '\A';
  white-space: pre;
}
<div class="items">
  <div class="item"><div> Item 1 with text </div></div>
  <div class="item"><div> Item 2 with more text </div></div>
  <div class="item"><div> Item 3 with some text </div></div>
  <div class="item"><div> Item 4 without text </div></div>
  <div class="item"><div> Item 5 lorem </div></div>
  <div class="item"><div> Item 6 ipsum </div></div>
  <div class="item"><div> Item 7 with lorem </div></div>
  <div class="item"><div> Item 8 with ipsum </div></div>
</div>

Answer №2

If you're looking for an alternative to using float, I have a clever solution just for you! :)
We can utilize inline, block, :nth-child, and ::after.

Here's the code:

.item {
  display: inline;
}

.item:nth-child(2n)::after {
  display: block;
  content: '';
}
<div class="items">
  <div class="item">Item 1 with text</div>
  <div class="item">Item 2 with more text</div>
  <div class="item">Item 3 with some text</div>
  <div class="item">Item 4 without text</div>
  <div class="item">Item 5 lorem</div>
  <div class="item">Item 6 ipsum</div>
  <div class="item">Item 7 with lorem</div>
  <div class="item">Item 8 with ipsum</div>
</div>

This solution is straightforward, right?

Answer №3

Have you experimented with utilizing CSS columns for layout?

.column-layout {
  columns: 2;
}
<div class="column-layout">
  <div class="column-item">Item 1 containing text content</div>
  <div class="column-item">Item 2 showcasing more textual information</div>
  <div class="column-item">Item 3 providing some text details</div>
  <div class="column-item">Item 4 lacking textual data</div>
  <div class="column-item">Item 5 with lorem ipsum content</div>
  <div class="column-item">Item 6 displaying ipsum lorem content</div>
  <div class="column-item">Item 7 presenting lorem examples</div>
  <div class="column-item">Item 8 featuring ipsum samples</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

Create a fixed navbar while scrolling in Angular 5

I want to make a change to the menu position so that it becomes fixed when it reaches the top of the page. Here is my approach: import { Component, OnInit, Inject, HostListener } from '@angular/core'; import {Location} from '@angular/common ...

Cause a bootstrap column to have overflowing content rather than expanding

I've searched extensively for the solution to my problem, but haven't found a satisfactory answer yet. My goal is to create a location finder with a map on the right and a list on the left. With over 18,000 locations to search through, the list ...

Resolving problems related to window scrolling in javascript for implementing a "sliding door" style animation triggered by a scroll event

I am new to web development and I have been working on creating a portfolio website to learn. I have seen some websites with really cool effects on their landing pages and I would like to incorporate something similar into my site. Specifically, I am inte ...

Receiving an absence of string or a null value from a text box

My goal is to test the functionality of an <input> element by entering text into a textbox and then displaying that data in the console. However, I am encountering issues with retrieving and printing the content. Additionally, I am interested in test ...

Search the database using a single text field and then automatically fill in the remaining fields using PHP

Being a beginner in PHP and Ajax, I find myself quite lost in the process. I have set up a MySQL database and created several forms in PHP. One of the forms is supposed to display customer information with text fields, including a search field for phone nu ...

What could be the reason for my post container not functioning properly?

I am facing an issue with my HTML and CSS code. The container is not displaying as expected, specifically the green bar that should appear below the image. I suspect that the container's auto attribute is causing it to overlook the content inside. Can ...

Utilizing CSS for styling a specific class in a Joomla Module

Recently, I was working on a Joomla Module and came across an interesting issue. When inspecting the CSS using Firebug, I noticed that there were multiple nested divs within the module, each with its own class. One example of this structure looked like: & ...

Issue with CSS background color not extending to full screen in webkit browsers when using 960.gs grid system

When using 960.gs, I have created a login screen with a div for the login form that has a background image. This div is positioned below a header, which centers it in the browser window. In my CSS, I am setting the body color as follows: body { backgr ...

CDK virtual scroll problem occurs when scrolling down, items are displayed slowly and occasionally blank for a few seconds

In my project, I am utilizing Angular's virtual scroll feature. However, whenever I scroll down, the items load but flicker momentarily. I have attempted to use maxBufferPx and minBufferPx as well as adjusting positions to resolve this issue, but so ...

What steps should I take to incorporate Google AdSense advertisements onto my website?

After developing a website using HTML and PHP, I attempted to incorporate Adsense ads. However, upon inserting the ad code between the body tags, the ads failed to display on the site. How can I resolve this issue? ...

What are the steps for implementing the innerClass style on the searchBox within the appBase?

I'm currently incorporating the searchBox component from appbase io into my upcoming js web application, but I've been facing challenges in customizing the layout of both the search box and the list. Despite following the documentation which sug ...

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 ...

Enhancing navbar with a dropdown menu in CSS

Is there a way to add a dropdown menu to my navigation bar and make it visible when hovered over? Any help or support would be greatly appreciated. Here is the code I have on JSFiddle: http://jsfiddle.net/nbh2e15y/2/ The CSS: #nav { background: none ...

Issue with CSS background image not showing up in Internet Explorer 8

Do you have any suggestions for tweaking the CSS to accommodate the compatibility issue with viewing our intranet sites in IE 8? The CSS image background is not displaying properly. Please keep in mind that the HTML and CSS work fine in Chrome, as well as ...

Scrape data from websites where the main URL remains constant but the information in the table varies

If you take a look at this link, you'll notice that when the next page is selected, the table on the website gets reloaded with new content without any change in the URL. Even after inspecting the developer tools > Network > XHR, it's diffi ...

When using the `position: absolute` and `left: 50%` properties on content with a `width: fit-content`, the content gets wrapped in Windows,

Why does the below code behave differently on Windows and Mac? On Windows, the content wraps or the div occupies only 50% of the browser's width, causing the content to wrap if its width exceeds 50% of the browser's width. However, on Mac, it tri ...

Loading an HTML page using jQuery's .load() method that contains embedded JavaScript

I'm facing an issue with loading .html pages (including JavaScript) into a div element. Below is my div setup: <div class="content"></div> This is my php code: <?php if(!$_POST['page']) die("0"); $page = (int)$_POST[&a ...

Looking for assistance with fundamental HTML concepts

I'm struggling to make the navigation bar stretch to full height. As a beginner in HTML and CSS, I have limited knowledge about it. I have tried numerous solutions found on the internet but nothing seems to work. The styling and HTML code are provide ...

Utilizing jQuery to convert letter input values into English: a step-by-step guide

I am developing a basic web application using Jquery. In this application, I aim to restrict user input language. For instance, if a user enters the Russian letter б in the input field, I want to automatically convert it to an English character. Below is ...

What might prevent an onSubmit event from triggering the execution of "checkTheForm()"?

Despite consuming a substantial amount of information on the internet, I still find myself puzzled by why my code isn't functioning as expected. I acknowledge that there are numerous tutorials out there guiding me to use <form action="index.html" o ...