Switch from flexbox layout to begin on the next column within the same row

I am attempting to create a CSS layout where, after a specific element like :nth-child(6), the elements will break and form a separate column within the same row. The parent element should split into 2 columns after every 6th element in the same row, then move to the next row and follow the same rule. I know this can be achieved using divs, but I want to make it dynamic by utilizing flexbox and grid.

HTML:

  <div class="home__content">
    <!-- sub cards -->
    <div class="sub__card">
      <div class="home__subcardContainer">
        <h4>একটা যুগের অবসান, চলে গেলেন সকলের প্রিয় ফেলুদা সকলের প্রিয় ফেলুদা</h4>
      </div>
      <img class="subcard__image"
        src="https://sambadkolkata.in/uploads/post_main_image/1605424509_image_IMG-20201114-WA0230.jpg" alt="Avatar"
        style="width:100%">
    </div>
    
    <div class="sub__card">
      <div class="home__subcardContainer">
        <h4>একটা যুগের অবসান, চলে গেলেন সকলের প্রিয় ফেলুদা সকলের প্রিয় ফেলুদা</h4>
      </div>
      <img class="subcard__image"
        src="https://sambadkolkata.in/uploads/post_main_image/1605424509_image_IMG-20201114-WA0230.jpg" alt="Avatar"
        style="width:100%">
    </div>

  </div>

CSS:

.sub__card {
  display: flex;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  transition: 0.3s;
  width: 90%;
  border-radius: 5px;
  margin: 2%;
}

.sub__card:hover {
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}

.home__subcardContainer {
  padding: 10px 10px;
}

.subcard__image {
  border-radius: 0 5px 5px 0;
  max-width: 30%;
}

.home__content {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  padding: 5% 0%;
}

.home__contentLeft {
  flex: 0.5;
}

.home__contentRight {
  flex: 0.5;
}

.sub__card:nth-child(1) {
  flex-direction: column-reverse;
}

.sub__card:nth-child(1) > .subcard__image {
  border-radius: 5px 5px 0 0;
  max-width: 100%;
}

.sub__card:nth-child(7) {
  flex-direction: column-reverse;
}

.sub__card:nth-child(7) > .subcard__image {
  border-radius: 5px 5px 0 0;
  max-width: 100%;
}

.sub__card:nth-child(6) {
  break-after: always;
}

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

Answer №1

Creating this layout is quite simple using css grid.

.container {
  display: grid;
  grid-template-columns: repeat(6, 6em);
  grid-gap: 1em;
}

.item {
  background-color: dodgerblue;
  height: 4em;
  border-radius: 0.5em;
}
<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>

Feel free to check out the codepen link for live editing and visualization.

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 for a memory leak to occur when a jQuery object is created within a function but never actually used?

As an illustration: function calculateTime(minutes) { var newTime = new Date(); newTime.setHours(0, minutes, 0, 0); return angular.element('<input type="hidden"/>').timepicker('setTime', newTime).val(); } I'm cu ...

Turn off automatic downloading of embedded frame content in Safari for iOS

I am encountering an issue with a modal that displays a PDF and offers two options - one to print and one to download. The download option uses a blob with content-type: application/octet-stream, while the print option utilizes a blob with content-type: a ...

Subject fails to subscribe to the change

There are two components in my project that share a common service. shared.service.ts // ..... skipping top level codes private pickAnalysisForBubble = new Subject<any>(); analysisForBubble$ = this.pickAnalysisForBubble.asObservable(); mapTo ...

Detecting activity and keeping idle ng alive

I am utilizing angular ng-idle (core and keepalive) in my project, with the goal of detecting user activity. Despite my efforts to search for documentation on when each ng-idle event occurs, I have come up empty-handed. While I have found examples of h ...

What is the best way to transfer data from a div tag to an li tag using JavaScript?

https://i.stack.imgur.com/se2qk.pngI am attempting to change the div tag content to li tag. Here is a snippet of the code from inspect for reference. I need to remove the div tag with the "domTitle" class. <li style="display: inline;" id="list_name"> ...

