Stop floating divs from wrapping incorrectly

Each of these inner divs share the same width, but their height varies. The outer div is fluid, so it's unpredictable how many will fit on each row. I'm seeking a solution to prevent unwanted line wraps, similar to this visually appealing example:

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

Is there a CSS-only way to achieve this without the need for Javascript? Below is my current implementation:

.outer_div {
    overflow: hidden;
    text-align: left;
}
.inner_div {
    float: left;
    width: 200px;
}

Answer №1

To achieve the desired layout, you can utilize CSS flexbox on the parent container and enable content wrapping by using flex-wrap: wrap.

For demonstration purposes, here is a sample implementation using ES6 to generate random heights for individual boxes:

const elements = Array.from(document.querySelectorAll('.inner_div'));

elements.forEach(element => {
  element.style.height = `${Math.round(Math.random() * 100) + 50}px`;
});
.outer_div {
  display: flex;
  flex-wrap: wrap;
}

.inner_div {
  width: 200px;
  margin: 5px;
  height: 50px;
  background-color: steelblue;
}
<div class="outer_div">
  <div class="inner_div"></div>
  <div class="inner_div"></div>
  <div class="inner_div"></div>
  <div class="inner_div"></div>
  <div class="inner_div"></div>
  <div class="inner_div"></div>
  <div class="inner_div"></div>
  <div class="inner_div"></div>
  <div class="inner_div"></div>
  <div class="inner_div"></div>
</div>

Answer №2

After some troubleshooting, I finally cracked the code! Special thanks to everyone for pitching in. The key adjustment that did the trick was switching from float to inline-block and adding vertical-align:top to the inner divs as shown below:

.inner_div {
    display: inline-block;
    vertical-align: top;
    width: 200px;
}

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

The document type specified in the HTML validation process does not support the use of the "hr" element

My goal is to display a list of articles including images, titles, horizontal lines, and dates using a UL. Unfortunately, I encountered a validation error when testing them on the website validator tool at https://validator.w3.org/. Below is a simplified ...

Incorporating dynamic HTML content into a Bootstrap 2.3.2 popover

I'm trying to create a dynamic Bootstrap popover with HTML content when hovering over a button. Here's what I have so far: $(".btn.btn-navbar").hover(function() { html = '<ul class="nav"><li><a href="#">hello</li>& ...

determining the number of td elements in a row of a table located within an iframe

When I interact with a cell in a table within an iframe, I want to determine the number of cells in that row. If a cell is actually a span element, I would like to consider it as part of the cell count. The iframe may contain multiple tables without any s ...

Unable to load JavaScript in an HTML file

I have tried numerous solutions for my issue, but none seem to work. When testing in JSFiddle, the code works perfectly due to automatic onload support. However, when I open the same HTML file locally in Chrome/IE/FireFox, the layout is correct but the J ...

Issue with background opacity not functioning properly in Internet Explorer 8

I am facing an issue with displaying 2 images in IE8 and IE9. Below is the code snippet causing the problem: .ui-widget-content { border: 0px solid #aaaaaa/*{borderColorContent}*/; background: rgba(0, 0, 0, 0.1) ; /*{bgColorContent}*/ url(images ...

iCloud Web Portal - Element Search Failed: Element Not Found

Exploring Python as a novice, I embarked on a project that requires logging into Gmail and iCloud using Selenium. The Gmail portion went smoothly, indicating that I am headed in the right direction. However, I hit a roadblock while trying to identify the l ...

CSS with three columns: the middle column has a fixed size, while the right and left columns

I am attempting to create a 3 column layout with specific requirements: The middle column is fixed at a width of 660px The left and right columns should each take up half of the remaining space, but have a minimum width of 120px The middle div need ...

What is the best way to ensure that my table cell dynamically adjusts to fit its contents in Bootstrap 3?

Currently, I am retrieving data from a MySQL database and displaying it in a table. However, I am having trouble getting the cell width to adjust to fit the content properly. I have attempted setting the width property of the table, but unfortunately, it ...

Is there a way to configure my sidebar to display on the left side of the content instead of appearing stacked on top of it?

How can I make the sidebar appear on the left side of the content without stacking on top of each other? Here is the HTML I am currently using for styling: <div class="bg-gray-600 flex flex-col max-h-min"> <div> <app-c ...

Retrieve the HTML tags following the modification of my information in ASP.NET

Apologies for any language errors, I am new to asp.net development. 1- I have a table with dynamically created rows and columns based on user selection of row and column numbers. 2- Each row in the table has the following controls: A- One textbox, one l ...

Adaptable Navigation

I am currently working on creating a responsive navigation menu inspired by Bootstrap's design. However, I have encountered some challenges in implementing it. The current issue I am facing is that the navigation disappears when transitioning from a s ...

Eliminating boundaries of a button component

Recently, I noticed an issue with a button on my website: https://i.sstatic.net/5S6fS.png The button has the following custom styling applied to it: const SubmitButton = styled.button` width: 275px; height: 30px; background-color: red; ...

To emphasize code in an HTML document, you can use the <font color="#xxx"> tag

Can anyone guide me on how to add color highlights to my HTML code in this way? <font color="#1773cc">#include </font><font color="#4a6f8b">&lt;NTL/ZZ.h&gt;</font><br> <br> <font color=&quo ...

Customizing Material UI Themes

Is there a way to customize the MuiTheme overrides for a specific named element like this? .MuiStepLabel-label.MuiStepLabel-active { color: rgba(0, 0, 0, 0.87); font-weight: 500; I've attempted modifying various classes such as MuiStepLabelLa ...

Issue with activation of onClick event in case/switch statement

Currently working on a JavaScript project to recreate the Fallout terminal game, with one of the main aspects being comparing words selected by the user to those chosen by the computer. The concept of this hacking game is reminiscent of the board game Mas ...

Implementing jQuery for form validation

I am facing an issue with validating an HTML page using jQuery. Despite having a simple layout, the textboxes are not being validated. Here is the code snippet: <link rel="stylesheet" type="text/css" href="style.css" /> <script type="text/javas ...

Making Adjustments to the Padding of Main Content Column in Typo3 Using Bootstrap Package

As the administrator of our local sports club's homepage, I oversee svwalddorf.de, powered by typo3 10.4.22 with bootstrap package 11.0.3. The current bootstrap template creates excessive empty space around the centered content area as shown here. Th ...

Tips for creating an inline list in MVC

I have a list in MVC that I want to display inline in blocks. List- <ul> <li> @foreach (var item in Model) { <img src="@item.cardFilePath"/> } </li> </ul> CSS attempted here- li { list-style-type: none; displa ...

Why isn't my jQuery click event functioning correctly following an AJAX request?

The functionality of the jQuery click event is successful in this scenario <div id="LangTable"><a class="deletelanguage">delete</a></div> $('.deletelanguage').click(function(){ alert("success"); }); However, wh ...

Eliminate the spacing between elements when they are in close proximity

I've noticed there is a margin between the buttons as shown below: .button + .button { margin-left : 1rem; } The buttons are contained within a <div class="row">, which will stack on top of each other on smaller screens: https://i.s ...