【发布时间】:2017-12-17 01:46:37
【问题描述】:
我有简单的联系表格,但我希望它有一些背景知识。但是,简单地输入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;
}
【问题讨论】:
-
顾名思义,
background-color是元素(整个)背景的颜色。 -
您可以将宽度设置为
95%或50%。 -
@James Douglas,我先尝试过。它有点作用,但随后表单不再居中,并且与表单的大小混淆。
-
@JonathonHooks 查看我的回答here。
标签: html css background-color contact-form