我正在尝试设置单选按钮的样式,并使用CSS将标签与单选按钮内联。
但单选按钮看起来不对,其标签与其不符。
我需要实现这个目标:
这是我到目前为止所做的:
input[type="radio"]{
display:none;
}
input[type="radio"] + label
{
background: #fff;
border: solid 2px #000;
height: 20px;
width: 20px;
display:inline-block;
padding: 0 0 0 0px;
border-radius:50%;
}
input[type="radio"]:checked + label
{
background: #000;
height: 20px;
width: 20px;
display:inline-block;
padding: 0 0 0 0px;
border-radius:50%;
}
<input type="radio" id="male" name="gender" value="M">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="F">
<label for="female">Female</label>
编辑:
我需要所有单选按钮及其标签都在一行中。
答案 0 :(得分:2)
你可以在标签之前,所以它看起来像图像
input{
display: none;
}
label{
float: left;
margin-right: 10px
}
label span{
width: 16px;
height: 16px;
background-color: #fff;
border-radius: 50%;
border: 1px solid #222;
float: left;
margin-right: 5px;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
}
input:checked + label span:before{
content: '';
position: absolute;
width: 8px;
height: 8px;
border-radius: 50%;
background-color: #222;
}
&#13;
<input type="radio" id="male" name="gender" value="M"/>
<label for="male"><span></span>Male</label>
<input type="radio" id="female" name="gender" value="F"/>
<label for="female"><span></span>Female</label>
&#13;
答案 1 :(得分:0)
如果您使用标签同时显示实际的单选按钮和实际标签,您确实会遇到问题:
现在,您告诉您<label>
是一个20px宽的圆,同时包含您的文本。
试试这个,而不是:
input[type="radio"] + label::before
{
background: #fff;
border: solid 2px #000;
height: 20px;
width: 20px;
display:inline-block;
padding: 0 0 0 0px;
border-radius:50%;
margin-right:5px;
}
这会在您的标签上添加一个伪元素,您可以像设置任何<div>
或<span>
那样设置样式。
另外,对于你选中的单选按钮,我会简化:
input[type="radio"]:checked + label::before
{
background: #000;
}
答案 2 :(得分:0)
只需向::before
添加input + label
。
input[type="radio"]{
display:none;
}
input[type="radio"] + label::before
{
content:'';
background: #fff;
border: solid 2px #000;
height: 20px;
width: 20px;
display:inline-block;
padding: 0 0 0 0px;
border-radius:50%;
}
input[type="radio"]:checked + label::before
{
background: #000;
height: 20px;
width: 20px;
display:inline-block;
padding: 0 0 0 0px;
border-radius:50%;
}
<input type="radio" id="male" name="gender" value="M">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="F">
<label for="female">Female</label>
答案 3 :(得分:0)
如果您对某些HTML调整持开放态度,您可能会发现这更灵活:
确保为输入添加focus
状态,以确保键盘用户可以访问它。
input[type="radio"] {
position: absolute;
opacity: 0;
cursor: pointer;
}
label {
position: relative;
cursor: pointer;
padding-left: 30px;
display: inline-block;
line-height: 1.6;
margin-right: 15px;
}
span {
position: absolute;
left: 0;
top: 0;
height: 20px;
width: 20px;
background: #FFF;
border: 2px solid black;
border-radius: 50%;
}
span:after {
content: '';
position: absolute;
left: 5px;
top: 5px;
width: 10px;
height: 10px;
border-radius: 50%;
background: black;
display: none;
}
input[type="radio"]:checked~span:after {
display: block;
}
input[type="radio"]:focus~span {
outline: 2px solid orange;
}
<label for="male">Male
<input type="radio" id="male" name="gender" value="M">
<span></span>
</label>
<label for="female">Female
<input type="radio" id="female" name="gender" value="F">
<span></span>
</label>
答案 4 :(得分:0)
.container {
display: inline-block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 50%;
}
input:checked ~ .checkmark {
background-color: #2196F3;
}
.checkmark:after {
content: "";
position: absolute;
display: none;
}
input:checked ~ .checkmark:after {
display: block;
}
.checkmark:after {
top: 9px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
<label class="container">One
<input type="radio" checked="checked" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
希望这有助于.... :)