对于flexbox,axt-height属性被忽略

发布于 2025-02-12 05:56:54 字数 2397 浏览 0 评论 0原文

我有一个基于弹性箱的页面,该页面会成比例地增长和收缩。我想将其最大高度限制在屏幕高度上,以便当它达到屏幕高度时,它不会增长,但会在左右添加空间。

我尝试将最大高调添加:100VH保证金:0自动 body ,但这无济于事。

<body class="center">
  <div class="container">
    <div class="menu center">menu</div>
    <div class="game">
      <div class="content">
        <div class="grid">
          <div class="cell center">1</div>
          <div class="cell center">2</div>
          <div class="cell center">3</div>
          <div class="cell center">4</div>
          <div class="cell center">5</div>
          <div class="cell center">6</div>
          <div class="cell center">7</div>
          <div class="cell center">8</div>
          <div class="cell center">9</div>
        </div>
        <div class="controls center">controls</div>
      </div>
      <div class="chat center">chat</div>
    </div>
  </div>
</body>
body {
  height: 100vh;
  max-height: 100vh;
  margin: 0 auto;
  display: flex;
}

.container {
  flex: 1;
  display: flex;
}

.menu {
  min-width: 100px;
  background-color: red;
}

.game {
  flex: 1 1 auto;
  display: flex;
}

.content {
  flex: 3 0 200px;
  flex-direction: column;
}

.grid {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  aspect-ratio: 1 / 1;
}

.cell {
  font-size: 2em;
  background-color: green;
}

.controls {
  height: 200px;
  background-color: yellow;
}

.chat {
  flex: 1 0 100px;
  background-color: lightblue;
}

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

codepen上的相同代码:

在狭窄的屏幕上看起来像什么(很好):

这就是它在宽屏幕上的外观。它增加了垂直滚动栏,而我想将其高度限制在屏幕高度上,并在左右添加空间。

I have a page based on flexboxes which grows and shrinks proportionally. I want to limit it's maximum height to the screen height so that when it reaches the screen height, it doesn't grow but adds space to the left and right instead.

I tried to add max-height: 100vh and margin: 0 auto to the body but that doesn't help.

<body class="center">
  <div class="container">
    <div class="menu center">menu</div>
    <div class="game">
      <div class="content">
        <div class="grid">
          <div class="cell center">1</div>
          <div class="cell center">2</div>
          <div class="cell center">3</div>
          <div class="cell center">4</div>
          <div class="cell center">5</div>
          <div class="cell center">6</div>
          <div class="cell center">7</div>
          <div class="cell center">8</div>
          <div class="cell center">9</div>
        </div>
        <div class="controls center">controls</div>
      </div>
      <div class="chat center">chat</div>
    </div>
  </div>
</body>
body {
  height: 100vh;
  max-height: 100vh;
  margin: 0 auto;
  display: flex;
}

.container {
  flex: 1;
  display: flex;
}

.menu {
  min-width: 100px;
  background-color: red;
}

.game {
  flex: 1 1 auto;
  display: flex;
}

.content {
  flex: 3 0 200px;
  flex-direction: column;
}

.grid {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  aspect-ratio: 1 / 1;
}

.cell {
  font-size: 2em;
  background-color: green;
}

.controls {
  height: 200px;
  background-color: yellow;
}

.chat {
  flex: 1 0 100px;
  background-color: lightblue;
}

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

Same code at codepen: https://codepen.io/Ihor0k/pen/wvmaaEO

Here is what it looks like on a narrow screen (which is nice):
enter image description here

And here is what it looks on a wide screen. It adds vertical scrollbar whereas I want to limit it's height to the screen height and add spaces to the left and right.
enter image description here

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

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

发布评论

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

评论(1

昨迟人 2025-02-19 05:57:01

解决方案

在这里是我必须修改的 (删除中心从身体上删除content div),

<body>
    <div class="container">
        <div class="menu center">
            menu
        </div>

        <div class="game">
            <div class="grid">
                <div class="cell center">1</div>
                <div class="cell center">2</div>
                <div class="cell center">3</div>
                <div class="cell center">4</div>
                <div class="cell center">5</div>
                <div class="cell center">6</div>
                <div class="cell center">7</div>
                <div class="cell center">8</div>
                <div class="cell center">9</div>
            </div>
            
            <div class="controls center">controls</div>
        </div>

        <div class="chat center">
            chat
        </div>
    </div>
</body>

如CSS,有一些修改:

body {
    margin: 0 auto;
    /* removed height, and flex from body */
}

.container {
    display: flex;
    justify-content: center;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 100%;
    /* not the best solution, but was the quickest */
}

.menu {
    min-width: 100px;
    background-color: red;
}

.game {
    max-width: calc(100vmin - 200px); /* minimum of viewport (height or width minus the minimum width for chat and menu */
    flex-grow: 1;
    display: flex;
    flex-direction: column; /* it's only the middle section with numbers and controls so it's column */
}

.grid {
    display: grid;
    grid-template-rows: repeat(3, 1fr);
    grid-template-columns: repeat(3, 1fr);
    gap: 2px;
    aspect-ratio: 1 / 1;
}

.cell {
    min-width: 10px; /* min width for each cell (you can put whatever you want here) */
    font-size: 2em;
    background-color: green;
}

.controls {
    height: 200px;
    background-color: yellow;
}

.chat {
    max-width: 30%; /* max width for chat, the rest should be taken by the game */
    flex-grow: 1;
    display: flex;
    background-color: lightblue;
}

.center {
    display: flex;
    justify-content: center;
    align-items: center;
}

Here is the solution

I've had to modify a bit the html structure (removed center from the body, removed the content div)

<body>
    <div class="container">
        <div class="menu center">
            menu
        </div>

        <div class="game">
            <div class="grid">
                <div class="cell center">1</div>
                <div class="cell center">2</div>
                <div class="cell center">3</div>
                <div class="cell center">4</div>
                <div class="cell center">5</div>
                <div class="cell center">6</div>
                <div class="cell center">7</div>
                <div class="cell center">8</div>
                <div class="cell center">9</div>
            </div>
            
            <div class="controls center">controls</div>
        </div>

        <div class="chat center">
            chat
        </div>
    </div>
</body>

As for the css, there were some modifications:

body {
    margin: 0 auto;
    /* removed height, and flex from body */
}

.container {
    display: flex;
    justify-content: center;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 100%;
    /* not the best solution, but was the quickest */
}

.menu {
    min-width: 100px;
    background-color: red;
}

.game {
    max-width: calc(100vmin - 200px); /* minimum of viewport (height or width minus the minimum width for chat and menu */
    flex-grow: 1;
    display: flex;
    flex-direction: column; /* it's only the middle section with numbers and controls so it's column */
}

.grid {
    display: grid;
    grid-template-rows: repeat(3, 1fr);
    grid-template-columns: repeat(3, 1fr);
    gap: 2px;
    aspect-ratio: 1 / 1;
}

.cell {
    min-width: 10px; /* min width for each cell (you can put whatever you want here) */
    font-size: 2em;
    background-color: green;
}

.controls {
    height: 200px;
    background-color: yellow;
}

.chat {
    max-width: 30%; /* max width for chat, the rest should be taken by the game */
    flex-grow: 1;
    display: flex;
    background-color: lightblue;
}

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