Arrange the Flex items in the bottom row so they are positioned below the other elements

I'm facing a challenge with aligning elements in my design. The first two rows are perfectly aligned because they have an equal number of elements. However, the last row has fewer elements and I want to maintain alignment between the bottom and top elements, similar to the example shown in this image:

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

Example HTML Structure

<div id="bulbsCentralizer">

            <div id="letterCentralizer">
                <h3 class="letter">A</h3>
            </div>

            <div id="letterCentralizer">
                <h3 class="letter">B</h3>
            </div>

            <div id="letterCentralizer">
                <h3 class="letter">C</h3>
            </div>

            <div id="letterCentralizer">
                <h3 class="letter">D</h3>
            </div>

            <div id="letterCentralizer">
                <h3 class="letter">E</h3>
            </div>

            <div id="letterCentralizer">
                <h3 class="letter">F</h3>
            </div>  

            <div id="letterCentralizer">
                <h3 class="letter">G</h3>
            </div>  

        </div>

Custom CSS Styles

#bulbsCentralizer {
width: 600px;
height: auto;
background-color: red;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
display: flex;
flex-wrap: wrap;
flex-flow: row wrap;    
}

#letterCentralizer {
width: 40px;
height: 60px;
text-align: center;
background-color: orange;
position: relative;
float: left;
width: calc(100% * (1/8) - 10px - 1px);
margin-top:10px;
}

Answer №1

To achieve alignment in your elements using flex, it is best to avoid using float or position. Utilize flex properties for better results! For more information, visit: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

.container {
  display: flex;
  flex-flow: row wrap;
  justify-content: flex-start;
  width: 200px;
}
.element {
  width: 50px;
  height: 50px;
  background-color: red;
  flex: 0 0 32%;
  margin: 1% 0;
}
.element:nth-child(3n-1) {
  margin-left: 2%;
  margin-right: 2%;
}
<div class="container">
  <div class="element"></div>
  <div class="element"></div>
  <div class="element"></div>
  <div class="element"></div>
  <div class="element"></div>
  <div class="element"></div>
  <div class="element"></div>
  <div class="element"></div>
</div>

Answer №2

https://example.com/jsfiddle-demo

In terms of handling displays over a single axis, flex does a good job. However, an even better solution for this particular issue is to utilize display: grid. The use of grid-template-columns here provides added convenience.

Cheers!

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

When adjusting the window size with the <ion-split-pane>, both the menu and router outlet disappear

When I have two ion menus and one main content (router outlet) and resize the page or switch to mobile view, the page appears blank as if the content is missing. Here is the code: <ion-app> <ion-split-pane> <ion-menu side="start" me ...

Having trouble with adding a class on scroll?

My challenge is to extract the header from this website, by adding an additional class when the user scrolls at a position greater than 0. I thought it would be easy, but Java always presents problems for me. Here’s the code I came up with: <!DOCTY ...

I've tried to hide the sub-menu initially using display:none in CSS, and then show it using display: block on hover. However, for some reason,

I have set the sub-menu to be hidden with display: none, and it should show on hover with display: block, but for some reason, it's not working as expected. To make the sub-menu appear when hovering over main-menu-link, you can use the following HTML ...

Tips for creating a dynamic div slider

I've successfully created an admin page that features a slider within the main content. While I've managed to style everything accordingly, implementing the slider functionality has proven to be a challenge for me. Can anyone provide guidance on ...

Connecting table checkboxes to buttons in Angular 4.x: A step-by-step guide

Exploring Angular 4.x for the first time and faced with a challenge. I have created an HTML table where each row contains a checkbox and buttons. My goal is to link the checkboxes with the buttons in such a way that when a checkbox is checked, the correspo ...

Finding the Row Number of an HTML Table by Clicking on the Table - Utilizing JQuery

Seeking assistance to tackle this issue. I currently have an HTML table with a clickable event that triggers a dialog box. My goal is to also retrieve the index of the row that was clicked, but the current output is not what I anticipated. //script for the ...

Display an Asterisk Icon for md-input fields with lengthy labels

Documentation states that md-inputs add an asterisk to the label if it is a required type. However, when input containers have width constraints and long labels, the label gets truncated and the asterisk becomes invisible. From a user experience perspectiv ...

What techniques are most effective for concealing a div container through css?

I attempted to hide the #div element using CSS: #div{ display: hide; } However, the method did not work as expected. ...

Creating a function that uses setInterval to continuously update the input with a specific value

I am looking to use the setInterval function to continuously update the value of #test1. Additionally, I want the value of #test1 to be cleared and reset to 1 second after the user clicks a button. Example output can be found here: http://jsfiddle.net/eK ...

Master the Flexbox Technique to Automatically Scroll to the End

I am currently working on a Chat UI that needs to function consistently across various browsers. The main issue I am facing is the inability to keep the chat-body div scrolled to the bottom when the page loads or when new messages are added. Even after ch ...

Login form with AJAX for seamless submission without page refresh

I found a helpful tutorial on creating a login form at This tutorial focuses on authenticating against our LDAP server. However, I am running into an issue with the following line of code: success: function(){ $('form#submit').hide(function() ...

Clicking on the initial link will in turn prompt clicks on the subsequent links

Can you please provide instructions on how to correctly simulate a click on the remaining links if a click is made on the first link? jQuery('a.one_num').click(function() { jQuery('a.two_num').click(); jQuery('a.three_num&ap ...

Utilizing Bootstrap3's mobile-first methodology ensures that rules designed for small screen sizes do not inadvertently impact larger screens

Utilizing Bootstrap 3.3 and following a mobile-first approach in authoring. Below is the CSS snippet: .link{ display: block; margin: 7px auto; text-align: center; /*Tablets+ only*/ @ ...

Update array when checkbox is selected/deselected

Using angular-google-maps, I am creating a map with various types of markers. Below the map, there are checkboxes in a simple form: <div class="mapOptions"> <form action="" class="form-inline" style="text-align:center"> <label for=""& ...

Numerous images with clickable features

Currently, I am in the process of designing a screen that will showcase around 50 toggle buttons to be displayed on a monitor within a building. My goal is to incorporate a variety of images as toggles to ensure they are easily visible and identifiable. I ...

Incorporate an SCSS file into the Stackblitz project

Is there a way to include an scss file in the stackblitz? I attempted it, but encountered some issues. Could you take a look and advise me on what to do? I also made an attempt to add home.html This is the project: Check out the stackblitz project here! ...

CSS is not being applied to the HTML select element generated by PHP

Issue Update: The problem I'm facing is not related to dynamic generation, but rather the sheer number of items. To see a demo, click here: plunker I have select boxes on my website with a consistent style: dark background with white text. select { ...

What is the best way to right-align navigation using bootstrap?

We are struggling to align our navigation items to the right side of our page. Despite using a container class and align-items-end, we have not been successful in achieving this. We must confess that this section is somewhat hastily put together, and we do ...

Adjustable Greybox Overlay Resizes Window Dimensions

Utilizing greybox on my business website allows users to easily log in and access their accounts. View an example of my site However, when the login or sign up window is activated at the top of the page, the page or window size changes, causing scroll bar ...

Implementing a JavaScript Function to Retrieve a Value When a Button is Clicked

<!DOCTYPE html> <html> <body> <button id="one">Try it</button> <h1>JavaScript Functions</h1> <p id="demo"></p> <script> var x=document.getElementById("one").addEventListener("click", onefunction); ...