Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an alternative to css subgrid?

I'm working on a page with a pretty simple layout - basically a data table, but using grid layout so that the columns will adjust nicely depending on content size. I want to make the rows sortable (using jQuery), so I need to find a way to wrap all the cells of a same row in a container.

display: subgrid;

I've tried using subgrid but it seems it's not much supported at the moment.. Obviously, just nesting a grid doesn't work as it won't sync the column sizes between different grids..

Any idea on how to achieve that?

Here is my pen so far: https://codepen.io/anon/pen/PEjqgx

like image 510
Buno Avatar asked Jan 01 '18 15:01

Buno


People also ask

Can I use CSS Subgrid?

CSS Subgrid on Firefox is fully supported on 71-104, partially supported on None of the versions, and not supported on 2-70 Firefox versions. CSS Subgrid on Chrome is fully supported on None of the versions, partially supported on None of the versions, and not supported on 4-106 Chrome versions.

What is sub grid CSS?

- CR. Feature of the CSS Grid Layout Module Level 2 that allows a grid-item with its own grid to align in one or both dimensions with its parent grid.

How do you make a nested grid in CSS?

Grid items can become grids themselves with CSS grid layout. You can then place grid items inside the grid item, therefore creating a nested grid. To create a nested grid, all you have to do is apply display: grid (or display: inline-grid ) to the grid item and it becomes a grid.

What is a Subgrid?

Subgrid definition Filters. (mathematics, physics) Describing any parameter measured on a scale smaller than the grid used to plot it. adjective. A grid comprising a subset of the contents of a larger grid.


2 Answers

Depending on the context, display: contents may be a viable workaround for display: subgrid.

From caniuse: (bold mine)

display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. This can be useful when a wrapper element should be ignored when using CSS grid or similar layout techniques.

The big win here is that we can keep our current markup - which groups together each row of data - while keeping components of each row synced together because they are part of just one CSS grid - the grid container.

Regarding browser support: display: contents is supported by Chrome, Firefox and iOS 11.4+.

So getting back to the OP's sample Codepen, we can make use of display: contents to implement the desired result by:

1) Moving the grid container properties to the globalWrapper div and

#globalWrapper {
  display: grid;
  grid-template-columns: 1fr 1fr max-content;
  grid-gap: 3px;
}

2) setting the rowWrapper divs with display: contents

.rowWrapper {
  display: contents;
}

Updated Codepen demo

like image 103
Danield Avatar answered Sep 23 '22 22:09

Danield


Using display:contents to simulate css subgrid is a hack: hacks add complexity — making the code harder to maintain and prone to other errors — and sooner or later they will stop working.

Beware of using display:contentsas a substitute for subgrid: there are good reasons why it should be avoided. Instead use either another Grid in the container element or Flex.

[Adendum 11/1/2020] Firefox supports Subgrid since V71, no word yet from the other browsers. There is a good explanations about the feature in codrops, and you can see some examples here. And this is the link to see the current support in all of the browsers in can I use

like image 26
ganar Avatar answered Sep 20 '22 22:09

ganar