对于flexbox,axt-height属性被忽略
我有一个基于弹性箱的页面,该页面会成比例地增长和收缩。我想将其最大高度限制在屏幕高度上,以便当它达到屏幕高度时,它不会增长,但会在左右添加空间。
我尝试将最大高调添加: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):
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案
在这里是我必须修改的 (删除
中心
从身体上删除content
div),如CSS,有一些修改:
Here is the solution
I've had to modify a bit the html structure (removed
center
from the body, removed thecontent
div)As for the css, there were some modifications: