更改表单布局,使字段水平排列 CSS
Change the layout a form so that the fields are laid out horizontally CSS
我开始学习 CSS,他们要求我更改表单的布局,以便字段水平排列,我必须像这样使用 display: flex;
属性:
但这就是我得到的:
h1 {
font-size: 20px;
letter-spacing: 2px;
margin-bottom: 10px;
margin-top: 10px;
}
body {
background-color: #e0e0e0;
}
section {
padding: 10px;
background-color: white;
}
#prices {
margin: 0;
}
table {
border-collapse: collapse;
}
td,
th {
border: 1px solid black;
padding: 10px;
}
th {
background-color: #2493df;
color: white;
}
ul {
list-style-type: square;
list-style-position: inside;
padding-left: 30px;
}
strong {
color: #2493df;
}
label {
font-size: 16;
font-weight: bold;
}
#buy-form {
background-color: #687373;
margin-top: 10px;
color: #fcfcfc;
display: flex;
}
.form-section {
flex: 1;
}
input[type="number"] {
padding: 2px;
text-align: center;
width: 50px;
}
button {
background-color: #2493df;
font-weight: bold;
font-size: 16px;
color: white;
border: none;
height: 40px;
width: 100%;
}
footer {
font-size: 14px;
color: #595959;
text-align: center;
margin-top: 10px;
}
<section>
<form id="buy-form">
<div class="form-section">
<label for "Adult_t">Adults:</label><br>
<input type="number" id="Adult_t">
<label for "Children_t">Childen:</label><br>
<input type="number" id="Children_t">
<button>Buy</button>
</div>
</form>
</section>
将每个 label
和 input
嵌套在各自的 div's
中。然后你可以将 flex
设置为 form-section
并向 #buy-form
添加一些填充。另外,我从按钮中删除了宽度和高度并改用填充。
h1 {
font-size: 20px;
letter-spacing: 2px;
margin-bottom: 10px;
margin-top: 10px;
}
body {
background-color: #e0e0e0;
}
section {
padding: 10px;
background-color: white;
}
#prices {
margin: 0;
}
table {
border-collapse: collapse;
}
td,
th {
border: 1px solid black;
padding: 10px;
}
th {
background-color: #2493df;
color: white;
}
ul {
list-style-type: square;
list-style-position: inside;
padding-left: 30px;
}
strong {
color: #2493df;
}
label {
font-size: 16;
font-weight: bold;
}
#buy-form {
background-color: #687373;
margin-top: 10px;
color: #fcfcfc;
padding: 10px;
}
.form-section {
display: flex;
justify-content: space-evenly;
}
input[type="number"] {
padding: 2px;
text-align: center;
width: 50px;
}
button {
background-color: #2493df;
font-weight: bold;
font-size: 16px;
color: white;
border: none;
padding: 20px;
}
footer {
font-size: 14px;
color: #595959;
text-align: center;
margin-top: 10px;
}
<section>
<form id="buy-form">
<div class="form-section">
<div>
<label for "Adult_t">Adults:</label><br>
<input type="number" id="Adult_t">
</div>
<div>
<label for "Children_t">Childen:</label><br>
<input type="number" id="Children_t">
</div>
<button>Buy</button>
</div>
</form>
</section>
我开始学习 CSS,他们要求我更改表单的布局,以便字段水平排列,我必须像这样使用 display: flex;
属性:
但这就是我得到的:
h1 {
font-size: 20px;
letter-spacing: 2px;
margin-bottom: 10px;
margin-top: 10px;
}
body {
background-color: #e0e0e0;
}
section {
padding: 10px;
background-color: white;
}
#prices {
margin: 0;
}
table {
border-collapse: collapse;
}
td,
th {
border: 1px solid black;
padding: 10px;
}
th {
background-color: #2493df;
color: white;
}
ul {
list-style-type: square;
list-style-position: inside;
padding-left: 30px;
}
strong {
color: #2493df;
}
label {
font-size: 16;
font-weight: bold;
}
#buy-form {
background-color: #687373;
margin-top: 10px;
color: #fcfcfc;
display: flex;
}
.form-section {
flex: 1;
}
input[type="number"] {
padding: 2px;
text-align: center;
width: 50px;
}
button {
background-color: #2493df;
font-weight: bold;
font-size: 16px;
color: white;
border: none;
height: 40px;
width: 100%;
}
footer {
font-size: 14px;
color: #595959;
text-align: center;
margin-top: 10px;
}
<section>
<form id="buy-form">
<div class="form-section">
<label for "Adult_t">Adults:</label><br>
<input type="number" id="Adult_t">
<label for "Children_t">Childen:</label><br>
<input type="number" id="Children_t">
<button>Buy</button>
</div>
</form>
</section>
将每个 label
和 input
嵌套在各自的 div's
中。然后你可以将 flex
设置为 form-section
并向 #buy-form
添加一些填充。另外,我从按钮中删除了宽度和高度并改用填充。
h1 {
font-size: 20px;
letter-spacing: 2px;
margin-bottom: 10px;
margin-top: 10px;
}
body {
background-color: #e0e0e0;
}
section {
padding: 10px;
background-color: white;
}
#prices {
margin: 0;
}
table {
border-collapse: collapse;
}
td,
th {
border: 1px solid black;
padding: 10px;
}
th {
background-color: #2493df;
color: white;
}
ul {
list-style-type: square;
list-style-position: inside;
padding-left: 30px;
}
strong {
color: #2493df;
}
label {
font-size: 16;
font-weight: bold;
}
#buy-form {
background-color: #687373;
margin-top: 10px;
color: #fcfcfc;
padding: 10px;
}
.form-section {
display: flex;
justify-content: space-evenly;
}
input[type="number"] {
padding: 2px;
text-align: center;
width: 50px;
}
button {
background-color: #2493df;
font-weight: bold;
font-size: 16px;
color: white;
border: none;
padding: 20px;
}
footer {
font-size: 14px;
color: #595959;
text-align: center;
margin-top: 10px;
}
<section>
<form id="buy-form">
<div class="form-section">
<div>
<label for "Adult_t">Adults:</label><br>
<input type="number" id="Adult_t">
</div>
<div>
<label for "Children_t">Childen:</label><br>
<input type="number" id="Children_t">
</div>
<button>Buy</button>
</div>
</form>
</section>