【问题标题】:Make label aligned to custom checkbox使标签与自定义复选框对齐
【发布时间】:2020-10-06 19:58:32
【问题描述】:

跟进这个问题Make custom checkbox label text row breaks like normal

我有一个自定义复选框,我想让标签文本与复选框 span 元素对齐。

我尝试了一些东西,但似乎没有任何动静。怎么解决?

      input[type="checkbox"].custom-switch {
        display: none;
      }

     .switch{
    display: inline-flex;
  }

  .switch:before {
    width: 30px;
    min-width: 30px;
    height: 15px;
    border-radius: 15px;
    border: 2px solid #ddd;
    background-color: #EEE;
    content: "";
    margin-right: 5px;
    transition: background-color 0.5s linear;
  }
  
  .switch:after {
    width: 11px;
    height: 11px;
    border-radius: 15px;
    background-color: #fff;
    content: "";
    transition: margin 0.1s linear;
    box-shadow: 0px 0px 5px #aaa;
    position: absolute;
    margin-top: 2px;
    margin-left: 2px;
  }

  input[type="checkbox"].custom-switch + label {
    cursor: pointer;
  }

  input[type="checkbox"].custom-switch:checked + label > .switch:before {
    background-color: #1a94ff;
  }
  
  input[type="checkbox"].custom-switch:checked +  label >.switch:after {
    margin: 2px 0 0 17px;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div>
  <input class="custom-switch" id="updateAllCheckbox" type="checkbox" style="top: 2px;">
  <label for="updateAllCheckbox" style="font-weight: normal; font-family: inherit;">
    <span class="switch"></span>
    Some text about updating
    <select class="form-control dropdownUpdate" style="width: 90px; display: initial; padding: 0px; height: 23px; margin-right: 3px;;margin-left: 5px;">
    <option value="red">Newest Ver</option>
    <option value="yellow">Old Version</option>
    <option value="green">Outdated Ver</option>
  </select>
  Even more text to format
  </label>
</div>
<br />
<br />
<br />
<div>
  <input class="custom-switch" id="test3" type="checkbox" style="top: 2px;">
  <label for="test3" style="font-weight: normal; font-family: inherit;">
    <span class="switch"></span>
    Checkbox with custom switch without a select element
  </label>
</div>

更新:

【问题讨论】:

    标签: html css checkbox


    【解决方案1】:

    我猜你的意思是垂直对齐。

    怎么样:

    label {
       /* replaces existing display: inline-block (i.e. 
          remove it from the inline style or replace it) */
       display: inline-flex; 
       align-items: center;
    }
    

    (顺便说一句:我通过玩https://flexboxfroggy.com/ 学到了这一点。这完全值得花时间!)

    【讨论】:

    • 我也会这样做,但我认为引导程序已将标签设置为内联块,它不会让它被覆盖,这使得事情变得复杂。至少我现在试图在一个片段中让它工作 10 分钟,它总是保持内联块
    • 那么内联样式是通过使用 JavaScript 的引导程序应用的吗?否则,OP 可能只能在内联样式中使用 display:inline-flex
    • 这可能会有效,但如果您知道我的意思,它看起来很“不干净”:D 混合内联样式和样式表。这就是为什么我试图在没有这个的情况下获得解决方案,但是是的,这是一种方法
    • 执行此操作时,文本换行会像我在开头链接的上一个问题中一样被破坏。我喜欢复选框现在一直在文本前面,但是如果选择元素之后只有一个单词并且空间变小,则选择之前的测试会换行,而不是选择之后的文本或选择元素本身
    • 这是我想要的图像:i.imgur.com/4RSazux.png
    猜你喜欢
    • 2017-07-27
    • 2018-02-20
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 2021-02-25
    相关资源
    最近更新 更多