Issue encountering with flex-wrap to shift to the next row once reaching the 5th element

Recently, I've been experimenting with creating an image gallery using flexbox and incorporating a flex-basis transition to control the growth and shrinkage of elements upon hover. However, I'm encountering difficulties in adjusting the gallery to utilize flex-wrap so that it breaks into the next row after reaching the 5th element.

I envision the functionality to work such that each row contains exactly 5 elements that expand and contract seamlessly on the same row. Once a sixth element is introduced, I'd like the layout to shift to the next row while maintaining the transitions intact.

  • The elements are supposed to stretch to fill the available space (approximately 20% width for each of the 5 elements)
  • -> Upon hover, the non-hovered elements should shrink by 5%, equating to a total of 20%
  • The hovered element should enlarge to 40%
  • For the sixth element transitioning to the next row, it must adjust its size proportionally
  • The seventh element occupying 50% of the row, eighth taking up 33.3%...and so forth

You can view my current progress here: https://codepen.io/TommyBoyLab/pen/YdzGjB

(source inspiration: https://codepen.io/joliveras/pen/GpLVKv)

Here's the HTML structure of the element:

<div class="container">
  <div class="item" style="background:url() center/cover">

And the corresponding CSS styling:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-flow: row wrap;
  width: 100%;
}
.container .item {
  display: grid;
  position: relative;
  flex: 1;
  transition: 500ms;
  min-width: 15%;
  max-width: 20%;
  height: 50vh;
}
.container .item:hover {
  transition: 500ms;
  max-width: 40%;
  flex-grow: 1;
}
.container .content {
  margin: auto;
  font-size: 1.5em;
}

Answer №1

You may simply need to adjust the flex-grow value on hover:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-flow: row wrap;
  width: 100%;
}
.container .item {
  display: grid;
  position: relative;
  flex: 1;
  transition: 500ms;
  min-width: 15%;
  max-width: 20%;
  height: 50vh;
  
}
.container .item:hover {
  transition: 500ms;
  max-width: 40%;
  flex-grow: 5;
}
.container .content {
  margin: auto;
  font-size: 1.5em;
}
<div class="container">
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  <div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
    <div class="content">
      <h3>Title</h3>
      <p>Lorem ipsum amet</p>
    </div>
  </div>
  </div>

Answer №2

Enhancement in CSS:

.item:first-child:nth-last-child(n + 5),
.item:first-child:nth-last-child(n + 5) ~ *{
    min-width: 17%;

}

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

Is it possible to assign numerical values to attributes in HTML code?

I'm unsure about something - is it possible to set an attribute value as a number? For example: <div data-check="1"></div> Is this the correct way to do it or not? I've heard conflicting opinions, with some people saying you shouldn ...

Is there a way to modify the background of a div when a specific field within my component is true and the div is being hovered over?

When I hover over a div, I want the background color to change. Additionally, I need the color choice to be based on an element within my component. <div *ngFor="let u of users;" [style:hover.background-color] = "u.selected ? 'red ...

Simple code styling tool

Seeking guidance to create a basic syntax highlighter using JavaScript or ClojureScript. While aware of existing projects like Codemirror and rainbow.js, I'm interested in understanding how they are constructed and looking for a simple example. Do th ...

Align the middle element in a dynamic row

There is a row consisting of three elements: left, center, and right. However, the problem lies in the fact that the center element is not aligned at the screen level, but rather at the row level. Is there a solution to align this element at the screen l ...

CSS Techniques for Smooth Transitions

When the hamburger icon is pressed, a menu appears over the current page with CSS from Glamor. The menu slides in perfectly from the right side of the screen, but I am having trouble getting it to slide out when any part of the menu is clicked. I have def ...

What is the best way to capture a form submission when there are no errors?

My form includes a validation check for valid fields upon submission. Here is an example of how it is structured: <form class="form-horizontal" method="post" action="#" name="basic_validate" id="basic_validate" /> <div class="con ...

Decoding JSON in AngularJS

Looking for assistance in parsing a JSON 2D array with Angular JS and fetching images from the array. Can anyone provide some guidance on this? I have updated my Plunker at this link HTML Code <!DOCTYPE html> <html lang="en" ng-app="myAp ...

Ways to eliminate the unusual dashed space in ReactJS using Bootstrap

While developing an app with NextJS and Bootstrap, I noticed a strange dashed gap at the top of the screen in the elements tab. Despite checking for margin or padding-top properties on any element, there doesn't seem to be a clear cause for this issue ...

Showcase a sizable picture broken down into smaller sections

I am interested in creating a mapping application similar to Google Maps that can asynchronously load images from the backend. I am seeking guidance on where to begin and how to proceed in this endeavor. The ultimate goal is to have the image displayed w ...

CSS Animations as an Alternative to jQuery's fadeIn(), fadeOut(), and fadeTo() Functions

I currently have the following code snippet: $('#button1').click(function(){ $('#header_bg').fadeTo(15, 0, function() { document.getElementById('header_bg').style.fill = '#FF0000'; }).fadeTo(&ap ...

Enhancing a JQuery progress bar with customized text and dynamic background color updates

I am considering implementing the Jquery progress bar to display the user's advancement in a course. Within the progress bar, I aim to exhibit the text Course Progress 70% Additionally, I wish to alter the default grey color of the progress bar Thi ...

Determine the vertical distance that a div element is currently visible while scrolling

To better understand this concept, please refer to the following fiddle: http://jsfiddle.net/abhicodes/LasxP/ The main goal here is to calculate the visible height of the element with the ID #content-wrapper as the user scrolls. The height of the header ( ...

Utilizing CSS position sticky in Bootstrap 4 to maintain visibility of a sidebar

I currently have a layout with two columns structured like this: <div class="row"> <div class="col-xs-8 content"> </div> <div class="col-xs-4"> </div> </div> By applying the position:sticky to the si ...

How can I open a new window, redirect the current one, and bring focus to the new window using JavaScript?

Trying to troubleshoot a problem I'm having with the following setup: - Using SAP Portal, I am launching an HTML page containing this code. - The goal is for the HTML page to open a new window. - Once the new window opens, the original HTML page ...

Closing the dropdown menu by directly clicking on the button

I've noticed several inquiries on this platform regarding how to close a drop-down menu by clicking anywhere outside of it. However, my question is a bit different. I want the dropdown-menu to remain open once clicked, only closing when the user clic ...

What is the best way to populate a dropdown menu with data and enable users to search for specific items by typing them in?

I need assistance with populating data in a drop-down box that is not pre-defined. The data needs to be dynamically created and then displayed in the drop-down box. Currently, the data is being shown using check boxes, but I want it to appear in a drop-dow ...

PHP XML loop encountering issues with functionality

Hi there, I'm a newcomer to stackoverflow, but I've been following this site for quite some time. Currently, I'm working on a simple web application that will serve as a catalogue for music tracks, allowing users to add their own tracks afte ...

Getting the input from an HTML editor and inserting it into a textarea using JavaScript

Currently, I am in the process of developing an HTML editor for a project. I have downloaded a program online that I am attempting to customize according to my requirements. However, I am encountering difficulties when trying to retrieve the inner HTML of ...

How can I position a form next to a title when utilizing the Bootstrap Panel feature?

I have experimented with multiple solutions found on various sources, but unfortunately none have been successful. I came close to achieving the desired result, but encountered an issue with the panel's default background color not displaying properly ...

Align these buttons in a vertical position

I'm trying to place a Twitter button and a LinkedIn profile button next to each other, using the recommended embed code from each service. However, they are not aligning vertically as I would like. I want both buttons to be centered vertically within ...