You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

容器无法自适应内容高度?多种CSS调整方案无效求助

表单容器无法自适应内容高度的解决方法

问题分析

你的表单容器无法自适应内容高度,核心原因如下:

  • 容器设置了max-height: min-content,强制限制容器最大高度为内容最小高度,直接导致内容溢出
  • 多处使用负边距(如#date的margin-bottom: -10%、.flex-row的margin-top: -5%),使部分内容脱离容器高度计算范围
  • 存在无效CSS语法(如border-radius: px、margin-top: px),干扰样式正常解析
  • 重复定义全局*选择器,引发样式冲突

你尝试过的操作

  • 将overflow从hidden改为relative:overflow无relative取值,属于错误设置,正确可选值为visible/hidden/scroll/auto等
  • 为容器添加position:relative;:该属性不影响容器高度自适应,因此无效果
  • 移除height属性,尝试将height:auto;改为height:relative:height无relative取值,height:auto是正确的自适应设置,但被max-height限制
  • 添加overflow:auto;:虽能显示内容,但会出现滚动条,不符合需求

修复步骤

  1. 修正容器样式:移除max-height: min-content,保留height: auto,让容器自动计算内容高度
  2. 清理无效CSS:删除所有无值属性(如border-radius: px),修正语法错误
  3. 替换负边距:用合理的margin/padding值替代负边距,避免内容溢出容器
  4. 合并全局样式:将重复的*选择器合并,消除样式冲突
  5. 规范空白区域:用padding替代大量<br>标签,提升代码可维护性

修改后的完整代码

CSS

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap');
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  width: 100%;
  background: #b0cac7;
}

.container {
  background-color: #ffffff;
  min-width: 80%;
  overflow: visible;
  position: relative;
  height: auto;
  padding: 3%;
  margin: 5%;
  border-radius: 0;
  box-shadow: 20px 30px 25px rgba(0, 0, 0, 0.15);
}

textarea {
  width: 100%;
  height: 150px;
  padding: 12px 20px;
  box-sizing: border-box;
  background-color: transparent;
  font-size: 16px;
  resize: none;
  border-radius: 0;
  border: 2px solid #707070;
  outline: none;
}

textarea::placeholder {
  font-size: 1rem;
  color: #9c9c9c;
}

.container .form {
  margin-top: 30px;
}

.form .input-box {
  width: 100%;
  margin-top: 10px;
}

.input-box label {
  color: #333;
}

.form .input-box input {
  position: relative;
  height: 50px;
  width: 100%;
  font-size: 1rem;
  color: rgb(39, 34, 34);
}

.form :where(.input-box input, .select-box) {
  position: relative;
  height: 50px;
  width: 100%;
  outline: none;
  font-size: 1rem;
  color: #707070;
  margin-top: 5px;
  border: 2px solid #cfd1d6;
  border-radius: 1px;
  padding: 0 15px;
}

.column {
  margin: 0;
  flex: 50%;
  padding: 10px;
}

#date {
  margin-bottom: 1rem;
}

.other {
  margin-left: 20px;
}

h1 {
  font-size: 30px;
  text-align: center;
  color: #1c093c;
  margin-top: 15px;
}

p {
  position: relative;
  margin: auto;
  width: 100%;
  text-align: center;
  color: #606060;
  font-size: 14px;
  font-weight: 400;
}

form {
  width: 100%;
  position: relative;
  margin: 30px auto 0 auto;
}

.row {
  margin-top: 1px;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-gap: 20px 30px;
}

label i {
  color: #1c093c;
  font-size: 16px;
}

.flex-row {
  margin-top: 1rem;
  display: flex;
  padding: 10px;
}

.wrapper {
  border: 1px solid #707070;
  border-right: 0;
  flex: 1;
}

.column .input-box #name {
  margin-bottom: 20%;
}

canvas#signature-pad {
  background: #fff;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

.jSignature {
  background-color: rgb(151, 23, 23);
}

button#clear {
  height: 100%;
  background: #0e5272;
  border: 1px solid transparent;
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

button#clear span {
  transform: rotate(90deg);
  display: block;
}

textarea,
input {
  width: 100%;
  font-weight: 400;
  padding: 8px 10px;
  border-radius: 0.5%;
  border: 2px solid #cfd1d6;
  margin-top: 5px;
}

textarea:focus {
  outline: none !important;
  border-color: #e2d847;
  box-shadow: 0 0 10px #e4c749;
}

input:focus {
  outline: none !important;
  border-color: #e2d847;
  box-shadow: 0 0 10px #e4c749;
}

h4 {
  margin-top: 1px;
  padding: 10px;
}

button {
  border: none;
  padding: 10px 20px;
  background: linear-gradient(130deg, #6f6df4, #4c46f5);
  color: #ffffff;
  border-radius: 3px;
}

@media screen and (max-width: 500px) {
  .form .column {
    flex-wrap: wrap;
  }
  .upload .up {
    flex-wrap: wrap;
  }
}

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Form</title>
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="style.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/signature_pad/4.1.7/signature_pad.umd.min.js"></script>
</head>
<body>
  <section class="container">
    <h1>Site Report Sheet</h1>
    <form action="#" class="form">
      <div class="row">
        <div class="column">
          <label for="client">Client details</label>
          <textarea id="client" name="client" placeholder="Enter details here.." rows="3"></textarea>
        </div>
      </div>

      <div class="row">
        <div class="column">
          <label for="site">Site Name & Address</label>
          <textarea id="issue" name="issue" placeholder="Enter details here..." rows="3"></textarea>
        </div>
      </div>

      <div class="column">
        <div class="input-box">
          <label>WIS Number</label>
          <input type="text" required />
        </div>

        <div class="input-box" id="date">
          <label>Date</label>
          <input type="date" required />
        </div>
      </div>

      <!-- 中间空白区域 -->
      <div style="padding: 4rem 0;"></div>

      <h4>THE ABOVE IS CERTIFIED CORRECT AND SYSTEM HAS BEEN LEFT IN <u>WORKING ORDER</u></h4> <br>

      <div class="column">
        <div class="input-box">
          <label id="name">Name</label>
          <input type="text" required />
        </div>
      </div>
      <br><br>

      <label>Signature</label> <br><br>
      <div class="flex-row">
        <div class="wrapper">
          <canvas id="signature-pad" width="300" height="100"></canvas>
        </div>
        <div class="clear-btn">
          <button id="clear"><span> Clear </span></button>
        </div>
      </div>

      <div class="column">
        <div class="input-box">
          <label>On Behalf Of</label>
          <input type="text" required />
        </div>
        <div class="input-box">
          <label>Position</label>
          <input type="text" required />
        </div>

        <div class="input-box" id="date">
          <label>Date</label>
          <input type="date" required />
        </div>
      </div>
    </form>
  </section>
</body>
</html>

内容的提问来源于stack exchange,提问作者Axelle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 03:37:05