What are some techniques for aligning div elements with varying heights?

I have been attempting to align div elements like in this example: https://i.sstatic.net/FoBPg.png

However, my result is not turning out as expected: https://codepen.io/online123/pen/VwvGoQP

The HTML code I am using is:

  <div class="under d-flex flex-wrap justify-content-center">
  <div class="box">Flex item 1</div>
  <div class="box">Flex item 2<br/><br/><br/>end</div>
  <div class="box">Flex item 3</div>
  <div class="box">Flex item 4</div>
  <div class="box">Flex item 5<br/><br/>end</div>
  <div class="box">Flex item 6</div>
  <div class="box">Flex item 7</div>
  <div class="box">Flex item 8</div>
  <div class="box">Flex item 9<br/><br/><br/><br/>end</div>
  <div class="box">Flex item 10</div>
  <div class="box">Flex item 11</div>
  <div class="box">Flex item 12</div>
  </div>

And the corresponding CSS code is:

.under {
    width:70%;
    margin:auto;
    padding-top:40px;
    padding-bottom:40px;
    text-align:center;
    border: 1px solid green;
    overflow:auto; 
    justify-content:center;
    position:relative;
}

.box {
    text-align:center;
    width:314px;
    padding:10px;
    margin:10px;
    display:inline-block;
    border: 1px solid black;
    align-self:flex-start;
}

body {
    height:100%;
    padding:0px;
    margin:0px;
    flex-direction:column;
    justify-content:center;
    background-color:#262626!important;
    color:#ddd!important;
    font-family: 'Jost', sans-serif;
}

Can anyone provide guidance on how to properly align these divs? Any help would be greatly appreciated. Thank you.

Answer №1

To achieve a static layout without using a loop to map over elements, you can follow this approach:

.under {
width:70%;
margin:auto;
padding-top:40px;
padding-bottom:40px;
text-align:center;
border: 1px solid green;
overflow:auto; 
  display:flex;
  flex-direction:row;
  align-items:flex-start;
  justify-content: center;
position:relative;
column-count: 5;
}

.box {
text-align:center;
width:314px;
  min-width:314px;
padding:10px;
margin:10px;
display:inline-block;
box-shadow: 0px 4px #1c1c1c;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
background-color: #363636;
border: 1px solid red;
}

@media screen and (max-width: 2450px) {
.under {
column-count: 4;
}
}

@media screen and (max-width: 1900px) {
.under {
column-count: 3;
}
}

@media screen and (max-width: 1350px) {
.under {
column-count: 2;
      flex-direction:column;
      align-items:center;
}
}

@media screen and (max-width: 900px) {
.under {
column-count: 1;
      flex-direction:column;
      align-items:center;
}
}


asd{
  display:flex;
  flex-direction:column
}


body {
height:100%;
padding:0px;
margin:0px;
flex-direction:row;
justify-content:center;
background-color:#262626!important;
color:#ddd!important;
font-family: 'Jost', sans-serif;
}
<div class="under">
  <div class='asd'>
    <div class="box">Flex item 1</div>
    <div class="box">Flex item 4</div>
  </div>
  <div class="box">Flex item 2<br><br><br>end</div>
  <div class='asd'>
    <div class="box">Flex item 3</div>  
    <div class="box">Flex item 6</div>
  </div>
  <div class="box">Flex item 5<br><br/>end</div>
  <div class='asd'>
    <div class="box">Flex item 7</div>
    <div class="box">Flex item 8</div>
  </div>
  <div class="box">Flex item 9<br><br><br><br>end</div>
  <div class='asd'>
    <div class="box">Flex item 10</div>
    <div class="box">Flex item 11</div>
    <div class="box">Flex item 12</div>
  </div>
</div>

Kindly update the answer and test it out for accuracy.

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

It is crucial to refrain from making any CSS adjustments in Google Chrome

So I encountered an interesting issue on my website. There are two CSS files - main.css and bootstrap.css, with some overlapping styles. I made a change in the bootstrap.css file by adding !important next to the property. Surprisingly, in Firefox, the chan ...

Button for Selecting Colors

I love the color picker button on Google Keep, where a variety of colors pop up for selection. I'd like to add something similar to my website. What steps should I take to implement this feature? ...

What is the process for displaying a document file in an iframe that is returned from a link's action?

I have a main page called index.cshtml. This page displays a list of document files along with an iframe next to it. My goal is to load the selected document file into the iframe when I click on any item in the list. However, currently, when I click on a d ...

