我的复选框或单选按钮没有对齐?

3

我一直在制作一个简单的调查页面,以了解更多关于CSS和HTML的知识。但是我遇到了这样的问题,我的单选按钮值和复选框值不能正确对齐,特别是在“你是如何支付旅行费用?”和“你最喜欢的颜色是什么?”这两个部分。

它们应该像这样对齐:

你是如何支付旅行费用?

  • 现金
  • 支票
  • 其他

body {
  height: 920px;
  width: 1300px;
  margin: 0 0 0 0;
  font-family: 'Roboto';
}

#subtag {
  width: 80%;
  text-align: center;
  margin-left: 75px;
}

.container {
  background-image: url("https://images.unsplash.com/photo-1491800943052-1059ce1e1012?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1267&q=80");
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  height: 920px;
  overflow: auto;
}

.container::before {
  content: " ";
  position: absolute;
  display: block;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 920px;
  background: rgba(0, 0, 0, 0.6);
}

.box {
  margin-left: auto;
  margin-right: auto;
  margin-top: 25px;
  width: 700px;
  height: 890px;
  border: 3px solid black;
  text-align: center;
  background-color: #f2f2f2;
  opacity: 0.6;
}

.textbox {
  width: 80%;
  background-color: #80bfff;
  padding: 8px 8px;
  margin-top: 10px;
  margin-bottom: 10px;
}

.money {
  display: inline;
}

#submit {
  font-size: 14px;
  color: #d6d4e0;
  width: 270px;
  height: 40px;
  background-color: #6b5b95;
  opacity: 1.0;
}

#submit:hover {
  cursor: pointer;
}
<div class="container">
  <div class="box">

    <h3> Villa's Traveling Survey </h3>
    <p id="subtag">
      If there is anything that we can do better please let us know by filling out the survey below </p>

    <form>
      <label for="fname"> Name: </label>
      <br>
      <input type="text" name="fname" placeholder="enter name" class="textbox">
      <br>
      <label for="email"> Email: </label>
      <br>
      <input type="text" name="email" placeholder="enter email" class="textbox">
      <br>
      <label for="age"> Age: </label>
      <br>
      <input type="text" name="age" placeholder="enter age" class="textbox">
      <p> Travel Destination: </p>
      <select name="country">
        <option disabled selected value="Select Your Travel Destination"> Select Your Travel Destination </option>
        <option value="Australia"> Australia</option>
        <option value="New Zealeand"> New Zealand </option>
        <option value="Thailand"> Thailand </option>
      </select>

      <p> How did you pay for your trip? </p>

      <label>
        <input type="radio" name= "trip" value = "check" class= "money">
        check<br>
    </label>

      <label>
        <input type="radio" name= "trip" value = "cash" class= "money">
        cash<br>
      </label>

      <label>
        <input type="radio" name= "trip" value = "other" class= "money">
        other
      </label>

      <p> How did you get to your travel destination? </p>
      <br>
      <input type="checkbox" placeholder="plane" value="plane" class="checkbox">Plane
      <input type="checkbox" placeholder="boat" value="boat" class="checkbox">Boat
      <input type="checkbox" placeholder="train" value="train" class="checkbox">Train
      <input type="checkbox" placeholder="other" value="other" class="checkbox">Other

      <p> What is your favorite color? </p>
      <label><input
         name="prefer"
         value="front-end-projects"
         type="checkbox"
         class="input-checkbox"
       />Front-end Projects</label
     >
     <br/>
     <label
      ><input
        name="prefer"
        value="front-end-projects"
        type="checkbox"
        class="input-checkbox"
      />back-end Projects</label
    >
    <br/>
    <label
     ><input
       name="prefer"
       value="front-end-projects"
       type="checkbox"
       class="input-checkbox"
     />love Projects</label
   >


      <p> Please enter any additional comments or suggestions </p>
      <textarea rows="5" cols="50" maxlength="100" placeholder="Enter comments here"></textarea>
      <br>
      <input type="submit" value="submit" id = "submit">
      </form>

    </div>
</div>

我尝试过将国家类垂直对齐居中,但是这并不奏效。我也尝试过改变显示方式为inline-block,但是这会把所有的值都推到一行上。
我是否有任何导致这个问题的原因呢?通常来说,一个简单的br标签就可以对齐单选框的值。但是这些值却没有正确地对齐。
如果有任何建议或者评论,将会很有帮助!
以下是jsfiddle链接:https://jsfiddle.net/fun_code11/9wvj130b/
4个回答