Issue encountered in Angular 2 rc.5 where the page fails to refresh after upgrading from rc.4

I recently updated my app from rc.4 to rc.5 and now I'm facing an unusual issue. Upon visiting localhost:8080, I am correctly redirected to localhost:8080/browse. However, when I refresh the page, I receive the error message: Cannot GET /browse. Pri ...

The absence of the iframe in ie8 is causing problems that cannot be fixed with relative positioning

On a website, I am integrating an external 2-factor authentication solution called Duo Web using their PHP and Javascript. It works smoothly on all browsers except for IE8. When the user passes the initial login screen, the 2FA login page loads, but the if ...

Updating hyperlinks from dynamic php links to stable static links

I have a script that predominantly uses absolute addressing, but now I need to switch to relative addressing. The problem I'm encountering is when trying to change the links in PHP from: <link href="<?php print $theme; ?>style.css" type="tex ...

Using AJAX POST for real-time validation of usernames and email addresses

Creating a basic form with AJAX and jQuery to validate the username and email address before submitting: <body> <div> <h5> Registration </h5> <hr /> <div> Username: <input type="text" size="32" name="membername" ...

Monitoring WooCommerce order submissions through AJAX

I'm exploring the world of Ajax for the first time. My goal is to submit an order tracking form and show the results using AJAX. I attempted to use Ajax to display the results within div track_result. The issue I'm facing is that the following ...

Accessing User Input Data with JQuery

Can someone help me figure out how to store the input value from a Materialize select form in HTML using a variable in javascript/jquery? Here is the HTML code for the form: <div class="input-field col s12"> <select> <option va ...

Applying CDNJS CSS or external CSS to Nodemailer HTML templates with Jade and Express: A Guide

I am attempting to send emails using node mailer. I have a jade template with HTML markup in an external file, which is compiled and rendered correctly. However, the styles inserted through maxcdn or cdnjs are not being applied. In this situation, how can ...

What is the best way to erase information displayed when hovering over an element using mouseout?

Whenever the user hovers over an image, an information box appears regarding that specific image. The information inside the box changes as I move over another image, but when not hovering over any images, the information box remains visible. Unfortunately ...

Is it not recommended to trigger the 'focusout' event before the anchor element triggers the 'click' event?

In a unique scenario, I've encountered an issue where an anchor triggers the 'click' event before the input field, causing it to lose focus and fire the 'focusout' event. Specifically, when writing something in the input field and ...

Mouse over the image link and update the CSS text effect

I need help with a WordPress plugin and I'm trying to avoid making too many changes to the HTML output. Is there a way to make both the image and text change color when hovered over? You can see what I'm working on here - http://jsfiddle.net/3JE ...

Unexpected data being passed in knockout click event binding

for (let i = 0; i < 9; i++) { vm.icdCodes.push({Index:i, ID:'',DiagnosisCd: '' ,Description:ko.observable('')}); } <tbody data-bind='foreach: $root.icdCodes'> <tr> <td> ...

Alter the status of a checkbox programmatically

There are two checkboxes available: <input type="checkbox" id="id3"></input> <input type="checkbox" id="id4"></input> The desired functionality is that when clicking on id3, id4 should also be checked. Initially, this works as e ...

Waypoint function malfunctioning when overflow:hidden is applied

CodePen If you exclude the overflow property from the CSS selector .wrapper in the CodePen example, the waypoints functionality will work properly. However, it does not function correctly when using the overflow set to hidden either horizontally (x) or ...

Challenges with conflicting CSS styling issues

I am facing a CSS issue on my website. I am trying to add captions for images on my site. I have experimented with the jquery-capty plugin () and some custom CSS styles for framed images (). While both solutions work in my regular layout, they do not funct ...

Following the recent update to IntelliJ IDEA 2022.1.3, the use of ::ng-deep has been marked

After updating the version of IntelliJ, I noticed that the ::ng-deep angular material selector is now marked as deprecated. Here's an example: <mat-form-field class="register-custom-select"> <mat-select formControlName="gende ...