Utilizing CSS Grid to dynamically stretch and wrap rows based on adjacent siblings within a flat DOM structure

Looking for a way to create a grid layout that adjusts row sizes based on adjacent siblings. Interested in SCSS or CSS solutions using grid, flexbox, or other techniques.

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

I am working with a dynamically generated list based on configurations. The length, number, and order of items are unknown. There are certain rules like item-type-a always taking up a full row and item-type-b should stretch across the row with a maximum of 3 per row.

Is it possible to achieve this solely through CSS?

  <div class="container">
    <div class="item item-type-a">Type A</div>
    <div class="item item-type-b">Type B</div>
    <div class="item item-type-b">Type B</div>
    <div class="item item-type-a">Type A</div>
    <div class="item item-type-b">Type B</div>
    <div class="item item-type-b">Type B</div>
    <div class="item item-type-b">Type B</div>
    <div class="item item-type-b">Type B</div>
    <div class="item item-type-a">Type A</div>
  </div>

To recreate the layout pictured, I used the following CSS but need to find a way to exclude item-type-c and item-type-d.

<style>
  .container {
    background: white;
    border: dashed red 1px;
    padding: 24px;
    max-width: 1080px;
    display: grid;
    gap: 2rem 2rem;
    grid-template-columns: repeat(12, 1fr);
  }

  .item {
    background: lightgray;
    min-width: 200px;
    min-height: 100px;

    display: grid;
    place-items: center;
    text-align: center;

    font-size: 24px;
    font-family: 'Helvetica';
  }

  .item-type-a {
    grid-column: auto / span 12;
  }

  .item-type-b {
    grid-column: auto / span 6;
  }

  .item-type-c {
    grid-column: auto / span 4;
  }

  .item-type-d {
    grid-column: auto / span 12;
  }
</style>

Answer №1

.container {
  background: white;
  border: dashed red 1px;
  padding: 24px;
  max-width: 1080px;
  display: flex;
  flex-wrap: wrap;
  gap: 2rem 2rem;
}

.item {
  background: lightgray;
  min-width: 200px;
  min-height: 100px;
  display: grid;
  place-content: center;
  text-align: center;
  font-size: 24px;
  font-family: 'Helvetica';
  flex: 1 0 calc(33% - 2rem);
}

.item-type-a {
  flex-basis: 100%;
}
<div class="container">
  <div class="item item-type-a">Type A</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-a">Type A</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-a">Type A</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-a">Type A</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">>Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-a">Type A</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b&";>Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-b">Type B</div>
  <div class="item item-type-a">Type A</div>
  <div class="item item-type-a">Type A</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

Creating multiple div elements with changing content dynamically

I am facing an issue with a div named 'red' on my website where user messages overflow the 40px length of the div. To prevent this, I want to duplicate the 'red' div every time a message is sent so that the messages stay within the boun ...

Tailwind CSS - when it comes to styling, larger screen media queries are taking precedence over smaller media queries

Currently tackling screen responsiveness issues in a Nextjs + Tailwind CSS project. All Tailwind breakpoints are functioning correctly except for "sm" and below. Snippet of the problematic code: <div className="bg-red-200 sm:bg-white md:bg-gray-70 ...

Incorporate dynamic animations into text wrapping using React

Currently, I am in the process of learning React and have successfully created a flexbox with 6 child containers using the wrap property. Now, I am looking to add animation when the containers wrap onto a new line. I attempted to add a transition animatio ...

determining CSS selector

Having trouble identifying a locator using CSS. There are 10 elements with the same locator name on the webpage. Each web element has the same XPath value: //div[@class='thumb_image'] The web element list size is 10. If I want to access the 5t ...

Creating a customized design for a q-popup-edit

As I navigate through using Quasar in Vue, I stumbled upon a q-popup-edit that allows me to gather input from the user. Despite my attempts at researching via Google and reading documentation, I have hit a roadblock when it comes to customizing the style o ...

Can we modify the cell width in knitr and pandoc?

