What is the best way to create a recurring design in a CSS grid?

Looking to create a unique CSS grid layout with a specific pattern:

  • Display 4 elements in a row, then display 2 in a row, followed by 12 (as 4 in a row), and finally 2 more elements in a row. After that, the whole pattern repeats. Here is an image of the first iteration: https://i.sstatic.net/z45gn.png

Currently experimenting with a CSS grid solution but facing some challenges. Try it out on FiddleJS: https://jsfiddle.net/g15nyto6/42/

.parent {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  /* grid-auto-rows: 50px; */
  grid-column-gap: 5px;
  grid-row-gap: 5px;
  grid-auto-flow: dense;
}

.child {
  background: tomato;
  width: 100%;
  height: 100%;
}

.child:nth-child(-n + 2) {
  background: cadetblue;
  grid-column: span 2;
  grid-row: span 2;
}

.child:nth-child(5n) {
  background: pink;
  grid-column: span 1;
  grid-row: span 2;
}
<div class="parent">
  <div class="child">child-1</div>
  <div class="child">child-2</div>
  <div class="child">child-3</div>
  <div class="child">child-4</div>
  <div class="child">child-5</div>
  <div class="child">child-6</div>
  <div class="child">child-7</div>
  <div class="child">child-8</div>
  <div class="child">child-9</div>
  <div class="child">child-10</div>
  <div class="child">child-11</div>
  <div class="child">child-12</div>
  <div class="child">child-13</div>
  <div class="child">child-14</div>
  <div class="child">child-15</div>
  <div class="child">child-16</div>
  <div class="child">child-17</div>
  <div class="child">child-18</div>
  <div class="child">child-19</div>
  <div class="child">child-20</div>
  <div class="child">child-21</div>
  <div class="child">child-22</div>
  <div class="child">child-23</div>
  <div class="child">child-24</div>
  <div class="child">child-25</div>
  <div class="child">child-26</div>
  <div class="child">child-27</div>
  <div class="child">child-28</div>
  <div class="child">child-29</div>
  <div class="child">child-30</div>
  <div class="child">child-31</div>
  <div class="child">child-32</div>
</div>

Any assistance or suggestions would be greatly appreciated.

Answer №1

For every group of 20 children, select the 5th, 6th, 19th, and 20th child using the :nth-child() formula as follows:

.child:nth-child(20n + 5),
.child:nth-child(20n + 6),
.child:nth-child(20n + 19),
.child:nth-child(20n + 20) {}

Give it a try:

const parent = document.querySelector('.parent');

for (let i = 0, l = 40 + Math.random() * 40 | 0; i < l; i++) {
  const child = document.createElement('div');
  child.classList.add('child');
  child.textContent = `child-${i + 1}`;
  parent.appendChild(child);
}
.parent {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
}

.child {
  background: tomato;
  height: 20px;
}

.child:nth-child(20n + 5),
.child:nth-child(20n + 6),
.child:nth-child(20n + 19),
.child:nth-child(20n + 20) {
  background: cadetblue;
  grid-column: span 2;
}
<div class="parent"></div>

Answer №2

To achieve this effect, you need to define a grid with 4 columns...

UPDATE: The instructions have been revised and the image now provides a clearer understanding of the requirement. Here is the updated version:

...then observe that all content resides in a single cell unless it meets the criteria of :nth-child(20n+5), :nth-child(20n+6), :nth-child(20n+19), or :nth-child(20n+20). For these specific cases, set the column span to 2.

Below is a simple code snippet with additional elements to emphasize the repeating pattern:

<style>
  .parent {
    display: grid;
    width: 100vw;
    grid-template-columns: repeat(4, 1fr);
  }
  
  .child:nth-child(20n + 5),
  .child:nth-child(20n + 6),
  .child:nth-child(20n + 19),
  .child:nth-child(20n + 20) {
    grid-column: span 2;
  }
</style>
<div class="parent">
  <div class="child">child-1</div>
  <div class="child">child-2</div>
  <div class="child">child-3</div>
  ...
  <div class="child">child-42</div>

</div>

Answer №3

Both of you are on the right track towards solving this puzzle. Based on the image provided, the 5th and 6th cells should be wider, along with another set of cells that are 14 units apart (12 plus the two wider ones).

Here is the code to achieve this:

.child:nth-child(14n + 5), .child:nth-child(14n + 6) {
    grid-column: span 2;
}

The following snippet from the original code needs to be removed:

.child:nth-child(-n + 2) {
  grid-column: span 2; 
  grid-row: span 2;
}

.child:nth-child(5n) {
  grid-column: span 1; 
  grid-row: span 2;
}

Update
To maintain the pattern (4->2->12->2->4->2->12->2) mentioned in the comments, use the following code:

.child:nth-child(20n + 5), .child:nth-child(20n + 6), .child:nth-child(20n + 19), .child:nth-child(20n + 20)  {
    grid-column: span 2;
}

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

Angular components are not receiving the Tailwind CSS attributes applied to :root classes

