HTML CSS响应式设计中间4/0或2/2,但没有3/1

发布于 2025-01-23 10:39:52 字数 2009 浏览 3 评论 0原文

我目前已经制作了这样的按钮(我已经取出了无用的东西来重现问题):

CSS:

    .containerWelcome{
        margin: 0;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    .buttonsWelcome {
        width: 200px;
    }
    .disabled {
      opacity: 0.6;
      cursor: not-allowed;
    }

html:

        <div class="containerWelcome">
            <button name="button1" onclick="location.href=something0"
                    class="buttonsWelcome" type="submit"
                    value="something0">something0
            </button>
            <button name="button2" onclick="location.href=something"
                    class="buttonsWelcome" type="submit"
                    value="something">something
            </button>
            <button name="button3" onclick=""
                    class="buttonsWelcome disabled" type="submit"
                    value="somethingelse0">somethingelse0
            </button>
            <button name="button4" onclick=""
                    class="buttonsWelcome disabled" type="submit"
                    value="somethingelse1">somethingelse1
            </button>
        </div>

如果我不放大,我将彼此相邻四个按钮。这很好。 如果我放大一点,或者我在另一台较小的笔记本电脑上,我有3个按钮,一个按钮下面。这有点丑陋。 如果我的缩小甚至更多,那么我有2个按钮的两个按钮。这很好。除了它们不是完全集中的,这有点奇怪。我想那是因为它们在容器的左侧?

我的主要问题是:我想从中央容器顶部从4个按钮到2个按钮,而在中间容器的底部则2个,而没有笨拙的阶段,顶部有3个按钮中央容器的中心容器的一个。

我还正在寻找一种将按钮置于容器中心的方法,但我想这并不重要。

I currently have made my buttons like so (I've taken off the useless stuff to reproduce the problem):

CSS:

    .containerWelcome{
        margin: 0;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    .buttonsWelcome {
        width: 200px;
    }
    .disabled {
      opacity: 0.6;
      cursor: not-allowed;
    }

HTML:

        <div class="containerWelcome">
            <button name="button1" onclick="location.href=something0"
                    class="buttonsWelcome" type="submit"
                    value="something0">something0
            </button>
            <button name="button2" onclick="location.href=something"
                    class="buttonsWelcome" type="submit"
                    value="something">something
            </button>
            <button name="button3" onclick=""
                    class="buttonsWelcome disabled" type="submit"
                    value="somethingelse0">somethingelse0
            </button>
            <button name="button4" onclick=""
                    class="buttonsWelcome disabled" type="submit"
                    value="somethingelse1">somethingelse1
            </button>
        </div>

And if I don't zoom in, I have the four buttons next to each other. Which is good.
enter image description here
If I zoom in a little bit or if I'm on another smaller laptop I have 3 buttons and one below them. Which is sort of ugly.
enter image description here
If I zoom even more, I have 2 buttons on top of 2 others one. Which is good. Except, they're not perfectly centered, which is a bit weird. I guess that's because they're at the left of the container?
enter image description here

My main issue is: I would like to go from 4 buttons to 2 on top of the center container and 2 at the bottom of the center container WITHOUT having the awkard stage where there are 3 buttons on top of the center container and one at the botton of the center container.

I also am looking for a way to center the buttons in the center of the container, but that's less important I guess.

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

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

发布评论

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

评论(2

别挽留 2025-01-30 10:39:52

尝试此代码:

.containerWelcome{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display:flex;
  
}
.buttonsWelcome {
  width: 200px;
}

@media only screen and (max-width: 600px)  {
  .containerWelcome{
     flex-wrap: wrap;
  }

  .buttonsWelcome {
    flex: 50%;
  }
}

try this code:

.containerWelcome{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display:flex;
  
}
.buttonsWelcome {
  width: 200px;
}

@media only screen and (max-width: 600px)  {
  .containerWelcome{
     flex-wrap: wrap;
  }

  .buttonsWelcome {
    flex: 50%;
  }
}
玻璃人 2025-01-30 10:39:52

最简单的方法是仅使用显示flex,不用担心,学习很容易学习,检查下面的解决方案并用它替换您的.containerwelcome样式。

.containerWelcome {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 410px;
  margin-inline: auto;
  gap: 5px;
}

the easiest way is to just use display flex, don't worry it's to simple to learn, check the solution below and replace your .containerWelcome style with it.

.containerWelcome {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 410px;
  margin-inline: auto;
  gap: 5px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文