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