有没有办法使网格容器的子元素不参与该子元素上的网格布局other than using 100 or 101

例如,给出一些简单的东西,比如:

body {
    display: grid;
    grid-template-columns: auto auto;
    grid-template-rows: auto auto;
}
<div>Element One</div>
<div>Element Two</div>
<div>Element Three</div>
<div>Element Four</div>
<custom-web-component id="exemptedElement">Exempted Element</custom-web-component>

有没有可能通过css或其他方式告诉最后一个元素不要参与网格布局?

我知道我可以在该元素上设置position: fixed;,它也会这样做,但这会在该组件中产生下游影响(例如,带有position: absolute;的子体将被锁定到视区,而不是预期的文档).

我知道我也可以在那个元素上设置display: contents,但它不会参与,将布局推迟到它的子元素……但这也会对下游产生影响,这意味着这个主要组件的子组件都需要小心上面的固定/绝对定位问题.

Note: I do not have control over the fact that the custom-web-component is sometimes being added to a grid container. So I'm trying to find some way to "Break out" of it when that does happen.

推荐答案

如果不能使用绝对/固定或display: contents,则可以通过将元素高度设置为0来对行为进行approximate操作,该元素跨越所有列,并在末尾有一个较大的间隔顺序.

我坚持认为这只是一个近似值,因为我认为在你的所有限制下,这是不可能的.

body {
  display: grid;
  grid-template-columns: auto auto;
  grid-template-rows: auto auto;
}

custom-web-component {
  grid-column: 1/-1; /* span all the possible columns */
  height: 0;
  order: 99;
  
  /* to illustre */
  outline: 1px solid red;
}

body {
  outline: 1px solid blue;
}
<div>Element One</div>
<div>Element Two</div>
<custom-web-component id="exemptedElement">Exempted Element</custom-web-component>
<div>Element Three</div>
<div>Element Four</div>
<div>Element five</div>

Css相关问答推荐

如何在CSS calc()中使用反向百分比/无单位计算?

在COLOR-MIX()中使用var()似乎会使其对于不支持COLOR-MIX()的浏览器具有可读性,从而阻止了回退的工作

Angular 17 Animation -在幻灯片左/右两侧同步

向左或向右浮动时的不同图像边距

指定from_max和to_min值时隐藏Shiny sliderTextInput下的行

如果一个类紧挨着另一个具有特定类的元素,请使用特殊的CSS,只使用不带JS的CSS

将数据从react组件发送到css文件

基于高度的容器查询不起作用

Mat table - 保留第一列和复选框

Tailwind css 和 Next.js 导航组件 z-index 不工作

为什么这个绝对定位的 ::after 伪元素会崩溃,除非父级有过滤器?

Ant Design:将位置子菜单移动到屏幕顶部

从 Angular SCSS 生成 CSS?

在表格中隐藏绝对伪元素

使用 CSS 剪辑/裁剪背景图像

CSS:将背景图像拉伸到屏幕的 100% 宽度和高度?

Angular 动画的目的是什么?

在 CSS 中使用多个 @font-face 规则

CSS:悬停一个元素,多个元素的效果?

如何在 CSS 中给出背景图像路径?