【问题标题】:Vertically align text next to checkbox垂直对齐复选框旁边的文本
【发布时间】:2016-09-05 12:15:39
【问题描述】:

我正在尝试在其旁边放置一个带有一些文本的复选框,以解释该复选框的作用。我想让他们的位置absolute 因为他们的包装 div 里面有其他东西。

虽然我将他们的两个top 设置为相同的值,但复选框似乎比文本更高。

我做了一个 jsfiddle 来解释我的意思:https://jsfiddle.net/9jx5t4xL/

我应该将复选框的top 设置为比文本大一点,还是有更好的方法将它们垂直对齐?

【问题讨论】:

    标签: html css


    【解决方案1】:

    使用 position% 值 不是一个好的选择,您可以使用很棒的 flexbox 布局来轻松实现:

    代码片段:

    #wrapper {
      height: 200px;
      width: 250px;
      border: 1px solid red;
      position: relative;
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-align: center;
      -webkit-align-items: center;
      -ms-flex-align: center;
      align-items: center;
      min-height: 24em;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
      -ms-flex-pack: center;
      justify-content: center;
    }
    label {
      -webkit-box-flex: 1;
      -webkit-flex: 1;
      -ms-flex: 1;
      flex: 1;
      text-align: center;
    }
    input {
      margin: 0;
      vertical-align: middle;
    }
    <div id="wrapper">
      <label>Caption for input
        <input type="checkbox" />
      </label>
    </div>

    【讨论】:

      【解决方案2】:

      改变你的CSS

      p{
            position: absolute;
            top: 40%;
            display: inline-block;
            left: 5%;
          }
      

      #wrapper {
        height: 200px;
        width: 250px;
        border: 1px solid red;
        position: relative;
      }
      
      p{
        position: absolute;
        top: 40%;
        display: inline-block;
        left: 5%;
      }
      
      input{
        position: absolute;
        top: 50%;
        display: inline-block;
        left: 70%;
      }
      <div id="wrapper">
        <p>Caption for input</p>
        <input type="checkbox"/>
      </div>

      【讨论】:

        猜你喜欢
        • 2013-07-25
        • 2017-02-07
        • 2010-10-04
        • 1970-01-01
        • 1970-01-01
        • 2021-08-08
        • 2019-07-11
        • 2013-09-12
        相关资源
        最近更新 更多