【问题标题】:Vertical-alignment label and checkboxes when label is split into two lines标签分为两行时的垂直对齐标签和复选框
【发布时间】:2014-08-31 17:15:12
【问题描述】:

有复选框,一切看起来都不错,但是当标签文本换成两行时,我无法强制我的复选框与标签居中对齐。它保持在顶部。 jsFiddle.

现在的样子:

预期:

HTML:

<input type="checkbox" id="c1" name="1" />
<label value="1" for="c1"><span></span>Lorem ipsum lorem ipsum</label>
<input type="checkbox" id="c2" name="2" />
<label value="2" for="c2"><span></span>Lorem ipsum lorem ipsum lorem ipsum 2</label>

CSS:

  body
{
    min-height:100%;
    width:100%;
    line-height: 1;
    font-family: 'Roboto', sans-serif;
    font-size:1em;
    display:table;
}   
input[type="checkbox"] {
       display:none;
   }
   input[type="checkbox"] + label span {
       display:inline-block;
       width:19px;
       height:19px;
       margin:-1px 8px 0 0;
       vertical-align:middle;
       background:url(http://oi59.tinypic.com/i5ngoj.jpg) left top no-repeat;
       float:left;
       cursor:pointer;
   }
   input[type="checkbox"]:checked + label span {
       background:url(http://oi59.tinypic.com/i5ngoj.jpg) -19px top no-repeat;
   }
   input[type="checkbox"]:checked + label {
       border: 1px #99cc00 solid;
       font-weight: 500;
   }
   label, .toggle {
       font-weight:300;
       margin-bottom:10px;
       padding:12px;
       display:block;
       max-width:100%;
       background-color:#fff;
       color:#4c4c4c;
       -webkit-border-radius: 3px;
       -moz-border-radius: 3px;
       border-radius: 3px;
       -webkit-box-shadow: 1px 1px 3px 0px rgba(0, 0, 0, 0.15);
       -moz-box-shadow: 1px 1px 3px 0px rgba(0, 0, 0, 0.15);
       box-shadow: 1px 1px 3px 0px rgba(0, 0, 0, 0.15);
   }

【问题讨论】:

  • 我为您添加了 Fiddle,但您似乎已将复选框设置为 display:none。
  • 不知道是什么问题。
  • 另外你还没有附加复选框 png 文件。
  • 问题是,当标签中的文本换行时,会分成两行,我不能强制复选框保持中间对齐。
  • 有一点时间,所以我用一个简单的解决方案更新了我的答案:)

标签: html css checkbox input vertical-alignment


【解决方案1】:

更新 - 更好的答案

在标签上显示背景图像。我将未选中和选中的图像拆分为单独的文件。

Have a fiddle!

HTML

<input type="checkbox" id="c1" />
<label for="c1">This is my label</label>

CSS

input[type=checkbox] {
    display: none
}
label {
    display: block;
    width: 100px;
    background: url(http://i.imgur.com/KwNXcwW.gif) left center no-repeat;
    padding: 0 0 0 28px;
    margin: 20px 0 0;
    cursor: pointer;
}
input:checked + label {
    background: url(http://i.imgur.com/4Zp4dZ0.gif) left center no-repeat;
}




旧答案

不替换复选框 - display: tabledisplay: table-cell; 会很好地工作。

在这个例子中,为了简单起见,我没有用图片替换复选框。

Have a fiddle!

HTML

<div>
    <input type="checkbox" id="c1" name="1" />
    <label value="1" for="c1">Lorem ipsum</label>
</div>

CSS

div {
    display: table;
    margin: 10px 0;
}
div input {
    display: table-cell;
    vertical-align: middle;
     width: 40px;
    height: 100%;
}
div label {
    display: table-cell;
    vertical-align: middle;
     width: 180px;
        padding: 3px 0 0;
}

【讨论】:

  • 太聪明了 :) 非常感谢!
猜你喜欢
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多