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;
}

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

ajaxform is not providing the correct response

There is a form below that logs into Instagram based on the response it receives. If the login is successful (returns "ok"), then it shows success, otherwise it should display an error state. However, in my case, it always returns a null state for some un ...

Editing an XML file on the browser and saving it in its original location on the local file system

Seeking assistance with editing an XML file directly from the browser on a local file system and saving it back to its original location. I have conducted extensive research using Google, but have not been able to find a suitable solution. Any help or gu ...

Routing with nested modules in Angular 2 can be achieved by using the same

Encountering a common issue within a backend application. Various resources can be accessed through the following routes: reports/view/:id campains/view/:id suts/view/:id certifications/view/:id Note that all routes end with the same part: /view/:id. ...

Combining cells through the utilization of JavaScript

I've searched for ways to merge cells in a table using JavaScript but haven't been able to find any code that works. Is there a specific approach I can follow to implement cell merging like in MS-WORD tables? Your advice would be greatly apprec ...

After pressing submit, any associated links should automatically open in a separate tab

I have a homework assignment that requires creating a form with multiple elements, including a checkbox. Once this checkbox is checked, the links displayed on the resulting page should open in a new tab. The catch? I can only use HTML and CSS - no JavaScri ...

Exploring the Possibilities of Utilizing Multiple Endpoints with Apollo Angular GraphQL

mywebsite.com/graphql/categories mywebsite.com/graphql/account mywebsite.com/graphql/connection I'm working with multiple GraphQL endpoints on my server, each one having its own mutations and queries. I want to create an Angular frontend for it bu ...

update component following an ajax request

I attempted the suggested solutions for similar questions, however, they proved ineffective. Upon inserting data into my database, I am looking to update my component (with the user remaining on the same component). I tried navigating directly to the com ...

Is there a way for me to retrieve the complete error response using the catchError method?

I'm currently testing my UI's response to a 404 message by deliberately triggering it in my application. For my backend API, I am utilizing NestJs. One of my methods for retrieving an organization is structured as follows: async findOne(organiza ...

Improving the efficiency of rendering multiple objects on a canvas

I'm currently working on developing a simulation for ants using the basic Javascript canvas renderer. Here is a snippet of the rendering code: render(simulation) { let ctx = this.ctx; // Clearing previous frame ctx.clearRect(0, ...

Webpack Is Having Trouble Parsing My JavaScript Code

I recently started using webpack and I'm struggling to get it to work properly. I haven't been able to find anyone else experiencing the same issue as me. Every time I attempt to run "npm run build" to execute webpack, I encounter this error: ER ...

Is the latest Swiper JS version compatible with outdated web browsers?

Seeking information on browser compatibility. I am interested in upgrading to the latest version 8.4.5 of Swiper JS for my project, currently using version 4.1.6. Upon examining their shared Github repository file .browserslistrc, I noticed changes that ta ...

What is the best method to achieve a width of 100% for an element (textarea) that has unspecified borders and padding, while also including the borders and padding

Native borders for input controls in various browsers often look more visually appealing compared to those set with CSS. However, the thickness of these native borders and padding can vary across different browsers, making it difficult to predict beforehan ...

Dynamic Dropdown Options in Angular 6 HTML Input Field

Looking to create an interactive HTML5 input field that guides the user with their input. Consider a scenario where the user is required to follow a specific grammar while entering text: rule: <expression> {<op><expression>} expression: ...

Material-UI's JSS does not adhere to the justify content property

Having trouble aligning elements in my React app using material-ui 1.0 and its JSS solutions. The code I've written below should center justify, but it's not working no matter how I configure it. I suspect there might be a naming issue since mat ...

Angular 5 does not recognize the function submitEl.triggerEventHandler, resulting in an error

Greetings! I am currently working on writing unit test cases in Angular5 Jasmine related to submitting a form. Below is the structure of my form: <form *ngIf="formResetToggle" class="form-horizontal" name="scopesEditorForm" #f="ngForm" novalidate (ngSu ...

Silencing the warning message from ngrx/router-store about the non-existent feature name 'router'

After adding "@ngrx/router-store" to my project, I noticed that it fills the app console in development mode and unit test results with a repeated message: The warning states that the "router" feature name does not exist in the state. To fix this, make ...

NodeJS Express throwing error as HTML on Angular frontend

I am currently facing an issue with my nodejs server that uses the next() function to catch errors. The problem is that the thrown error is being returned to the frontend in HTML format instead of JSON. I need help in changing it to JSON. Here is a snippe ...

Is it possible to implement a custom radio tab index without using JavaScript

Is it possible to apply the tabindex attribute on custom radio buttons, hide the actual input element, and use keyboard shortcuts like Tab, Arrow Up, and Arrow Down to change the value? Check out this example on StackBlitz ...

Delete the element that was generated using jQuery

Is there a way to add and remove an overlay element using JavaScript? I am struggling to get JavaScript to remove an element that was created with JavaScript. Any suggestions? Check out this JS Fiddle example Here is the HTML code: <div id="backgroun ...

How can you efficiently update another ng-model value based on a select input in AngularJS using ng-change?

<select data-ng-init="selectedItem='previewWidth = 1920; previewHeight = 1080'" data-ng-model="selectedItem" data-ng-change="GetNewData(); {{selectedItem}}"> <option value="previewWidth = 1920; previe ...