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

为何input/textarea无法填满.hero宽度?求自适应解决方案

问题解决:让输入框自动适配.hero宽度并消除两侧空白

问题原因分析

  1. 表单样式未生效:你CSS里定义了.form类,但HTML中的<form>标签没加class="form",导致width:200%的设置完全无效,表单默认仅由内容宽度决定,加上.hero的justify-content:center,自然会出现两侧空白。
  2. 盒模型导致宽度溢出隐患:默认box-sizing:content-box下,input/textarea的width:100%会叠加padding值,实际宽度会超出父容器(不过这里主要问题是表单本身太窄)。
  3. 按钮宽度设置错误:button的width:111%会让按钮超出输入框宽度,破坏布局一致性。

解决方案代码修改

HTML部分(给form添加class)

<section class="contact">
  <h9>Contact</h9>
  <div class="hero">
    <!-- 给form加上form类,让CSS样式生效 -->
    <form class="form" action="https://formsubmit.co/galus.films@gmail.com" method="POST">
      <div class="input-group">
        <input type="text" id="name" required>
        <label for="name"><i class="fa-solid fa-user"></i> Full Name</label>
      </div>
      <div class="input-group">
        <input type="text" id="email" required>
        <label for="email"><i class="fa-solid fa-envelope"></i> Email Address</label>
      </div>
      <div class="input-group">
        <input type="text" id="subject" required>
        <label for="subject"><i class="fa-solid fa-circle"></i> Subject / Order No.</label>
      </div>
      <div class="input-group">
        <textarea style="resize: none;" id="message" rows="8" required></textarea>
        <label for="message"> Message </label><i class="fa-solid fa-message-text"></i>
      </div>
      <input type="hidden" name="_next" value="https://galusfilms.com/contact">
      <button type="submit">SUBMIT</button>
    </form>
  </div>
</section>

CSS部分(核心修改)

/* 全局设置盒模型,避免padding导致宽度溢出,消除默认边距干扰 */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

h9 {
  font-size: 36px;
  font-weight: 600;
  color: white;
  text-align: center;
}

.contact {
  min-height: 130vh;
  width: 80%;
  margin: auto;
  padding-top: 30px;
  text-align: center;
}

h10 {
  font-size: 36px;
  font-weight: 600;
  color: white;
}

.contact-text {
  padding-bottom: 30px;
}

.hero {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 30px 20px 20px; /* 添加上下左右内边距,避免表单贴容器边缘 */
  background: gainsboro;
  width: 50%;
  left: 25%;
  position: absolute;
  height: 90vh;
  border-radius: 10px;
  box-shadow: 0 0 15px black;
}

/* 修改form宽度为100%,占满.hero的可用宽度 */
.form {
  width: 100%;
}

.input-group {
  margin-bottom: 30px;
  position: relative;
  width: 100%; /* 确保.input-group占满form宽度 */
}

input,
textarea {
  padding: 10px;
  outline: 0;
  border: 1px solid black;
  color: black;
  background: transparent;
  font-size: 15px;
  width: 100%;
}

label {
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 10px;
  color: black;
  cursor: text;
  transition: 0.2s ease;
}

/* 修改按钮宽度为100%,和输入框对齐 */
button {
  padding: 10px 0;
  color: black;
  outline: none;
  background: transparent;
  border: 1px solid black;
  width: 100%;
  cursor: pointer;
}

input:focus~label,
input:valid~label,
textarea:focus~label,
textarea:valid~label {
  top: -35px;
  font-size: 14px;
}

关键修改点说明

  1. 全局盒模型设置:box-sizing:border-box让元素的width包含padding和border,彻底解决宽度溢出问题,同时消除默认边距的干扰。
  2. 表单宽度适配:给<form>添加form类,设置width:100%让表单占满.hero的可用宽度,配合.hero的左右padding,既填满容器又不会贴边。
  3. 按钮宽度修正:把按钮宽度改成100%,和输入框保持一致,避免错位。
  4. .input-group宽度:明确设置width:100%,确保输入框父容器占满表单宽度。

修改后,输入框会自动适配.hero的宽度,同时保持响应式效果,两侧空白也会消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 14:10:28