Ensure that the CSS grid has a minimum span of 3 columns, and if a class object is the only item in a grid row, it

I am currently utilizing CSS grid to create the layout for my web application. I need to specify that a particular class should have a minimum grid-column span of 3 frames and extend across the entire grid row if it is the only object in that specific row.

Grid Layout

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, auto));
  grid-template-rows: 1fr min-content;
  grid-gap: 50px;

}

Class Definitions

.overalldata {
  grid-column: auto / span 3;
}

.clustergraph {
  grid-column: auto / span 6;
  justify-items: center;
}

.userinfoboard {
  grid-column: auto / span 3;
  justify-items: center;
  align-self: center;;
}

When my browser window has a full width, it can accommodate up to 19 columns. If I reduce the window size to allow only 11 columns, the userinfoboard class object must expand to fill the entire width since it is the sole object in its grid row.

How can I achieve this?

I came across an answer on a similar issue in this Stack Overflow post, but it lacks the feature of PSEUDO: min-span: span 3:

.userinfoboard {
  grid-column: 1 / -1;
  justify-items: center;
  align-self: center;;
}

Thank you for your help!

Answer №1

To achieve a grid layout using flexbox, you can manipulate the flex-basis and max-width properties along with considering the gutter spacing between items:

  • To create a grid gap, add margins to the flex items and apply negative margins to the container,

  • Set flex-grow: 1 for all flex items and use

    calc(n * var(--column) + 2 * var(--gutter))
    to span n columns,

  • Prevent overalldata and clusterdata from expanding by setting a max-width value equivalent to flex-basis.

The userinfoboard will now occupy a minimum of three columns - view demo below:

.wrapper {
  overflow: hidden;
}

.grid {
  display: flex;
  flex-wrap: wrap;
  --column: 100px; /* size of one column */
  --gutter: 20px; /* size of gap between columns */
  margin: -10px; /* remove space between container and items */
}

.grid > * {
  background: lightblue;
  border: 1px solid cadetblue;
  height: 50px;
  margin: 10px;
  flex-grow: 1; /* grow items */
}

.overalldata { /* span three columns */
  flex-basis: calc(3 * var(--column) + 2 * var(--gutter));
  max-width: calc(3 * var(--column) + 2 * var(--gutter));
}

.clustergraph { /* span six columns */
  flex-basis: calc(6 * var(--column) + 5 * var(--gutter));
  max-width: calc(6 * var(--column) + 5 * var(--gutter));
}

.userinfoboard { /* span miniumum three columns */
  background: beige;
  flex-basis: calc(3 * var(--column));
}
<div class="wrapper">
  <div class="grid">
    <div class="overalldata"></div>
    <div class="userinfoboard"></div>
    <div class="clustergraph"></div>
    <div class="overalldata"></div>
    <div class="userinfoboard"></div>
    <div class="clustergraph"></div>
    <div class="userinfoboard"></div>
  </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

Using flex and align properties to position two elements on the right and one on the left is a common layout challenge

I'm facing an issue with element positioning and text alignment. The navigation container consists of three elements: logo, title, and nav-list. You can find the code on CodePen. /**{ margin: 0; padding: 0; box-sizing: ...

Is there a way to execute a JavaScript function by clicking on an anchor tag?

Many websites, such as StackOverflow, have attempted to address this particular issue. However, the explanations provided are complex and difficult for me to grasp. I am looking for someone who can help identify my mistakes and explain the solution in simp ...

Tips for adjusting the size of nav-pills when collapsing with bootstrap 5

I'm attempting to utilize Bootstrap to create active classes that display a nav-pill/container around the active section of my webpage. It works well on larger screens, but on smaller screens with the hamburger menu active, the nav-pill stretches acro ...

Generating a dataframe with cells that have a combination of regular and italicized text using the sjPlot package functions

I've been trying to set up a basic data table where the genus name in the "Coral_taxon" column is italicized, but the "spp." part following it remains lowercase. I thought of using the expression() function for each row in "Coral_taxon," but so far, I ...

The CSS 'top' attribute is without impact

