我的意思是,单选按钮本身由一个圆形和中心的一个点组成(当按钮被选中时)。我想改变的是两者的颜色。使用CSS可以做到这一点吗?
当前回答
单选按钮是特定于每个操作系统/浏览器的原生元素。没有办法改变它的颜色/风格,除非你想实现自定义图像或使用自定义Javascript库,其中包括图像(例如这个-缓存链接)
其他回答
将单选按钮绑定到样式标签可能会有所帮助。进一步的细节在这个回答中。
正如Fred提到的,没有办法在颜色、大小等方面对单选按钮进行本地样式设置。但是您可以使用CSS Pseudo元素来设置任意给定单选按钮的冒名顶替者,并对其进行样式化。谈到JamieD所说的,关于我们如何使用:after Pseudo元素,您可以同时使用:before和:after来实现理想的外观。
这种方法的好处:
Style your radio button and also Include a label for content. Change the outer rim color and/or checked circle to any color you like. Give it a transparent look with modifications to background color property and/or optional use of the opacity property. Scale the size of your radio button. Add various drop shadow properties such as CSS drop shadow inset where needed. Blend this simple CSS/HTML trick into various Grid systems, such as Bootstrap 3.3.6, so it matches the rest of your Bootstrap components visually.
简短演示说明如下:
Set up a relative in-line block for each radio button Hide the native radio button sense there is no way to style it directly. Style and align the label Rebuilding CSS content on the :before Pseudo-element to do 2 things - style the outer rim of the radio button and set element to appear first (left of label content). You can learn basic steps on Pseudo-elements here - http://www.w3schools.com/css/css_pseudo_elements.asp If the radio button is checked, request for label to display CSS content (the styled dot in the radio button) afterwards.
HTML
<div class="radio-item">
<input type="radio" id="ritema" name="ritem" value="ropt1">
<label for="ritema">Option 1</label>
</div>
<div class="radio-item">
<input type="radio" id="ritemb" name="ritem" value="ropt2">
<label for="ritemb">Option 2</label>
</div>
CSS
.radio-item {
display: inline-block;
position: relative;
padding: 0 6px;
margin: 10px 0 0;
}
.radio-item input[type='radio'] {
display: none;
}
.radio-item label {
color: #666;
font-weight: normal;
}
.radio-item label:before {
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid #004c97;
background-color: transparent;
}
.radio-item input[type=radio]:checked + label:after {
border-radius: 11px;
width: 12px;
height: 12px;
position: absolute;
top: 9px;
left: 10px;
content: " ";
display: block;
background: #004c97;
}
一个简短的演示,看看它的行动
总之,不需要JavaScript,图像或电池。纯CSS。
对于那些喜欢从最小示例开始开发的人来说,这里有一个简单的自定义单选按钮,它不依赖于标签:
[type="radio"] { visibility: hidden; /* hide default radio button */ /* you may need to adjust margin here, too */ } [type="radio"]::before { /* create pseudoelement */ border: 2px solid gray; /* thickness, style, color */ height: .9em; /* height adjusts with font */ width: .9em; /* width adjusts with font */ border-radius: 50%; /* make it round */ display: block; /* or flex or inline-block */ content: " "; /* won't display without this */ cursor: pointer; /* appears clickable to mouse users */ visibility: visible; /* reverse the 'hidden' above */ } [type="radio"]:checked::before { /* selected */ /* add middle dot when selected */ /* slightly bigger second value makes it smooth */ /* even more (e.g., 20% 50%) would make it fuzzy */ background: radial-gradient(gray 36%, transparent 38%); } <br> <input type="radio" name="example" id="one" value="one"> <label for="one">one</label> <br> <br> <input type="radio" name="example" id="two" value="two"> <label for="two">two</label>
如果你使用react bootstrap Form。你可以这样做
HTML
<Form.Check
type="radio"
id="Radio-card"
label={`check me out`}
name="paymentmethod"
value="card"
/>
SCSS
.form-check {
display: flex;
align-items: center;
input[type="radio"] {
-moz-appearance: none;
appearance: none;
width: 11px;
height: 11px;
padding: 1px;
background-clip: content-box;
border: 1px solid hotpink;
background-color: white;
border-radius: 50%;
}
input[type="radio"]:checked {
outline: none;
background-color: hotpink;
border: 1px solid hotpink;
}
label {
font-size: 14px;
font-weight: 600;
}
}
我构建了@klewis的代码样本的另一个分支,通过使用:before/:after伪元素和隐藏的单选输入按钮来演示纯css和渐变。
HTML:
sample radio buttons:
<div style="background:lightgrey;">
<span class="radio-item">
<input type="radio" id="ritema" name="ritem" class="true" value="ropt1" checked="checked">
<label for="ritema">True</label>
</span>
<span class="radio-item">
<input type="radio" id="ritemb" name="ritem" class="false" value="ropt2">
<label for="ritemb">False</label>
</span>
</div>
:
CSS:
.radio-item input[type='radio'] {
visibility: hidden;
width: 20px;
height: 20px;
margin: 0 5px 0 5px;
padding: 0;
}
.radio-item input[type=radio]:before {
position: relative;
margin: 4px -25px -4px 0;
display: inline-block;
visibility: visible;
width: 20px;
height: 20px;
border-radius: 10px;
border: 2px inset rgba(150,150,150,0.75);
background: radial-gradient(ellipse at top left, rgb(255,255,255) 0%, rgb(250,250,250) 5%, rgb(230,230,230) 95%, rgb(225,225,225) 100%);
content: "";
}
.radio-item input[type=radio]:checked:after {
position: relative;
top: 0;
left: 9px;
display: inline-block;
visibility: visible;
border-radius: 6px;
width: 12px;
height: 12px;
background: radial-gradient(ellipse at top left, rgb(245,255,200) 0%, rgb(225,250,100) 5%, rgb(75,175,0) 95%, rgb(25,100,0) 100%);
content: "";
}
.radio-item input[type=radio].true:checked:after {
background: radial-gradient(ellipse at top left, rgb(245,255,200) 0%, rgb(225,250,100) 5%, rgb(75,175,0) 95%, rgb(25,100,0) 100%);
}
.radio-item input[type=radio].false:checked:after {
background: radial-gradient(ellipse at top left, rgb(255,225,200) 0%, rgb(250,200,150) 5%, rgb(200,25,0) 95%, rgb(100,25,0) 100%);
}
.radio-item label {
display: inline-block;
height: 25px;
line-height: 25px;
margin: 0;
padding: 0;
}
预览: https://www.codeply.com/p/y47T4ylfib