Balancing spaces in flexbox

In this scenario, no matter what value is assigned to justify-content, it is difficult to horizontally align the elements around their gaps.

* {
  box-sizing: border-box;
  margin: 0; padding: 0;
}
html, body {
  height: 100%;
}
body, div {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}
div {
  width: 10rem;
  height: 10rem;
  padding: 2rem 0rem;
  background-color: rgba( 0,0,0,0.0725 );
}
span {
  outline: solid 0.1rem red;
}
span:nth-of-type( 2n ) {
  margin: 0.5rem;
  width: 2rem;
  height: 1rem;
}
<div>
  <span>first item</span> <span></span>
  <span>second item</span> <span></span>
  <span>third item(etc)</span> <span></span>
</div>

Here is the intended outcome displayed below.

* {
  box-sizing: border-box;
  margin: 0; padding: 0;
}
html, body {
  height: 100%;
}
body, div {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}
div {
  width: 10rem; height: 10rem;
  padding: 2rem 0rem;
  background-color: rgba( 0,0,0,0.0725 );
}
span {
  outline: solid 0.1rem red;
}
span:nth-of-type( 2n ) {
  margin: 0.5rem;
  width: 2rem;
  height: 1rem;
}
/* additional code added below. Utilizing transforms to adjust each element individually */
span:nth-of-type( 3 ), span:nth-of-type( 4 ) {
  transform: translateX( 0.5rem );
}
span:nth-of-type( 1 ), span:nth-of-type( 2 ) {
  transform: translateX( 1rem );
}
<div>
  <span>first item</span> <span></span>
  <span>second item</span> <span></span>
  <span>third item(etc)</span> <span></span>
</div>

Is there a method to achieve the desired layout above without the need to target each individual element? When dealing with a large list of items, targeting each one for alignment would not be feasible.

Can the intended outcome be accomplished without modifying the base HTML or specifying each element?

Answer №1

To achieve the desired result of aligning them to the right, simply adjust the justification of the div. Additionally, fine-tune the margins to ensure it is in the correct position.

div{
    justify-content: flex-end;
}

By following these steps, everything will be aligned to the right and if the empty spans have the same width, the columns will consistently line up.

Answer №2

Working with flexbox allows for utilizing the available space on the line.

Aligning columns or gaps across rows can be challenging due to varying content lengths in each row, leading to fluctuating amounts of free space.

In contrast, CSS Grid simplifies the layout process.

div {
  display: grid;
  grid-template-columns: auto 2rem;
  grid-auto-rows: 1rem;
  grid-gap: 0.5rem;            /* shorthand for grid-column-gap and grid-row-gap */
  justify-content: center;     /* center the columns */
}

span:nth-child(2n + 1) {
  justify-self: end;
}

span {
  outline: solid 0.1rem red;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}

div {
  width: 10rem;
  height: 10rem;
  padding: 2rem 0rem;
  background-color: lightgray;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
<div>
  <span>first item</span>
  <span></span>
  <span>second item</span>
  <span></span>
  <span>third item(etc)</span>
  <span></span>
</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

Using jQuery, retrieve the "select" element within a specific row of a

I am working on a table row that has the following HTML structure: When the user interacts with the "Name" field, I trigger the "select" event and pass it to the nameDropChanged function. Once I have a reference to this select element, I need to change th ...

Catalog indexed in a JSON document

I'm currently facing an issue with extracting data from a JSON file and populating a list with that information. HTML : <ol id="dataT"> </ol> JavaScript : function GetData(index) { var xhttp = new XMLHttpRequest(); xhttp.onre ...

Creating AngularJS variables with dependencies on integer values

Hello, I am a brand new developer and I am currently working on creating an expenses calculator. The goal is to have the sum of inputted integers from a table, each with a default value, become the integer value of another variable. However, I seem to be m ...

Ways to halt a script entirely once its tag has been eliminated

I have a script from a page tracker website that runs periodically. Occasionally I need to restart the script from the beginning. To do this, I typically remove the script tag from the DOM and then re-append it. However, because the script utilizes setInt ...

Upon rerender, React fails to refresh the style changes

I am encountering an issue with my React component where the visibility and position can be changed by the user. Currently, the visibility can be toggled by adding or removing a CSS class, while the position is adjusted through a function that updates the ...

The <img> tag is displaying with dimensions of 0 x 0 pixels even though its size was specified

Is there a way to control the image size within an <img> tag placed inside an html5 video element? Currently, it keeps defaulting back to 0 x 0 pixels. The reason behind using this img is to serve as a fallback for outdated browsers where the video ...

Step-by-step guide to selecting a specific point on an HTML5 canvas using Python's selenium webdriver

Looking to automate interactions with a simple HTML5 application on a website using Selenium webdriver in Python with Firefox on Linux. The challenge is clicking a button on an HTML5 canvas, then dragging one or two objects around the canvas post-button cl ...

What could be the reason behind the issue of my HTML draggable feature not functioning properly on touch

I've set up a draggable div with headers and p tags, but I'm encountering an issue when trying to run it on a touch screen. The div tag isn't draggable in this scenario. Can anyone suggest the best solution for making this page touch screen ...

Is your browser tab failing to display the complete URL?

Encountering a strange issue while working on my current website project: When you click on "about," it does take you to the correct page, but upon refreshing the page, it redirects back to the home page. The same scenario is happening with other pages as ...

CSS: The Drop Down menu suddenly vanishes every time I attempt to select an item from the visible list

I'm facing an issue with my dropdown menu that I can't seem to resolve. Whenever I hover over the menu, it disappears before I can even click on any items. <ul id="menu"> <li><a href="#title">Home</a></li> ...

Organize pictures and words side by side on both sides at the same time

I am currently facing an issue while trying to load images and quotes vertically on the left and right side of the page. The problem arises with the 3rd image and quote due to an unexpected margin in the CSS code. Despite my efforts, I have not been able t ...

Tips for implementing a linear gradient on a bar graph using Highcharts

I'm trying to create a bar chart using highcharts, and I want to apply a linear gradient for the bar fill color. However, I am not sure how to set this up. Can anyone provide some guidance or ideas? Here is an example of what I'm hoping to achie ...

Checking for CSS-truncated text with JavaScript

I am currently working on a JavaScript project to determine if text is truncated. While I found a useful solution mentioned here, there is one edge case that needs to be addressed. Despite the visual truncation of the text, the first block on mouse hover i ...

Bring JavaScript Function into Vue's index.html File

Recently in my code files, I noticed the following: function example() { console.log("testing"); } In another file, I have something like this: <head> <script src="../src/example.js" type="text/babel"></sc ...

Developing User-Friendly Websites with Responsive Design

While I was working on my project, I realized that using only pixel values in tables and sidebars was a big mistake. This caused issues because if someone had a different screen resolution, my website would look distorted and unappealing. Could you please ...

What's the best way to center and expand a Bootstrap 5.3 dropdown menu to fill the entire width of the page?

I want to recreate the dropdown menu seen on this page, with the full width of the page but without any content. Additionally, I need all the "lorem" links in the menu to be centrally aligned in the navbar. Unfortunately, I am unable to modify the bootstra ...

What causes Chrome Extension Images to appear broken when they are inserted into the DOM?

Currently working on a Chrome extension, I am attempting to insert a div with a background image into the DOM using a content script. The CSS is loading correctly, and upon inspecting the Developer Tools, the image URL appears to be correct. $('.clos ...

Can a sophisticated text editor be utilized without a content management system?

Many website builders utilize rich text editors as plugins to enhance content creation, such as in CMS platforms like Joomla and WordPress. However, can these same editors be easily integrated into a custom website built from scratch using just HTML, PHP ...

Bootstrap 5 introduces a new feature where col-sm-6 will take precedence over col-md

When using Bootstrap 5, I encountered an issue where the columns were not sizing evenly in a row. Specifically, on small screens, I wanted them to be exactly size 6. In this case, the columns should have been size 3 for screens larger than md, but they end ...

Resizing rectangular images with CSS/Bootstrap into perfect squares

Imagine there is a rectangular image with dimensions of 1400px (height) x 700px (width). I am looking to turn it into a square while maintaining its original proportions and having white sides added. I specifically do not want to crop the image - I want it ...