2
由于您的 .box 类设置了 text-align 属性为 center,因此您的标签和复选框也遵循了这个规则。只是因为您的标签内容长度不同,因此较短的单词距离中心更近,所以看起来有些奇怪。
虽然有很多方法可以对其进行样式设置,但我不知道您整体需要什么,但解决方案是将它们包装在一个容器中,该容器将由 .box 居中并重置对齐方式。请记住,在单词之前的任何空格也会被包括在内:
#money-container {
  text-align: left;
  width: 100px;
  margin: auto;
}

<div id="money-container">
  <label>
    <input type="radio" name= "trip" value = "check" class= "money">check<br>
  </label>

  <label>
    <input type="radio" name= "trip" value = "cash" class= "money">cash<br>
  </label>

  <label>
   <input type="radio" name= "trip" value = "other" class= "money">other
  </label>
</div>

body {
  height: 920px;
  width: 1300px;
  margin: 0 0 0 0;
  font-family: 'Roboto';
}

#subtag {
  width: 80%;
  text-align: center;
  margin-left: 75px;
}

.container {
  background-image: url("https://images.unsplash.com/photo-1491800943052-1059ce1e1012?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1267&q=80");
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  height: 920px;
  overflow: auto;
}

.container::before {
  content: " ";
  position: absolute;
  display: block;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 920px;
  background: rgba(0, 0, 0, 0.6);
}

.box {
  margin-left: auto;
  margin-right: auto;
  margin-top: 25px;
  width: 700px;
  height: 890px;
  border: 3px solid black;
  text-align: center;
  background-color: #f2f2f2;
  opacity: 0.6;
}

.textbox {
  width: 80%;
  background-color: #80bfff;
  padding: 8px 8px;
  margin-top: 10px;
  margin-bottom: 10px;
}

#submit {
  font-size: 14px;
  color: #d6d4e0;
  width: 270px;
  height: 40px;
  background-color: #6b5b95;
  opacity: 1.0;
}

#submit:hover {
  cursor: pointer;
}

#money-container {
  text-align: left;
  width: 100px;
  margin: auto;
}
<div class="container">
  <div class="box">

    <h3> Villa's Traveling Survey </h3>
    <p id="subtag">
      If there is anything that we can do better please let us know by filling out the survey below </p>

    <form>
      <label for="fname"> Name: </label>
      <br>
      <input type="text" name="fname" placeholder="enter name" class="textbox">
      <br>
      <label for="email"> Email: </label>
      <br>
      <input type="text" name="email" placeholder="enter email" class="textbox">
      <br>
      <label for="age"> Age: </label>
      <br>
      <input type="text" name="age" placeholder="enter age" class="textbox">
      <p> Travel Destination: </p>
      <select name="country">
        <option disabled selected value="Select Your Travel Destination"> Select Your Travel Destination </option>
        <option value="Australia"> Australia</option>
        <option value="New Zealeand"> New Zealand </option>
        <option value="Thailand"> Thailand </option>
      </select>


      <p> How did you pay for your trip? </p>

      <div id="money-container">
        <label>
        <input type="radio" name= "trip" value = "check" class= "money">check<br></label>

        <label>
        <input type="radio" name= "trip" value = "cash" class= "money">cash<br></label>

        <label>
        <input type="radio" name= "trip" value = "other" class= "money">other</label>
      </div>

      <p> How did you get to your travel destination? </p>
      <br>
      <input type="checkbox" placeholder="plane" value="plane" class="checkbox">Plane
      <input type="checkbox" placeholder="boat" value="boat" class="checkbox">Boat
      <input type="checkbox" placeholder="train" value="train" class="checkbox">Train
      <input type="checkbox" placeholder="other" value="other" class="checkbox">Other

      <p> What is your favorite color? </p>
      <label><input
         name="prefer"
         value="front-end-projects"
         type="checkbox"
         class="input-checkbox"
       />Front-end Projects</label
     >
     <br/>
     <label
      ><input
        name="prefer"
        value="front-end-projects"
        type="checkbox"
        class="input-checkbox"
      />back-end Projects</label
    >
    <br/>
    <label
     ><input
       name="prefer"
       value="front-end-projects"
       type="checkbox"
       class="input-checkbox"
     />love Projects</label
   >


      <p> Please enter any additional comments or suggestions </p>
      <textarea rows="5" cols="50" maxlength="100" placeholder="Enter comments here"></textarea>
      <br>
      <input type="submit" value="submit" id = "submit">
      </form>

    </div>
</div>


0

body {
  height: 920px;
  width: 1300px;
  margin: 0 0 0 0;
  font-family: 'Roboto';
}

