无法修改container的max-height,容器未显示问题求助
问题描述

我无法修改我的container的max-height,认为这个容器完全没有显示出来,但找不到原因。
代码示例
CSS代码
*{ margin: 0; padding: 0; box-sizing: border-box; font-family: 'Sora', sans-serif; } body{ background-color: lightblue; display: flex; justify-content: center; align-items: center; min-height: 100vh; } .container{ position: relative; width: 80%; max-width: 1000px; max-height: 100px; background: white(0, 0, 0); display: grid; grid-template-columns: 1fr 2fr; box-shadow: 0 35px 55px rgba(0, 0, 0, 0.103); border: 1px solid red; } .container .left_side{ position: relative; background-color: rgb(87, 120, 158); padding: 40px; } .container .right_side{ position: relative; background-color: white; padding: 40px; }
HTML代码
<!DOCTYPE html> <html> <head> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Yasef Ahnaf CV</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <div class="left_side"></div> <div class="right_side"></div> </div> </body> </html>
问题分析与解决方法
- 背景属性错误:
.container里的background: white(0, 0, 0);是无效写法,这会导致容器背景不显示,改成background: white;就能看到白色背景。 - max-height的作用逻辑:
max-height是容器的最大高度限制,不是固定高度。当前容器的高度由子元素的padding:40px撑开,总高度为80px(上下各40px),小于你设置的max-height:100px,所以这个限制没生效。如果需要容器有固定高度,改用height属性;如果要让容器高度不低于某个值,添加min-height。 - 容器显示问题:其实容器是显示的(红色边框能证明),只是因为背景错误+高度较低,视觉上不明显。修改背景属性后,再调整高度设置就能正常显示。
调整后的.container示例样式:
.container{ position: relative; width: 80%; max-width: 1000px; min-height: 400px; /* 最小高度,保证容器有足够显示空间 */ max-height: 600px; /* 最大高度限制 */ background: white; display: grid; grid-template-columns: 1fr 2fr; box-shadow: 0 35px 55px rgba(0, 0, 0, 0.103); border: 1px solid red; }
内容的提问来源于stack exchange,提问作者yasef05
相关产品推荐
相关产品推荐

