如何在一行中将单选按钮与标签,图例和字段集对齐

时间:2018-12-31 07:05:38

标签: html css html5 css3 radio-button

因此,我正在为我的项目创建一个驱动程序注册表格,我需要在单行中对齐这些单选按钮的帮助。我尝试使用Clear:两者;方法以及下面提到的方法:

form label[for="yes"],
form label[for="yes"] + input,
form label[for="no"],
form label[for="no"] + input {
clear: none;
width: auto;
}

但是什么都没有用:(。我也尝试使用段落标记,但是那也没有用。

以下是代码段:

body{
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  background: #34495e;
}
.box{
  width: 300px;
  padding: 40px;
  position: absolute;
  top: 80%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #191919;
  text-align: center;
}
.box h1{
  color: white;
  text-transform: uppercase;
  font-weight: 500;
}

select > option{
  background: #191919;
  color: white;
}

.box textarea{
  height: 75px;
}

.box label[for="Male"],
.box label[for="Male"] + input,
.box label[for="Female"],
.box label[for="Female"] + input,
.box label[for="Other"],
.box label[for="Other"] + input,
.box input[type = "radio"] {
  display: inline;
  clear: none;
  width: auto;
}

.box input[type = "text"],.box input[type = "password"], .box select, .box input[type = "tel"], .box textarea, .box fieldset,
.box input[type = "radio"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #3498db;
  padding: 14px 10px;
  width: 200px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
}
.box input[type = "text"]:focus,.box input[type = "password"]:focus,.box select:focus, .box input[type = "tel"]:focus,
 .box textarea:focus{
  width: 280px;
  border-color: #2ecc71;
}
.box input[type = "submit"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #2ecc71;
  padding: 14px 40px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
  cursor: pointer;
}
.box input[type = "submit"]:hover{
  background: #2ecc71;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>My Example</title>
<link rel="stylesheet" href="Driver-css.css">

</head>
<body>  
  
<form class="box" method="POST"  action="test.html">

  <h1>Driver Registration</h1>

  <input type="text" name="" placeholder="First Name" required>

  <input type="text" name="" placeholder="Middle Name" required>

  <input type="text" name="" placeholder="Last Name" required>

  <input type="tel" name="phone_number" placeholder="Mobile Number" pattern="[0-9]{10}" required>

  <input type="tel" name="phone_number" placeholder="Emergency Number" pattern="[0-9]{10}" required>

  <textarea name="comments" placeholder="Address" maxlength="500" required></textarea>

  <input type="tel" name="" placeholder="License Number" pattern="[A-Z]{2}[0-9]{13}" required>

  <fieldset>
        <legend>Gender</legend>
         <label for="Male"><input type="radio" name="Gender" required value="Male"> Male</label>
         <label for="Female"><input type="radio" name="Gender" required value="Female"> Female</label>
        <label for="Other"><input type="radio" name="Gender" required value="Other"> Other </label>
        </fieldset>


  <select required id="pickup_place" name="pickup_place">
    <option value="" selected="selected">Select One</option>
    <option value="Automobile" >Automobile</option>
    <option value="Civill" >Civil</option>
    <option value="Computer" >Computer</option>
    <option value="Electrical" >Electrical</option>
    <option value="Mechanical" >Mechanical</option>
    </select>

  <input type="submit" name="" value="Submit">

</form>
</body>
</html>

我们将不胜感激!

2 个答案:

答案 0 :(得分:1)

添加以下样式

.box input[type="radio"] {
  display: inline;
  width: auto;
}

fieldset label {
  margin-right: 5px;
}

body {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  background: #34495e;
}

.box {
  width: 300px;
  padding: 40px;
  position: absolute;
  top: 80%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #191919;
  text-align: center;
}

.box h1 {
  color: white;
  text-transform: uppercase;
  font-weight: 500;
}

select>option {
  background: #191919;
  color: white;
}

.box textarea {
  height: 75px;
}

.box label[for="Male"],
.box label[for="Male"]+input,
.box label[for="Female"],
.box label[for="Female"]+input,
.box label[for="Other"],
.box label[for="Other"]+input,
.box input[type="radio"] {
  display: inline;
  clear: none;
  width: auto;
}

.box input[type="text"],
.box input[type="password"],
.box select,
.box input[type="tel"],
.box textarea,
.box fieldset,
.box input[type="radio"] {
  border: 0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #3498db;
  padding: 14px 10px;
  width: 200px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
}

.box input[type="text"]:focus,
.box input[type="password"]:focus,
.box select:focus,
.box input[type="tel"]:focus,
.box textarea:focus {
  width: 280px;
  border-color: #2ecc71;
}

.box input[type="submit"] {
  border: 0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #2ecc71;
  padding: 14px 40px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
  cursor: pointer;
}

.box input[type="submit"]:hover {
  background: #2ecc71;
}

.box input[type="radio"] {
  display: inline;
  width: auto;
}

fieldset label {
  margin-right: 5px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>My Example</title>
  <link rel="stylesheet" href="Driver-css.css">

</head>

<body>

  <form class="box" method="POST" action="test.html">

    <h1>Driver Registration</h1>

    <input type="text" name="" placeholder="First Name" required>

    <input type="text" name="" placeholder="Middle Name" required>

    <input type="text" name="" placeholder="Last Name" required>

    <input type="tel" name="phone_number" placeholder="Mobile Number" pattern="[0-9]{10}" required>

    <input type="tel" name="phone_number" placeholder="Emergency Number" pattern="[0-9]{10}" required>

    <textarea name="comments" placeholder="Address" maxlength="500" required></textarea>

    <input type="tel" name="" placeholder="License Number" pattern="[A-Z]{2}[0-9]{13}" required>

    <fieldset>
      <legend>Gender</legend>
      <label for="Male"><input type="radio" name="Gender" required value="Male"> Male</label>
      <label for="Female"><input type="radio" name="Gender" required value="Female"> Female</label>
      <label for="Other"><input type="radio" name="Gender" required value="Other"> Other </label>
    </fieldset>


    <select required id="pickup_place" name="pickup_place">
      <option value="" selected="selected">Select One</option>
      <option value="Automobile">Automobile</option>
      <option value="Civill">Civil</option>
      <option value="Computer">Computer</option>
      <option value="Electrical">Electrical</option>
      <option value="Mechanical">Mechanical</option>
    </select>

    <input type="submit" name="" value="Submit">

  </form>
</body>

</html>

答案 1 :(得分:0)

我想您可以尝试为Legend标签添加display:flex属性(如果您也希望将标签排成一行)。看起来像这样:

legend {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
}

当然,您可以分配一个类而不是设置标签的样式。

有关弹性框的更多信息,请参见:https://css-tricks.com/snippets/css/a-guide-to-flexbox/