#subtag {
  width: 80%;
  text-align: center;
  margin-left: 75px;
}

.container {
  background-image: url("https://images.unsplash.com/photo-1491800943052-1059ce1e1012?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1267&q=80");
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  height: 920px;
  overflow: auto;
}

.container::before {
  content: " ";
  position: absolute;
  display: block;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 920px;
  background: rgba(0, 0, 0, 0.6);
}

.box {
  margin-left: auto;
  margin-right: auto;
  margin-top: 25px;
  width: 700px;
  height: 890px;
  border: 3px solid black;
  text-align: center;
  background-color: #f2f2f2;
  opacity: 0.6;
}

.textbox {
  width: 80%;
  background-color: #80bfff;
  padding: 8px 8px;
  margin-top: 10px;
  margin-bottom: 10px;
}

#submit {
  font-size: 14px;
  color: #d6d4e0;
  width: 270px;
  height: 40px;
  background-color: #6b5b95;
  opacity: 1.0;
}

#submit:hover {
  cursor: pointer;
}

#money-container,#check-container {
    display: flex;
    text-align: left;
    flex-direction: column;
}
#money-container label {
    width: 100px;
    margin: auto;
}
#check-container label
{ 
  width: 150px;
  margin:auto;
}
<div class="container">
  <div class="box">

    <h3> Villa's Traveling Survey </h3>
    <p id="subtag">
      If there is anything that we can do better please let us know by filling out the survey below </p>

    <form>
      <label for="fname"> Name: </label>
      <br>
      <input type="text" name="fname" placeholder="enter name" class="textbox">
      <br>
      <label for="email"> Email: </label>
      <br>
      <input type="text" name="email" placeholder="enter email" class="textbox">
      <br>
      <label for="age"> Age: </label>
      <br>
      <input type="text" name="age" placeholder="enter age" class="textbox">
      <p> Travel Destination: </p>
      <select name="country">
        <option disabled selected value="Select Your Travel Destination"> Select Your Travel Destination </option>
        <option value="Australia"> Australia</option>
        <option value="New Zealeand"> New Zealand </option>
        <option value="Thailand"> Thailand </option>
      </select>


      <p> How did you pay for your trip? </p>

      <div id="money-container">
        <label>
        <input type="radio" name= "trip" value = "check" class= "money">check<br></label>

        <label>
        <input type="radio" name= "trip" value = "cash" class= "money">cash<br></label>

        <label>
        <input type="radio" name= "trip" value = "other" class= "money">other</label>
      </div>

      <p> How did you get to your travel destination? </p>
      <br>
      <input type="checkbox" placeholder="plane" value="plane" class="checkbox">Plane
      <input type="checkbox" placeholder="boat" value="boat" class="checkbox">Boat
      <input type="checkbox" placeholder="train" value="train" class="checkbox">Train
      <input type="checkbox" placeholder="other" value="other" class="checkbox">Other

      <p> What is your favorite color? </p>
      <div id="check-container">
      <label><input
         name="prefer"
         value="front-end-projects"
         type="checkbox"
         class="input-checkbox"
       />Front-end Projects</label
     >
     <br/>
     <label
      ><input
        name="prefer"
        value="front-end-projects"
        type="checkbox"
        class="input-checkbox"
      />back-end Projects</label
    >
    <br/>
    <label
     ><input
       name="prefer"
       value="front-end-projects"
       type="checkbox"
       class="input-checkbox"
     />love Projects</label
   >
</div>

      <p> Please enter any additional comments or suggestions </p>
      <textarea rows="5" cols="50" maxlength="100" placeholder="Enter comments here"></textarea>
      <br>
      <input type="submit" value="submit" id = "submit">
      </form>

    </div>
</div>


0

添加一个父级 div 并应用以下 CSS:

.chkcon{
    display:inline-block;
    text-align: center;
}

body{
    text-align:center;
}
.chkcon{
    background:red;
    display: inline-block;
    text-align: left;
}
<body>
<div class="chkcon">
<label>
    <input type="radio" name= "trip" value = "check" class= "money">
    check<br>
</label>

  <label>
    <input type="radio" name= "trip" value = "cash" class= "money">
    cash<br>
  </label>

  <label>
    <input type="radio" name= "trip" value = "other" class= "money">
    other
  </label>
