html - 为什么这些被复选框破坏的单选按钮在被选中时会跳转?

标签 html css checkbox

我正在使用复选框标签 hack 来为单选按钮添加特殊的类似复选框的样式。 我有两种复选框样式,常规样式和 .angabe。

正如您在 JSFiddle 中看到的那样,当 .angabe radio 被 :checked 时,框会“跳”上几个 px。尝试了几个不同的 top: 位置都没有很好的效果。有什么建议吗?

这是标记:

<div class="angabe">
    <input type="radio" value="1" id="ohne">
    <label for="ohne">Mit Angabe</label>
    <input type="radio" value="0" id="mit">
    <label for="mit">Ohne Angabe</label>
</div>

<div class="row">
    <input type="radio" id="pregnancy[1]"><label for="pregnancy[1]">Pregnancy 1</label>
    <input type="radio" name="[pregnancy]" value="3" id="pregnancy[3]"><label for="pregnancy[3]">Pregnancy 2</label>
    <input type="radio" name="[pregnancy]" value="3" id="pregnancy[5]"><label for="pregnancy[5]">Pregnancy 3</label>
</div>

还有 CSS(去掉了长的渐变声明,但仍然很长):

body:nth-of-type(1) input[type="radio"], body:nth-of-type(1) .module.forms .indicationbox input { 
    display: none; 
}

body:nth-of-type(1) input[type="radio"] + label     {
    position:relative;
    margin-left:20px;
}

body:nth-of-type(1) input[type="radio"] + label:after  {
    position:absolute;
    left:4px;
    top:7px;

    content: "\00a0"; /* character: space */
    height: 10px;
    width: 10px;
    display: inline-block;
    font-size: 12px;
    line-height: 14px;
    text-align: center;

    -webkit-box-shadow: inset 1px 2px 3px rgba(50, 50, 50, 0.5);
    -moz-box-shadow:    inset 1px 2px 3px rgba(50, 50, 50, 0.5);
    box-shadow:         inset 1px 2px 3px rgba(50, 50, 50, 0.5);

    background:#c6e0b3;
} 

body:nth-of-type(1) input[type="radio"] + label:before  {
    left:0px;
    top:0px;
    content: "\00a0"; /* character: space */
    height: 18px;
    width: 18px;
    margin-right:5px;
    display: inline-block;
    font-size: 12px;
    line-height: 16px;
    padding:0px;
    text-align: center;
    background-color: #ebd196;
    -webkit-box-shadow: 1px 2px 3px rgba(50, 50, 50, 0.5);
    -moz-box-shadow:    1px 2px 3px rgba(50, 50, 50, 0.5);
    box-shadow:         1px 2px 3px rgba(50, 50, 50, 0.5);
}

body:nth-of-type(1) input[type="radio"]:checked + label:after {
    content: "\02714"; /* character: tick mark */
}


/** Special Angabe radio styles **/ 
body:nth-of-type(1) .angabe input[type="radio"]:checked + label:before {
    top: 0px;
    left:0px;
    height: 11px;
    width: 11px;
    border: 4px solid #324b23;
    background: rgba(255,255,255,0.4);
}

body:nth-of-type(1) .angabe input[type="radio"]:checked + label:after {
    top: 6px;
    left:6px;
    width:7px;
    height:7px;
    background: #324b23; 
}

body:nth-of-type(1) .angabe input[type="radio"]:checked + label:after {
    content: "\00a0"; /* character: space */
}

最佳答案

试试这个改变:

body:nth-of-type(1) input[type="checkbox"] + label:after, body:nth-of-type(1) input[type="radio"] + label:after {
top: 3px;
}
body:nth-of-type(1) input[type="checkbox"] + label:before, body:nth-of-type(1) input[type="radio"] + label:before {
position: relative;
top: -4px;
}
body:nth-of-type(1) .angabe input[type="radio"]:checked + label:before {
top: 1px;
margin-right: 4px;
}

JSFIDDLE

关于html - 为什么这些被复选框破坏的单选按钮在被选中时会跳转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19424228/

相关文章:

html - Bootstrap 等高列中断响应

php - 使用 XMLhttpRequest 加载的页面上的复选框

html - 在 checkbox_wrapper 中按下时切换

javascript - 获取 block 元素文本的实际宽度,而不是 JQuery 中的整个 block 元素本身

jquery - css 缩放会递减但不会正确递增

html - 跨浏览器滚动条自定义

c# - 防止复选框选中事件

css - HTML5 CSS 等高列不适用于图像 [Table/Table Cell Method]

html - 当链接文本跨越多行时,最小化 <a> 链接行之间的空间

javascript - 使用 Angular 样式加载 CSS