When generating an HTML file using knitr/pandoc to display multiple tables in a loop, how can you ensure that each table has the same total width despite having varying numbers of cells? --- output: html_document: theme: cosmo --- {r results ="asis", e ...

Ways to hide the value from being shown

I have integrated jscolor (from jscolor) to allow users to pick a color with an input field. However, I am facing issues in styling it as I'm unable to remove the hex value of the selected color and couldn't find any relevant documentation on av ...

Issues arising when checking the responsiveness of the navigation bar on display

Recently, I encountered an issue with the navigation bar on my webpage. It contains an image logo on the left and three links on the right. The problem arises when I test the responsiveness using different devices on Chrome; the navigation bar resizes and ...

The customized cursor image fails to load after switching the application URL from http to https

After implementing a redirect from http to https in my application, I encountered an issue with custom cursor images not displaying as intended. Prior to the redirect, the custom cursor images worked fine and were visible on the page. The URL for these cur ...

Setting the CSS position to fixed for a dynamically generated canvas using JavaScript

My goal is to fix the position style of the canvas using JavaScript in order to eliminate scroll bars. Interestingly, I can easily change the style using Chrome Inspector with no issues, but when it comes to implementing it through JS, I face difficulties. ...

Place a list with scrolling and overflow features side by side

Having trouble getting my headers to scroll with overflow auto and white-space nowrap. Can't figure out why it's not working. I want to create hyperlinks within headers to link to specific parts of the website. I have the code for hyperlinking s ...

I noticed that my CSS style sheet is only working on three of my pages. What should I do to make sure it affects the fourth page as well?

I am currently facing an issue with my website where I have linked one .css stylesheet to 3 different pages. This is just a temporary template until I finalize the individual look of each page. After performing some div positioning on the site, most pages ...

Experiencing difficulty aligning the Material UI grid to float on the right side

I'm currently in the learning phase of material-ui and encountering an issue with floating the grid content to the right side. Despite trying alignItems="flex-start" justify="flex-end" direction="row" in the container grid, as well as using the css p ...

The shadow effects and color overlays do not seem to be functioning properly in Mozilla Firefox

I have designed a popup registration form using the Bootstrap modal class. To ensure form validation, I have integrated some jQuery validation engine functionality. Additionally, I customized the appearance by adding a box shadow, adjusting the background ...

The stunning fade effect of Magnific Popup enhances the visual appeal of the inline gallery

I'm currently using magnific popup for an inline gallery, but I'm not seeing any effects. I would like to have a fade effect when opening/closing the popup. Can someone assist me with achieving this? https://jsfiddle.net/gbp31r8n/3/ [Check o ...

In a React application, adjusting the main container height to 100vh results in the window.scrollY position being set to

I was facing problems with flashing on the Safari/Firefox browsers. By setting the main container height to max-height: 100vh, I managed to resolve the flickering issue. However, I am also looking for a solution to keep track of the window.scrollY positi ...

css failing to load properly following javascript redirection

Upon clicking the button labeled id=cancel, the user will be directed to index.php. $('#cancel').click(function() { if(changes > 0){ $('#dialog-confirm').dialog('open'); }else{ window.location.href = ". ...

Internet Explorer 10 offers 3D transformation capabilities

My 3D rotating image carousel works well in Chrome and Firefox but not in IE. I am aware that IE10+ does not fully support the preserve-3d tag, and although there are workarounds by applying transforms to the children instead, I have been unsuccessful in ...

What is the best way to eliminate excess elements from overflow:hidden?

Whenever I click on a modal, it changes my body to overflow:hidden. As a result, the scrollbar disappears and my page becomes unscrollable. Once I close the modal, my body reverts back to overflow:auto allowing the page to scroll again. These functionaliti ...

The method window.scrollTo() may encounter issues when used with overflow and a height set to 100vh

Suppose I have an HTML structure like this and I need to create a button for scrolling using scrollTo. However, I've come across the information that scrollTo doesn't work well with height: 100vh and overflow: auto. What would be the best way to ...