In my Angular 12 project, I have integrated Tailwind CSS. The structure of the package.json is as below: { "name": "some-angular-app", "version": "0.0.0", "scripts": { "ng": "ng&quo ...

Show/Hide Section

I am struggling to implement two buttons - one for expanding a div and one for collapsing it. Despite multiple attempts to modify the code, I have not been successful. It appears that I am having trouble understanding how to prevent a link from toggling. ...

Tips for adjusting the angle in SVG shapes

I have designed an svg shape, but I'm struggling to get the slope to start from the middle. Can someone please provide assistance? <svg xmlns="http://www.w3.org/2000/svg" fill="none"> <g filter="url(#filter0_b_1_2556)"&g ...

CSS - changing images for the menu based on user interaction

I'm in the process of designing a website and I have a unique challenge - I need to figure out how to create a dynamic triangle below an item on the menu bar using CSS. The catch is that the horizontal position of this triangle will shift based on wh ...

What is typically regarded as the optimal size for a full-screen background image?

What is the ideal image size for a full-screen background? I currently have an image that is 1920x1080 pixels, but I'm unsure if that's suitable for desktop screens. Can you provide guidance on this? ...

The CSS hover effect on a <td> element is malfunctioning and not working as expected

My goal is to create a dropdown list embedded within each td cell of a table, similar to this setup: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> ...

Can a div be aligned in the center with an "absolute" parent element?

<style type="text/css"> .square { width:251px; height:207px; border: 1px solid #d6d6d6; -webkit-box-shadow: 1px 1px 3px rgba(0,0,0,.1); -moz-box-shadow: 1px 1px 3px rgba(0,0,0,.1); box-shadow: 1px 1px 3px rgba(0,0,0,.1); ...

Issue with Firefox position: absolute not producing the desired outcome

I am working on a webpage where I want to put a label over an image. I was able to achieve this using position: absolute for the label and float: left for the img tag. This method worked perfectly in most browsers, except for Firefox (both version 3 and 4) ...

Is it possible to create clickable titles in highcharts that also interact with bootstrap popovers?

Hello, I'm trying to figure out how to turn the title in highcharts into a clickable link that works with bootstrap's popover feature. You can find an example of what I'm working on here: http://jsfiddle.net/287JP/4/ $(function () { ...

Using Material UI, I have styled a typography element and a button to appear on the

Struggling to position a button and a <Typography> element side by side: Here's what I currently have: <Grid item lg={12} md={12} sm={12} xs={12} className={classes.register}> <Typography noWrap className={classes.typoText} varian ...

Troubleshooting: Rails Submit Button Not Responding to Bootstrap's btn-block Class

Currently, I am encountering an issue with the btn-block class while styling my Rails application using the bootstrap-sass gem specifically in a new user registration form. Outlined below is the relevant code... <%= link_to "Sign in with Facebook", ...

``There Seems to be an Issue with Loading Content in Tabs

Hey! I'm encountering an issue with my jquery tabs. The first tab content loads fine, but when I select another tab, the content doesn't display. Then, when I try to go back to the first tab, it doesn't load either. Here's the HTML cod ...

How can I utilize CSS shapes to create clickable images?

I'm facing a challenge in making an input field and button functional even when they are positioned behind a uniquely shaped PNG image. https://i.stack.imgur.com/eOg0N.jpg Initially, I believed that by applying a shape to the image, it would automat ...

html2canvas does not support integration with asp.net

Feeling a bit lost here. I've been reassured that the jquery code below is solid, but for some reason html2canvas just won't cooperate. When I click the button, nothing happens at all. Other methods with the button seem to work fine. Can anyone s ...

I crafted this dropdown menu, but for some reason, the selections aren't registering when clicked. Below is the code I used. Any assistance would be greatly appreciated!

Hi there, I need some help with getting my code to run properly. I've created a dropdown box using HTML and CSS, but it seems like there's an issue with the JavaScript portion as the options are not being selected. I've included a code snipp ...

Display CSS specifically to the super administrator

I am trying to customize the appearance of certain tables in the WordPress dashboard using CSS so that they are only visible to super admins. <?php is_super_admin( $user_id ); ?> add_action( 'admin_head', 'my_custom_function' ); ...

Bug in Chrome 89 causing flex elements to overlap

Recently, I encountered an issue with my list items overlapping in the latest version of Chrome 89 To display the list, I am using flex-direction: column-reverse;. Does anyone have any suggestions on how to fix this problem? Chrome 89 https://i.sstatic. ...

Tips for incorporating a Python script into a online project

I've been working on a Python code that detects faces and eyes using face recognition. When the code runs in PyCharm, it displays a camera window. Now I'm trying to figure out how to integrate this window into a webpage project written in HTML, C ...

Stylish Interactive Menu Item

Having a fixed navigation at the bottom of my website has presented me with an issue. The hover effect, specifically the background slide, triggers before I even place my mouse over the text. Currently, the background slides up when my mouse enters the .it ...

Centered Tailwind CSS logo design

Here is my current fiddle: https://jsfiddle.net/w5c36epd/ Upon close inspection, you may notice that the logo is not properly centered. I have experiment with various flexbox options such as: <div class="flex-shrink-0 justify-center"> ...