Activate month input programmatically

Having an issue trying to open a month popup using jQuery trigger. I have a button and an input of type month, but when the button is clicked, the popup does not open as expected. You can find the fiddle for this question here. $(document).ready(functio ...

Different ways to modify the color and thickness of a variable in HTML5 with either JavaScript or CSS

I am currently working with a JavaScript file where I have a variable defined as follows: var nombre = document.getElementById('nombre').value; The 'nombre' variable corresponds to an element in an HTML file: Nombre: <input type=" ...

Guide to leveraging clsx within nested components in React

I am currently using clsx within a React application and encountering an issue with how to utilize it when dealing with mappings and nested components. For instance: return ( <div> <button onClick={doSomething}>{isOpened ? <Component ...

`Scrolling through a horizontal menu with no scrollbar present`

Is there a way to create a horizontal menu that can scroll left or right without the scrollbar? I'm looking for a solution like adding on-screen arrow buttons or implementing mouseover functionality. What would be the best approach? div.scrollmenu ...

Widget for tracking attendance - incorporating HTML, CSS, and Bootstrap styling

I recently created a widget named attendance to track monthly data, from January to December. I spent time designing and coding it using HTML, CSS, and Bootstrap, as shown in the image below https://i.sstatic.net/FtFJv.png However, I am now faced with t ...

The text input field is unresponsive and unable to be selected in the mobile layout

I have implemented this <textarea> component within my web application <div class="col-xs-11 lessSpaceBetweenLine"> <textarea class="form-control actionitems" id="actionitems" name="actionitems" rows="2" pl ...

Is your margin not functioning correctly? Is padding taking on the role of margin?

One issue I'm encountering is that the margin isn't behaving as expected in this example. The padding seems to be working like margin should. Why is that? Print Screen: https://i.stack.imgur.com/N1ojF.png Why is it that the margin in the h3 ta ...

Customize Google Chrome to display a vibrant splash screen instead of a boring white blank page when

"I've been on the lookout for Chrome apps that can help make my screen darker or inverted to reduce eye strain. While I have found some apps that do the job, there's one thing they don't seem to be able to override - the White Blank page. W ...

Setting an AngularJS variable as the name attribute in an HTML tag

Important Values {{item.name1}} equals item_name_1 {{item.description}} represents a Band Situation 1 <input name="item_name_1" value={{item.description}}> The input field is filled with Band Situation 2 <input name={{item.name1}} value={{ ...

The process of manipulating the content of an HTML textarea using Selenium with Python

http://neomx.iwedding.co.kr/roundcube I attempted to change the content of a textarea, specifically the tracking number. My goal was to replace "9400111206213835724810" with "487289527385922090". However, I encountered an error message stating "ElementNot ...

Display a popup notification when clicking in Angular 2

Can anyone help me with displaying a popup message when I click on the select button that says "you have selected this event"? I am using Angular 2. <button type="button" class="button event-buttons" [disabled]="!owned" style=""(click)="eventSet()"&g ...

Icon displayed within the input field

Is there a simple method to add an input text element with a clear icon on the right side, similar to the layout of the Google search box? I've searched, but I could only find instructions for placing an icon as the background of the input element. I ...

The outputstring encountered an issue with the HTML class

Struggling a bit with php, I'm trying to incorporate a basic comment system that accesses and writes to a comments.php file. Everything is functioning well until I attempt to style the $outputstring. Here's the code in question: $outputstring ...

Adjusting the color of an element in ReactJS as the user scrolls and hits a specific position

Is there a way to dynamically change the color of my header based on the background color as I scroll through different sections of my webpage? I have a fixed header and multiple sections with varying background colors. I want the header's text color ...

How can you position navbar links to the right in Bootstrap?

I have been given an assignment that requires me to create HTML code to replicate a specific webpage design, which can be viewed here: https://i.sstatic.net/GC68r.png One of the requirements is to align the Home, Contact, About Us, and Messages links to t ...

Showcasing an item hidden behind text and a dropdown menu

I have made changes to the background image in my Wordpress theme and now I want to display another image on top of the white content area. To achieve this, I used the following code: img style="position: absolute; top:244px; left: 220px;" src="http://ww ...

Enhancing a Bootstrap 4 navbar menu system with padding tweaks

Just starting out with Bootstrap 4 and creating a practice page with a header to familiarize myself with the navbar system. I have put together this jsFiddle (full code) for reference, but essentially, my page structure is as follows: index.html <nav ...