简体   繁体   English

根据通用容器大小更改网格布局(不使用类)

[英]change grid layout based on generic container size (not using classes)

In the following example I have two containers for a flex item.在下面的示例中,我有两个用于弹性项目的容器。 When the container is 300px I want the two 150px boxes to be side by side (as seen in the example).当容器为 300px 时,我希望两个 150px 的盒子并排放置(如示例所示)。 When the container is 150 however I'd like to change the container display to block to have them stacked on each other (we shouldn't see the yellow below).但是,当容器为 150 时,我想将容器显示更改为block ,以使它们相互堆叠(我们不应该看到下面的黄色)。

Is it possible to write CSS WITHOUT leveraging the example_1 or example_2 classes and just referencing their container?是否可以在不利用 example_1 或 example_2 类并仅引用其容器的情况下编写 CSS? I tried Chromes @container query but this didn't work... any help appreciated!!我尝试了 Chromes @container查询,但这没有用......感谢任何帮助!!

 .example_1 { width: 300px; height: 30px; background-color: yellow; }.example_2 { width: 150px; height: 60px; background-color: yellow; }.flex-thing { display: flex; flex-direction: row; width: max-content; justify-content: stretch; }.flex-thing.item { background-color: red; width: 150px; height: 30px; border: solid 1px black; }
 <div class="example_1"> <div class="flex-thing"> <div class="item"></div> <div class="item"></div> </div> </div> <br> <div class="example_2"> <div class="flex-thing"> <div class="item"></div> <div class="item"></div> </div> </div>

If you want the red box to auto wrap down to second line, you need to add flex-wrap: wrap;如果你想让红框自动换行到第二行,你需要添加flex-wrap: wrap; to the container.到容器。 or you can add或者你可以添加

.example_2 .flex-thing{
   flex-direction: column;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM