csswg-drafts icon indicating copy to clipboard operation
csswg-drafts copied to clipboard

[css-grid] Property "grid-item-of": display any element as part of any given grid

Open tobireif opened this issue 8 years ago β€’ 54 comments

I want to be able to display any element located anywhere in the HTML tree as part of any given grid (without changing the HTML / the DOM).

For example:

#foo {
  display: grid;
}
#bar {
  display-as-grid-item-of: "#foo";
  /*
  Where to place it in grid #foo :
  */
  grid-row: 5;
  grid-column: 3;
}

Update: A more complete example:

.any_selector.pointing_to_the_grid {
  display: grid;
  /* Any additional normal Grid stuff here: */
  grid-template-columns: repeat(2, auto);
}
.any_selector.pointing_to_the_element {
  /* The new feature: */
  grid-item-of: .really_any_selector. pointing.to_the_grid;
  /* The element gets auto-placed in the grid as if it were appended as grid child. */
  /* Optional: Where to place it in the Grid: */
  grid-row: 1;
  grid-column: 2;
  /* Any additional Grid item properties here. */
}

tobireif avatar Apr 11 '18 10:04 tobireif

This is similar what's suggested in Eric Meyer's example: https://21fb920ea3b040f8acad2b67bc992b92.production.codepen.plumbing/ericmeyer/subgrid-inner-alignment.html

Which is linked from https://codepen.io/rachelandrew/project/full/68cc7a5da9cfbb56c6e8366d7d92e6ba/XWYGMD/ (via https://github.com/w3c/csswg-drafts/issues/2280#issuecomment-380078275).

mrego avatar Apr 11 '18 10:04 mrego

I don't think that subgrid would provide the full flexibility of my wish. I'll be happy to be proven wrong though πŸ˜€

Subgrid is looking good for the listed use cases, but my wish is not mainly about lining up stuff, instead it's more about "reordering" across the complete DOM tree.

To illustrate:

This

<body>
  <div id="foo">
    <div>some</div>
    <div>content</div>
    <div>here</div>
  </div>
  <div id="bar">also some content</div>
</body>

plus this

#foo {
  display: grid;
  grid-template-columns: repeat(2, auto);
}
#bar {
  display-as-grid-item-of: "#foo";
  /*
  Where to place it in grid #foo :
  */
  grid-row: 1;
  grid-column: 2;
}

would make #bar appear in the grid #foo, as grid item of grid #foo, as if the markup would have been

<body>
  <div id="foo">
    <div>some</div>
    <div>content</div>
    <div>here</div>
    <div id="bar">also some content</div>
  </div>
</body>

Element #bar gets laid out as part of grid #foo, in this example at the location grid-row: 1; grid-column: 2 . (If grid-row and grid column would not be specified, the element would get laid out as if it would have been added as last child of #foo).

This feature would allow the CSS Grid user to place any element located anywhere in the page DOM into any layout grid.

It might be a feature for Grid 2 or Grid 3.

tobireif avatar Apr 11 '18 10:04 tobireif

The example I linked, dunno why it only works if linked from @rachelandrew's codepen) is the one called Inner alignment. It's not only subgrids, but it's about named grids, check the proposed CSS there:

  #outer {display: grid(pagecolumns);}
  #inner .thing1, #inner .thing2 {display: pagecolumns;}

If I got it correctly that would be naming a grid container and referring it from somewhere else.

mrego avatar Apr 11 '18 11:04 mrego

Interesting.

That would make .thing1 and .thing2 get laid out as part of the grid with ID #outer, right? That might fulfil my wish.

Is it a requirement of that proposed feature that #inner is a descendant of #outer? (As their names might imply.) In the feature I want there'd be no such restriction. The element that gest moved into the given grid could be located anywhere in the whole page DOM - it could be part of a sibling tree, or it could even be a grandchild of the given grid. (One obvious restriction would be that you can't place eg the grid itself into that same grid.)

Another aspect: The grid #outer already has a "name" (something to refer to it / a selector), so I'd prefer my syntax where it's not necessary to create an additional name for the grid into which the other element gets moved. eg

#some_selector {
  display: grid;
  grid-template-columns: repeat(2, auto);
}
#some_other_selector {
  display-as-grid-item-of: "#some_selector";
  /*
  Where to place it in the grid #some_selector :
  */
  grid-row: 1;
  grid-column: 2;
}

