【问题标题】:How do I make radio buttons sit inside label and center text next to them?如何使单选按钮位于标签内并在它们旁边居中文本?
【发布时间】:2012-11-19 19:29:43
【问题描述】:

fiddle

* {
    font-family:sans-serif;
}
#quote_form table {
    border-collapse: collapse;
}
#quote_form td, #quote_form th {
    padding: 0;
    margin: 0;
}
#quote_form fieldset>label {
    display: block;
    font-weight: bold;
    margin-top: 7px;
}

#quote_form input {
    height: 16px;
    padding: 2px;
}
#quote_form select {
    height: 22px;
    padding: 2px;
}

#quote_form td>label {
    font-size: .9em;
}
#quote_form td {
    padding-right: 3px;
}
#quote_form td>input {
    width: 200px;
}
#quote_form .required {
    color: red;
}
#quote_form input {
    font-family: sans-serif;
}
#quote_form fieldset>input {
    width: 500px;
}
#quote_form fieldset>select {
    width:  504px;
}
#quote_form fieldset {
    border: 1px solid silver;
    padding: 10px;
}
#quote_form legend {
    margin-left: 15px;
    padding: 2px;
    font-weight: bold;
}
#quote_form .calendar {
    width: 80px;
}
#quote_form .datetime td {
    padding: 0;
}
#quote_form .datetime .date {
    padding-right: 4px;
}
#quote_form .datetime .hour {
    font-weight:bold;
}
#quote_form .datetime .minute {
    padding-right: 4px;
}
#quote_form .datetime .ampm input {
    margin: 0 2px 0 0;
}
#quote_form .datetime .ampm label {
    text-align: baseline;
    margin-right: 2px;
    background-color: #eeeeee;
    border-radius: 4px;
}
<form id="quote_form">
    <table class="datetime">
        <tbody>
            <tr>
                <td class="date"><input type="text" class="calendar"></td>
                <td class="hour"><select></select>:</td>
                <td class="minute"><select></select></td>
                <td class="ampm">
                    <label><input type="radio" name="pickup_ampm">AM</label>
                    <label><input type="radio" name="pickup_ampm">PM</label>
                </td>
            </tr>
            <tr>
                <td><label>Date</label></td>
                <td><label>HH</label></td>
                <td><label>MM</label></td>
                <td></td>
            </tr>
        </tbody>
    </table>
</form>

单选按钮从灰色标签中伸出。我希望它们坐得更低一点,以便它们位于标签文本旁边的中心。我尝试了负边距——没有用。

【问题讨论】:

  • input 不应该在label 之外吗?你真的必须把它们放在里面吗?
  • @SidharthMudgal:我不需要,但我喜欢。

标签: html css alignment


【解决方案1】:

我这里用的是windows8,效果很好。

随便试试这个方法。

label > input[type="radio"]
{
    position: relative; // Don't forgot this line
    top: -5px; // Change this value to set exact place.
}

另一种自动方式是

label > input[type="radio"]
{
    position: relative;
    vertical-align: middle;
}

【讨论】:

    【解决方案2】:

    如果我理解正确的话,这应该是你想要的。

    .ampm label{
        display:block;
        float: left;
        padding:2px 4px;
    }
    

    【讨论】:

    • 我还应该提到标签元素不应该环绕输入。你应该使用类似
      的东西。并将上述样式应用于 div 而不是标签。
    • 为什么?我特别喜欢包装输入元素,并且它使整个区域可点击,包括之间的任何空格,并且不需要“for”/“id”对,而且它从未在我使用过的任何浏览器中引起任何问题经过测试。
    • 这个 CSS 使标签背景覆盖了整个收音机,但仍然存在垂直居中的问题..虽然我现在可以用 line-height 做到这一点,因为它是一个块..跨度>
    • @Mark HTML 和 XHTML 规范允许隐式和显式标签。但是,某些辅助技术无法正确处理隐式标签(例如,)。来源:[链接]w3.org/TR/2008/NOTE-WCAG20-TECHS-20081211/H44.html
    • 那么只适用于屏幕阅读器?我会考虑的,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多