As I begin my journey with CSS, I am eager to grasp some of its behaviors. In the CSS file, the following code is defined: #spa { position : absolute; top : 8px; left : 8px; bottom : 8px; right : 8px; min-height : 500px; min-width : 500px ...

Arrangement within a span

I've been refreshing my HTML/CSS skills in preparation for a new job opportunity. The last time I dabbled in HTML was way back in 1999... Needless to say, I've fallen quite behind. As a big fan of the "Space Trader" game on Palm OS, I've tak ...

Utilizing Django to Retrieve HTML LIST Data Seamlessly Without Forms or Models

After creating an HTML page containing a series of list items, I wanted to implement a feature where clicking a button would read the data within the list and perform a transformation. I came across an example similar to this on Stack Overflow. However, t ...

Two-toned diagonal design featuring a lone character

I'm searching for a unique design featuring the letter "d" in two colors, arranged diagonally without any gradient effects. Here is the CSS code I am currently using: .gradient_text_class { font-size: 72px; background-image: linear-gradient(50de ...

Struggling with the installation of SimpLESS

I encountered an issue while attempting to install SimpLESS on my Windows 7 64bit PC. Upon running the installer, I was met with an error message stating: "Could not query info: Invalid HTTP Status Code (403)". Despite my efforts to search for a solution o ...

Is it possible to adjust the height of sibling divs within a sequence to match the tallest div?

Do you have a set of inline div containers with hardcoded widths but varying content heights? Is there a way to ensure that all divs maintain the same height without risking content overflowing from its parent container? I've experimented with inher ...

Switching the menu based on screen size successfully functions for the div element, however, it does not

Currently, I am working on creating a responsive menu. The structure of my menu is set up as follows: HTML: <ul id="top-menu"> <li class="active"> <a href="#">HOME</a> </li> <li> <a href="#div2">ABOUT</ ...

Loading HTML content in a WPF WebBrowser without encountering security messages

Currently, I am developing a WPF application in which I create the content of an HTML file as a string (including some JavaScript functions for calculations). After generating the string, I save it as an HTML file on my local disk and then reload it using ...

verified that all form checkboxes were selected in angularJS

Hey there, I have a form in AngularJS with multiple checkboxes. When the first checkbox (Check All) is clicked, I want all the checkboxes to be checked. How can I achieve this using Angular for each field loop? Below is a snippet of my code: vm.asas = f ...

CSS: Trouble with elements positioning

I am attempting to position two boxes on top of each other at the bottom of a different div. Here is the code I have: <div style = "height:400px;width:400px;border:1px solid #000;"> <div style = "position:relative;height:100px;width:100px;bor ...

Unable to adjust image opacity using jQuery

I am attempting to change the opacity of an image based on a boolean flag. The image should have reduced opacity when the var pauseDisabled = true, and return to full opacity when pauseDisabled = false. To demonstrate this, I have created a fiddle below. ...

How can I prevent text highlighting on a website?

Is there a way to lock the copy button on my site without restricting the save as button, which is activated by right click? I want users to be able to save the website as an HTML file, but prevent them from copying text. Can this be achieved using Javas ...

Scraping Secrets: Unraveling the Art of Procuring User Links

I need assistance with extracting links from the group members' page on Meetup: response.css('.text--ellipsisOneLine::attr(href)').getall() Can you please help me understand why this code is not functioning correctly? This is the HTML stru ...

Tips for leveraging Backbone.js for creating dynamic single page websites

I am eager to create my own personal website with a unique functionality similar to this example: The idea is to have the entire website contained within a single HTML page, where clicking on a navigation item will dynamically load only the necessary info ...

Is it possible to substitute responsive image placeholders with corresponding CSS for div containers?

I have successfully implemented the desired design using HTML and CSS, but I am currently using image placeholders. I am seeking to achieve a similar effect with CSS for div elements instead of images. <div class="item-box">{{ item.title }}</div ...

Include the button beneath the Rating section using JQuery or AJAX

I am having trouble adding buttons after the result.date in my code. Placing the buttons between td tags is causing an [object Object] error to show up. $.ajax({ type: 'GET', url: 'someUrl.php', data: {op : "demo"}, da ...