But the syntax display: grid(foo) with display: foo would be OK for me as well if the effect is the same - that the element with display: foo gets laid out as grid item of the element which has display: grid(foo), and that it's possible (optionally) to specify the grid-row and grid-column in which the "layout-moved" item should appear (this is crucial - when I move an element into a Grid I need to be able to specify where exactly that Grid item should appear).

Is there a ticket for that other feature/syntax? We should cross-link them.

tobireif avatar Apr 11 '18 11:04 tobireif

This functionality would be a really useful addition to Grid.

tobireif avatar Apr 16 '18 08:04 tobireif

I like the approach suggested in @mrego's comment very much! From the first look, it seems that the only requirement that the grid items should come in the source document after the element that defines the grid (either as its descendants, its following siblings or any of its ancestor's following siblings) is enough to prevent any circularity. These grid items would be taken out of the flow (just as if they were absolutely positioned) and placed into the well-defined visual context. Sounds great!

The only downside I can see is the discrepancy between source order and visual order, but this problem is not new for CSS Grid, so I'm sure browsers find a way to solve it, and that solution would apply to this proposal, too.

SelenIT avatar May 04 '18 08:05 SelenIT

Since I'm thinking of it...

The CSS specs themselves would strongly benefit from this. Right now the ToC sidebar is positioned using a fairly hacky combo of position:fixed (on the sidebar) and a big left margin (on the body), and we have to be really careful that things work out properly. With this we could just make html into a two-column grid, and pull the ToC up into the first column, and put body in the second.

(Alternately, the old Template Layout idea of being able to have a grid item that was just "the rest of the stuff all flowed together" (the @ cell) would work here, since the ToC is top-level in body - we'd make body a grid, just make ToC a normal grid item, and make the second column a "catch-all" cell to get the rest of body's contents.)

The reason we can't just use Grid right now is that we have to support both "inline" ToC (between the Abstract/Status and the rest of the spec) and sidebar ToC, so we can't just put the ToC and the rest of the content into two container elements. (Tho, if we had a way to span across the entire implicit grid (#2402), we could just use the existing markup, and let all the non-ToC elements just create a bunch of implicit rows...)

tabatkins avatar May 14 '18 19:05 tabatkins

This sounds like a use-case that should be addressed in an updated CSS Regions spec. It breaks down as (a) defining a named region that is laid out as a grid area, and (b) defining a container element to flow into that region.

Regions was all about creating a layout tree that is independent from the DOM tree, and which can be dynamically adjusted. The current spec was abandoned, as far as I understand, because of objections to its reliance on dummy markup to define the layout tree. But the rest of the spec is still worth considering. For many use-cases, including this one.

AmeliaBR avatar May 14 '18 23:05 AmeliaBR

This sounds like a use-case that should be addressed in an updated CSS Regions spec.

I sincerely don't think that that's the case.

I simply want to be able to display any element as grid-item of any given grid - no matter where it is located in the DOM tree (with the exception of eg the grid parent element). That's all - there's no region and no flowing into regions.

When I write eg this

#the_grid {
  display: grid;
  grid-template-columns: repeat(2, auto);
}
#the_element {
  display-as-grid-item-of: "#the_grid";
  /*
  Where to place it in the Grid :
  */
  grid-row: 1;
  grid-column: 2;
}

... #the_element becomes a Grid item of #the_grid, as if it were a DOM tree child of #the_grid. That's all. The result should be the same as if #the_element had been moved from its location in the DOM tree to become a child of #the_grid (eg always as last child - it can still get moved/positioned in that grid layout using grid-row etc). Nothing less should happen, and definitely nothing more - thus it is purely a Grid wish (which could be generalized so that it can also be used with Flexbox) and is not a Regions feature or wish.

And yes, Regions should also get finished πŸ˜€

tobireif avatar May 15 '18 10:05 tobireif

The CSS specs themselves would strongly benefit from this.

Glad to hear that!

And I think that many more projects could benefit from the new feature.

There's a given DOM tree, and Grid gives us the freedom to lay out eg a sub-tree in nearly any possible way (yay!) But when the design process identifies that eg one element from outside of that sub-tree should get displayed as part of that mentioned grid, then that currently could only be solved by moving it in the DOM tree (eg using JS). That's a bit hacky - the beauty of Grid, Flexbox, etc is that the DOM tree does not have to get modified in order to apply the layout.

