我正在尝试使用 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>
编辑:
我需要所有单选按钮及其标签都在一行中。
您可以在标签上之前,因此它看起来像图像
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;
}
<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>
如果你对一些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>
如果您同时使用标签来显示实际的单选按钮和实际的标签,您确实会遇到问题:现在,你告诉你<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;
}
只需在input + label
中添加一个::before
即可。
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>
.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>
希望这有帮助。 :)