CSS RWD带有不均匀的盒子

发布于 2025-02-07 03:24:56 字数 1455 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2025-02-14 03:24:56

使用网格而不是Flex,您可能会获得更多对布局的控制。

使用网格,您可以指定要进入哪一列的列。您还可以指定系统使用第一个可用的单元格将项目放入(使用“密度”),以便即使您是,也可以填写网格将第一项放入第三个单元格。

该片段使用您给出的代码,但作为纯HTML/CSS。

它的媒体查询为600px,当然可以根据需要更改。那时,它将网格设置为具有2列而不是3列。

通过使用GAP满足50px的要求。

.App {
  font-family: sans-serif;
  text-align: center;
  gap: 50px;
  display: grid;
  grid-auto-flow: dense;
  grid-template-columns: repeat(3, 1fr);
  margin: 10px;
  width: 100%;
  max-width: 400px;
}

.App>* {
  width: 100px;
  height: 150px;
}

.App> :nth-child(3n+1) {
  transform: translateY(10px);
  grid-column: 3;
}

.App> :nth-child(3n+2) {
  transform: translateY(30px);
  grid-column: 2;
}

.App> :nth-child(3n) {
  transform: translateY(50px);
  grid-column: 1;
}

@media (max-width: 600px) {
  .App {
    grid-template-columns: repeat(2, 1fr);
  }
  .App> :nth-child(odd) {
    grid-column: 2;
    transform: translateY(10px);
  }
  .App> :nth-child(even) {
    grid-column: 1;
    transform: translateY(30px);
  }
}
<div class="App">
  <div style="background-color: green;">box1</div>
  <div style="background-color: yellow;">box2</div>
  <div style="background-color: orange;">box3</div>
  <div style="background-color: purple;">box4</div>
  <div style="background-color: blue;">box5</div>
  <div style="background-color: grey;">box6</div>
  <div style="background-color: red;">box7</div>
  <div style="background-color: brown;">box8</div>
</div>

You will probably get more control of layout using grid rather than flex.

With grid you can specify which column an item is to go in. You can also specify that the system is to use the first available cell to put an item in (using 'dense') so the grid can be filled up even though you are putting the first item into the third cell.

This snippet uses the code you have given but as pure HTML/CSS.

It has a media query at 600px which of course can be altered as required. At that point it sets the grid to have 2 columns instead of 3.

The 50px requirement is met by using gap.

.App {
  font-family: sans-serif;
  text-align: center;
  gap: 50px;
  display: grid;
  grid-auto-flow: dense;
  grid-template-columns: repeat(3, 1fr);
  margin: 10px;
  width: 100%;
  max-width: 400px;
}

.App>* {
  width: 100px;
  height: 150px;
}

.App> :nth-child(3n+1) {
  transform: translateY(10px);
  grid-column: 3;
}

.App> :nth-child(3n+2) {
  transform: translateY(30px);
  grid-column: 2;
}

.App> :nth-child(3n) {
  transform: translateY(50px);
  grid-column: 1;
}

@media (max-width: 600px) {
  .App {
    grid-template-columns: repeat(2, 1fr);
  }
  .App> :nth-child(odd) {
    grid-column: 2;
    transform: translateY(10px);
  }
  .App> :nth-child(even) {
    grid-column: 1;
    transform: translateY(30px);
  }
}
<div class="App">
  <div style="background-color: green;">box1</div>
  <div style="background-color: yellow;">box2</div>
  <div style="background-color: orange;">box3</div>
  <div style="background-color: purple;">box4</div>
  <div style="background-color: blue;">box5</div>
  <div style="background-color: grey;">box6</div>
  <div style="background-color: red;">box7</div>
  <div style="background-color: brown;">box8</div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文