(I'm aware that I'm mostly preaching to the choir πŸ˜€ but reasoning about the feature can help with crystallizing the details.)

The example from above, with a more general property name display-as-layout-item-of:

#the_grid {
  display: grid;
  /* Any additional normal Grid stuff here: */
  grid-template-columns: repeat(2, auto);
}
#the_element {
  /* The new feature: */
  display-as-layout-item-of: "#the_grid";
  /* Where to place it in the Grid: */
  grid-row: 1;
  grid-column: 2;
}

I hope that the new functionality will get added to Grid πŸ˜€

tobireif avatar May 15 '18 10:05 tobireif

Honestly, I also wouldn't like to make this issue too broad by adding more generic concepts like Regions to its scope. I'd rather leave it a Grid-specific issue, since Grids (unlike most other CSS mechanisms) is "container-first", so once a grid is defined, it can potentially create "slots" with the definite size and position for elements that are not loaded yet. And since the structure of the grid is well-defined, I don't see strict reasons why these elements necessarily have to be direct children of the grid container.

The only note regarding the proposed syntax, I don't like identifying the grid by the ID of the element in the source document language. I'd prefer naming grids in CSS only, like in @mrego's example above or something like this:

#the_grid {
  display: grid;
  /* Naming the Grid with the CSS identifier, similarly to naming grid areas etc.: */
  grid-name: my-super-grid;
  /* Any additional normal Grid stuff here: */
  grid-template-columns: repeat(2, auto);
}
#the_element {
  /* The new feature β€” attaching the element to the named grid: */
  grid-item-of: my-super-grid;
  /* Other grid placements properties as usually: */
  grid-row: 1;
  grid-column: 2;
}

SelenIT avatar May 15 '18 14:05 SelenIT

Thanks for you input!

once a grid is defined, it can potentially create "slots" with the definite size and position for elements that are not loaded yet.

You're referring to the loading phase, but just in case someone might misunderstand the above:

Just to clarify:

The element would get rendered as if it had been appended to the children elements of the grid container element. There's no need for the web developer to create "slots" with the definite size and position - the layout-moved element would simply get rendered as if it were a DOM tree child of the Grid container element. It would get placed via auto-placement just as if it would be an actual DOM tree sibling of the other grid items in that grid. Optionally, the layout-moved item could get positioned using eg grid-row and grid-column.

I don't like identifying the grid by the ID of the element in the source document language.

You could use any selector πŸ˜€and you could easily avoid using IDs.

I'd prefer naming grids in CSS only

Selectors are CSS πŸ˜€(The ID selectors in my above example are just plain CSS selectors.)

Since we already have a system for pointing at elements (CSS selectors) we don't have to (and thus should not) add an additional system for pointing at elements. And we don't have to (and thus should not) require the web developer to create names. We can already point at the grid (using CSS selectors) - without having to add the line grid-name: the-name.

.any_selector > .pointing_to_the_grid {
  display: grid;
  /* Any additional normal Grid stuff here: */
  grid-template-columns: repeat(2, auto);
}
footer .any_selector .pointing_to_the_element {
  /* The new feature: */
  grid-item-of: ".really_any_selector .pointing.to_the_grid";
  /* The element gets placed in the grid as if it were appended to the grid children. */
  /* Optional: Where to place it in the Grid: */
  grid-row: 1;
  grid-column: 2;
  /* Any additional Grid item properties. */
}

tobireif avatar May 15 '18 17:05 tobireif

@tobireif, yes, by mentioning "slots" for future elements (implicitly) created by the Grid I just wanted to note that the proposed extension for the Grid mechanism is quite progressive-loading-friendly (which may be important for the implementers). Thanks for the clarification!

But regarding referencing the elements by CSS selectors I'm not convinced yet, AFAIK, CSS never used selectors as values before, it would be a new idiom for CSS parsers and could be therefore harder to implement than reusing the common pattern of naming things in CSS. Also, selectors can change if DOM is modified dynamically, while grid names assigned through CSS property would be unaffected in the same case. And if at some point in the future it becomes possible for the element to establish more than one grid (this idea is cropping up from time to time), then referencing the grid by name would be more unambiguous and flexible than referencing its originating element by selector.

