Dynamic design with consistent spacing that increases in harmony with each row

My challenge is to maintain the left alignment of elements in a container that can increase or decrease in width. If the container size increases, the first element from the next row should move back one row and align on the right side if there is enough space with padding on both sides.

If there isn't enough space for the next row's element to move up, then the spacing between the elements should expand until it fits properly.

This functionality should also work in reverse.

Below is the code I'm currently using:

.container {
  min-height: 400px;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  align-content: flex-start;
}

.container:after {
  display: block;
  content: " invisible node ";
  flex(999 999 auto);
}

.item {
  flex: 0 0 auto;
  margin: 5px;
  width: 50px;
  height: 50px;
  background-color: green;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

https://jsfiddle.net/p6k537ep/

The issue I am facing is that the second row doesn't have consistent spacing between the elements as the first row (depending on how many elements are present).

Edit:

Although the elements from the second row only move up one row if there is sufficient space, the gap between them grows correctly in the first row but not in the second row.

Answer №1

As far as I know, solving this issue in Flexbox requires adding invisible non-semantic helper elements to maintain the horizontal rhythm of the last row. These elements should have the same width and horizontal margins as the .items but zero height.

Alternatively, CSS Grid offers a straightforward solution:

.container {
  min-height: 400px;
  display: grid;
  grid-template-columns: repeat(auto-fill, 60px);
  justify-content: space-around;
  align-items: center;
  align-content: start;
}

.item {
  flex: 0 0 auto;
  margin: 5px;
  width: 50px;
  height: 50px;
  background-color: green;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

Another way to tackle this is using inline-blocks with a pseudo-element hack for "magic" formatting:

.container {
  min-height: 400px;
  text-align: justify;
  font-size: 5px;
  line-height: 0;
}

.container:after {
  display: inline; /* important for continuity */
  content: '— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — ';
  letter-spacing: 55px;
  word-spacing: -56px;
  margin-left: 5px;
}

.item {
  display: inline-block;
  margin: 5px;
  width: 50px;
  height: 50px;
  background-color: green;
  vertical-align: top;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

However, I would not recommend the second option for production use. Aligning the items to the left may be a better fallback for browsers without Grid support.

Answer №2

To maintain the alignment of blocks, one approach is to utilize the margin property on the :after pseudoelement.

Check out this fiddle for reference

.container {
  min-height: 400px;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  align-content: flex-start;
}

.container:after {
  display: block;
  content: " ";
  margin-left: auto; /* added */
}

.item {
  flex: 0 0 auto;
  margin: 5px;
  width: 50px;
  height: 50px;
  background-color: green;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></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

Incorporate content from a single HTML file into a different one

Hello, I am working with two HTML documents - let's call them index.html and index2.html for this example. I am looking to embed all the code within the body tag of index2.html into a section within index.html. Is there a way to create this connectio ...

Utilizing a large logo alongside the NavBar in Bootstrap

Trying to design a sleek navbar that is proportional to the logo size, while also allowing for additional content below it. The jsFiddle link below showcases the current setup. The black line signifies the bottom of the nav bar, which has expanded to matc ...

What is the best way to shift my content to the next line once it reaches a specific width in my paragraph

When a string is pulled from a database and inserted into a paragraph, it breaks perfectly if the paragraph has spaces. However, if a word is longer than the line, it goes off the page. How can I make it wrap to the next line instead of overflowing off the ...

Safari IOS experiencing issue with element disappearing unexpectedly when input is focused

I am facing a situation similar to the one discussed in the question (iOS 8.3 fixed HTML element disappears on input focus), but my problem differs slightly. My chatbox iframe is embedded within a scrollable parent, and when the iframe is activated, it exp ...

Adjusting offcanvas height in Bootstrap based on content

.cursor-pointer { cursor: pointer; } <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="690b06061d1a1d1d09c13c809e9afbcfc1792efeff8">[email protected]</a>/dist/js/bootstr ...

Update a div container using jQuery

Hey there, I need some help with a specific part of my code snippet: <div class="tags-column" id="tags"> <h2>Tags</h2> <ul> <% @presenter.tag_counters.each do |tag_counter| %> <li class=" ...

increasing the size of the input field on the lower row of the form

I have a form with two rows of text boxes. The first row contains three text boxes, while the second row should only have one text box that spans 100% of the width. Here is the current code: <div class="row"> <div class="col-md-4"> ...

Utilizing CSS naming conventions to bring order to class inheritance

I'm a bit unclear about the rationale behind using this specific CSS structure: h2.class-name It seems like you could simply use .class-name and apply that class to the h2 element: <h2 class="class-name">Title thing</h2> Unless it&apos ...

What is the best way to preserve the background color of nested progress bars?

In this scenario, I am attempting to apply border-radius specifically to the upper and lower right corners of the progress bars. The idea is to create a cascading effect where each color (red, green, and blue) progress bar overlaps slightly with the next o ...

What is the best way to eliminate base tags from jQuery mobile scripts?

In jQuery Mobile, how can I remove the base href tags from the page and disable base href? Code Snippet: // Function to test for dynamic-updating base tag support function baseTagTest() { var fauxBase = location.protocol + "//" + location.host + l ...

Tips on utilizing media queries to optimize website layout for mobile devices

I'm currently working on a responsive website and utilizing media queries for that purpose. However, I've encountered an issue where the media queries don't seem to apply when the browser window is set to mobile view. Below is the snippet of ...

What is the method for inserting line breaks in Django at particular characters?

How can I add line breaks after specific characters in my text? Check out this image: https://i.sstatic.net/bz1h1.png I need to insert line breaks after every 50 characters in the text shown in the image. Take a look at my HTML FILE: {% extends ' ...

Styling specific to Nuxt.js layout with scoped css

Currently, I am using Nuxt and have a header layout for my navigation that has a white background color. However, on a specific page, I would like to change the navigation background color to be transparent (only for this page). I have attempted a few so ...

``I am having trouble getting the Bootstrap carousel to start

I am having trouble getting the Bootstrap Carousel to function as expected. Despite including all the necessary code, such as initializing the carousel in jQuery.ready, the images are stacking on top of each other with navigation arrows below them instea ...

What is the best way to send props to a CSS module in React?

Currently utilizing NextJS, I have a route named about that is displayed through page.js. Within /about/page.js, I am incorporating a component called <Hero /> and aiming to pass a prop to <Hero heroHeight={'height: 400px'} /> to my C ...

Should input fields be placed inside separate columns or share a column in Bootstrap to keep them together while being right justified?

When using Bootstrap, is it better to place separate input fields inside their own col or share a col to keep them together and right-justified? I have a combo and input that I want to display on the same row at the right-hand side of the screen. If I put ...

Modifying the color of text within an element using CSS and the ::after pseudo

Is there a way to change the text color when a user hovers over it without interfering with the background effect? I currently have a hover effect set to show a background section when hovering over the text, but when I try to change the text color separat ...

Aligning HTML form with CSS styling

After researching various strategies on how to achieve this, and implementing the suggestions provided, I am still encountering difficulties. While the text successfully centers, the actual elements remain aligned to the left of the screen. Below is the f ...

Creating product cards with equal heights in Bootstrap 5 is a simple and effective way

Any ideas on how to achieve equal height for product cards? Flex doesn't seem to be doing the trick. .product-card { position: relative; padding: 1px; display: -ms-flexbox; display: flex; -ms-flex-direction: column; flex-direction: column; ...

The hover state of a div will not be lost if its parent element is not being hovered over

When hovering over the second, third, or fourth item, hidden text will appear on the left side. If you hover your cursor over the hidden text, it will disappear again. I want to be able to hover over the second item, move my cursor to "hide", and click o ...