flexbox防止并排显示两个元素

发布于 2025-02-06 13:01:35 字数 900 浏览 3 评论 0原文

在大屏幕上,我想要1行上的所有元素,在较小的屏幕上,我想要三行。基本上,我不希望仅在同一行上使用H1看到按钮。 Flexbox如何解决此问题?谢谢:)

<div class="buttoncontainer">
      <button class="change">Change background</button>
      <h1 id="currentcolor">currentcolor</h1>
      <button class="change">Change background</button>
    </div>

和CSS:

'''

.buttoncontainer {
  display: flex;
  flex-wrap: wrap;
  height: 90vh;
  justify-content: flex-start;
}
button {
  border: 10px solid black;
  font-size: xx-large;
  text-shadow: #222;
  box-shadow: 10vh;
  margin: auto;
  text-align: center;
  line-height: 4.5;
  height: 25%;
  flex-wrap: wrap;
  border-radius: 20px;

}
h1 {
  border: 10px solid black;
  margin: auto;
}

不想要这个:不好

所以我 对此

On large screens i want all elements on 1 row, on smaller i want three rows. Basically i dont want see button only with H1 on same row. How can flexbox solve this issue? thank you :)

<div class="buttoncontainer">
      <button class="change">Change background</button>
      <h1 id="currentcolor">currentcolor</h1>
      <button class="change">Change background</button>
    </div>

And css:

'''

.buttoncontainer {
  display: flex;
  flex-wrap: wrap;
  height: 90vh;
  justify-content: flex-start;
}
button {
  border: 10px solid black;
  font-size: xx-large;
  text-shadow: #222;
  box-shadow: 10vh;
  margin: auto;
  text-align: center;
  line-height: 4.5;
  height: 25%;
  flex-wrap: wrap;
  border-radius: 20px;

}
h1 {
  border: 10px solid black;
  margin: auto;
}

SO i dont want this :Bad one

But straight to thisGOod one

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

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

发布评论

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

评论(1

熟人话多 2025-02-13 13:01:35

因此,解决此问题的最简单方法是通过媒体查询。媒体查询可以使您轻松地设置最大或最小宽度,以便以某种方式发生事物。

https:> https:https://developer.mozilla.org/en-en-us/ DOCS/WEB/CSS/MEDIA_QUERIES/used_media_queries

从我的理解中,您可以将其设置为以便每当它少于某个分辨率(在这种情况下,我设置了900px),应将flex方向属性设置为'柱子'。

@media (max-width: 900px) {
    .buttoncontainer {
        flex-direction: column;
    }
  }

这应该解决您的问题。

So the easiest way to solve this problem would be through Media Queries. Media queries make it so you can easily set a max or min width for things to happen a certain way.

https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries

From what I'm understanding, you can set it so whenever it's less than a certain resolution (in this case I set 900px), the flex-direction property should be set to 'column'.

@media (max-width: 900px) {
    .buttoncontainer {
        flex-direction: column;
    }
  }

This should solve your problem.

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