html - 在 CSS 中使 div 背景小于 100% 宽度?

标签 html css background-color contact-form

我有一个简单的联系表,但我希望它有一些背景知识。但是,简单地放置 background-color 并不像我希望的那样工作。背景颜色是整个页面的宽度,但我只希望它覆盖联系表格周围的一小块区域。我已经按照我希望它看起来像什么的方式画了一张图片。 Example of what is required HTML:

<div class="contact">
    <form action="/action_page.php">
        <input type="text" id="fname" name="firstname" placeholder="Your name"><br>
        <input type="text" id="lname" name="lastname" placeholder="Your last name"><br>
        <textarea rows="10" placeholder="Type Message"></textarea><br>
        <input type="submit">
    </form>
</div>

CSS

    .contact {
    text-align: center;
    background-color: #E0E0E0;
}

input[type=text] {
    width: 20%; 
    padding: 6[enter image description here][1]px; 
    border: 1px solid #ccc; 
    border-radius: 4px; 
    box-sizing: border-box; 
    margin-top: 6px; 
    margin-bottom: 16px; 
}

textarea {
    width: 45%; 
    padding: 6px; 
    border: 1px solid #ccc; 
    border-radius: 4px; 
    box-sizing: border-box; 
    margin-top: 6px; 
    margin-bottom: 16px; 
}

input[type=submit] {
    background-color: #4CAF50;
    color: white;
    padding: 12px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

input[type=submit]:hover {
    background-color: #45a049;
}

最佳答案

将所有内容放入容器并添加一些边距怎么样?

HTML

<div class="container">
  <div class="contact">
      <form action="/action_page.php">
          ...
      </form>
  </div>
</div>

CSS

.contact {
    text-align: center;
    background-color: #E0E0E0;
    margin: 0px 50px 20px 50px;
}

然后你可以使用 container css 使其适应上下文。

关于html - 在 CSS 中使 div 背景小于 100% 宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45060939/

相关文章:

php - 需要使用 jQuery 显示包含数据库信息的数组元素

jquery - 根据主题更改 css 并删除以前的主题 css

php - 表格出现在页脚下方,无法确定在 functions.php 中放置 </table> 的位置

html - <body> 具有一定的宽度和颜色

xaml - 将 StackPanel 背景设置为 WP7 选定的强调(主题)颜色

html - 如何在html中设置session和cookie?

javascript - Chrome(和其他)将以什么顺序加载 <img>?如何改变这个?

javascript - jQuery 切换困惑

css - Gantry 5 网格系统与 Bootstrap 4 中的相同

css - css中的替代彩色表格行