</div>
<p> What is your favorite color? </p>
<div class="chkcon">
      <label><input
         name="prefer"
         value="front-end-projects"
         type="checkbox"
         class="input-checkbox"
       />Front-end Projects</label
     >
     <br/>
     <label
      ><input
        name="prefer"
        value="front-end-projects"
        type="checkbox"
        class="input-checkbox"
      />back-end Projects</label
    >
    <br/>
    <label
     ><input
       name="prefer"
       value="front-end-projects"
       type="checkbox"
       class="input-checkbox"
     />love Projects</label
   ></div>
</body>

body {
  height: 920px;
  width: 1300px;
  margin: 0 0 0 0;
  font-family: 'Roboto';
}
.chkcon{
    display: inline-block;
    text-align: left;
}
#subtag {
  width: 80%;
  text-align: center;
  margin-left: 75px;
}

.container {
  background-image: url("https://images.unsplash.com/photo-1491800943052-1059ce1e1012?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1267&q=80");
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  height: 920px;
  overflow: auto;
}

.container::before {
  content: " ";
  position: absolute;
  display: block;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 920px;
  background: rgba(0, 0, 0, 0.6);
}

.box {
  margin-left: auto;
  margin-right: auto;
  margin-top: 25px;
  width: 700px;
  height: 890px;
  border: 3px solid black;
  text-align: center;
  background-color: #f2f2f2;
  opacity: 0.6;
}

.textbox {
  width: 80%;
  background-color: #80bfff;
  padding: 8px 8px;
  margin-top: 10px;
  margin-bottom: 10px;
}

.money {
  display: inline;
}

#submit {
  font-size: 14px;
  color: #d6d4e0;
  width: 270px;
  height: 40px;
  background-color: #6b5b95;
  opacity: 1.0;
}

#submit:hover {
  cursor: pointer;
}
<div class="container">
  <div class="box">

    <h3> Villa's Traveling Survey </h3>
    <p id="subtag">
      If there is anything that we can do better please let us know by filling out the survey below </p>

    <form>
      <label for="fname"> Name: </label>
      <br>
      <input type="text" name="fname" placeholder="enter name" class="textbox">
      <br>
      <label for="email"> Email: </label>
      <br>
      <input type="text" name="email" placeholder="enter email" class="textbox">
      <br>
      <label for="age"> Age: </label>
      <br>
      <input type="text" name="age" placeholder="enter age" class="textbox">
      <p> Travel Destination: </p>
      <select name="country">
        <option disabled selected value="Select Your Travel Destination"> Select Your Travel Destination </option>
        <option value="Australia"> Australia</option>
        <option value="New Zealeand"> New Zealand </option>
        <option value="Thailand"> Thailand </option>
      </select>

      <p> How did you pay for your trip? </p>
 <div class="chkcon">
      <label>
        <input type="radio" name= "trip" value = "check" class= "money">
        check<br>
    </label>

      <label>
        <input type="radio" name= "trip" value = "cash" class= "money">
        cash<br>
      </label>

      <label>
        <input type="radio" name= "trip" value = "other" class= "money">
        other
      </label>
    </div>
      <p> How did you get to your travel destination? </p>
      <br>
      <input type="checkbox" placeholder="plane" value="plane" class="checkbox">Plane
      <input type="checkbox" placeholder="boat" value="boat" class="checkbox">Boat
      <input type="checkbox" placeholder="train" value="train" class="checkbox">Train
      <input type="checkbox" placeholder="other" value="other" class="checkbox">Other

      <p> What is your favorite color? </p>
 <div class="chkcon">
      <label><input
         name="prefer"
         value="front-end-projects"
         type="checkbox"
         class="input-checkbox"
       />Front-end Projects</label
     >
     <br/>
     <label
      ><input
        name="prefer"
        value="front-end-projects"
        type="checkbox"
        class="input-checkbox"
      />back-end Projects</label
    >
    <br/>
    <label
     ><input
       name="prefer"
       value="front-end-projects"
       type="checkbox"
       class="input-checkbox"
     />love Projects</label
   ></div>


      <p> Please enter any additional comments or suggestions </p>
      <textarea rows="5" cols="50" maxlength="100" placeholder="Enter comments here"></textarea>
      <br>
      <input type="submit" value="submit" id = "submit">
      </form>

    </div>
</div>


-1

HTML 的外观将如下所示

<label class="checkLabel">
  <input type="radio" name="trip" value="cash" class="money">
  <span>Cash</span>
</label>

在你的 CSS 中。
.checkLabel {
  display: block;
}

.checkLabel input, .checkLabel span {
  display: inline-block;
  vertical-align: middle;
}

正如您所看到的,它将帮助您在标签中垂直对齐项目。


不幸的是,我进行了那些更改,但并没有起作用。它使得对齐更像三角形。 - traveler316

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接