SelenIT avatar May 15 '18 18:05 SelenIT

CSS never used selectors as values before,

Not technically true, but at least there it's limited to ID selectors.

That said, I still agree that declaring names manually is better.

tabatkins avatar May 15 '18 18:05 tabatkins

I, for one, would be estactic if we could have grid slots as regions that anything could flow into. I think it is a fantastic pairing. A lot of work has already gone into regions before it entered its deep slumber, and finally we have a good existing mechanism (grid) for creating the regions without the extra markup that distracted so many people.

bradkemper avatar May 15 '18 23:05 bradkemper

The current spec was abandoned, as far as I understand, because of objections to its reliance on dummy markup to define the layout tree.

No, Chrome killed it by declaring they weren’t interested in implementing it.

bradkemper avatar May 15 '18 23:05 bradkemper

CSS never used selectors as values before

That's not an argument against it πŸ˜€

Having a selector as value looks unfamiliar, but that's not an issue.

it would be a new idiom for CSS parsers

Every CSS implementation supports CSS selectors.

By the way, the quotes probably aren't necessary:

grid-item-of: .the_grid;

Also, selectors can change if DOM is modified dynamically, while grid names assigned through CSS property would be unaffected in the same case.

When naming a grid you're always binding it to an element, right? ... to which you point using a CSS selector πŸ˜‰

if at some point in the future it becomes possible for the element to establish more than one grid (this idea is cropping up from time to time), then referencing the grid by name would be more unambiguous and flexible than referencing its originating element by selector.

If that will indeed get specd, then that is a good reason for naming.

Pointing to eg the grid named "grid_two" of an element could be done using something like this

grids-of(.the_grid_element)[grid_two]
/* grids-of(.selector)[name] */

... or using the (if I understood you correctly) global names you're suggesting.

My main wish is the functionality enabling us to display any element as grid item of any grid. I'll sure take it - no matter whether it requires me to declare names (even if there's just one grid for the grid parent); or whether it allows me to point at the respective grid parent using a selector without having to declare names when there's just one grid for that element.

tobireif avatar May 16 '18 08:05 tobireif

I, for one, would be estactic if we could have grid slots as regions that anything could flow into.

Unfortunately that's out of scope for this here issue. My ticket is a feature suggestion of extremely narrow scope: it does nothing else but have the same layout-effect as if you'd move one element into a grid tree - that's all. This here feature must keep that minimal scope. I'm sure you'll understand. Please (really and sincerely) feel free to create a separate ticket for your related but separate wish. Thanks!

If you're lucky, @AmeliaBR will also support the ticket / contribute to it.

I, for one, would be estactic if we could have grid slots as regions that anything could flow into. I think it is a fantastic pairing. A lot of work has already gone into regions before it entered its deep slumber, and finally we have a good existing mechanism (grid) for creating the regions without the extra markup that distracted so many people.

Please don't forget to include these great points in the new ticket.

And I also hope that Regions itself will get finished and implemented!

Perhaps your suggestion could get filed as suggestion for the CSS Regions spec (this ticket here is prefixed with "[css-grid]" and applies only to the Grid spec).

Sincerely: Good luck with your Regions feature suggestion!

tobireif avatar May 16 '18 08:05 tobireif

When naming a grid you're always binding it to an element, right? ... to which you point using a CSS selector

My point here was that the same element can match different CSS selectors in different moments of time (e.g. div:nth-child(2) becomes div:nth-child(4) if two more divs are dynamically inserted before it). To maintain the reference to the same grid, you will have to update this selector after any DOM change. With manually named grid, this would not be the case β€” the DOM and CSS concerns are separated better.

My main wish is the functionality enabling us to display any element as grid item of any grid.

Mine too. That's why I'm for the syntax that would be (probably) easier and faster to implement! :)

SelenIT avatar May 16 '18 08:05 SelenIT

My point here was that the same element can match different CSS selectors in different moments of time

When naming a grid you're always binding it to an element, right?

... to which you point using a CSS selector.

That selector can get broken just as well, right?

I'm for the syntax that would be (probably) easier and faster to implement! :)

CSS selectors are implemented already.

But I'm still open to offering naming (eg in addition to selectors or as sole ref mechanism), especially for cases where there actually are more than one grid for the given grid parent.

tobireif avatar May 16 '18 08:05 tobireif