css - 尝试自定义我的单选按钮,但什么都不会改变
问题描述
我想将选中的按钮更改为不同的颜色。我没有checked
属性,因为我通过对象映射以显示值。我已经尝试定位输入标签,但似乎没有任何效果。我也希望能够在悬停时改变颜色,但我也没有运气。
这是我的CSS:
.radio-btn {
margin: 0 10px 10px 0;
background-color: black;
}
.radio-btn:hover {
background-color: black;
}
这是按钮文件:
import React from "react";
const EyewearPurchaseBtn = (props) => (
<div>
<form className="eyewear-purchase-form" onSubmit={props.handleSubmit}>
<h3>Select Size</h3>
{
props.sizes.map( size => (
<div key={size} >
<input onChange={props.handleChange} className="radio-btn" type="radio" name="purchaseVal" value={size} />
<label className="eyewear-purchase-label">{size}</label><br/>
</div>
))
}
<button className="buy-now">Buy Now</button>
</form>
</div>
);
export default EyewearPurchaseBtn;
解决方案
我不知道它是否正是您正在寻找的,但您可以使用 css 创建假单选按钮。这是一个例子:
input[type="radio"] {
opacity: 0;
z-index: 1;
}
label { position: relative; }
label:before, label:after {
content: "";
position: absolute;
border-radius: 50%;
top: 50%;
transform: translateY(-50%);
z-index: -1;
}
label:before {
width: 15px;
height: 15px;
background-color: #3b3a39;
left: -20px;
}
label:after {
width: 7px;
height: 7px;
background-color: #f0e9e9;
left: -16px;
opacity: 0;
}
input[type="radio"]:checked + label:after { opacity: 1; }
input[type="radio"]:hover + label:before { background-color: #74706c; }
<div>
<input type="radio" name="purchaseVal" checked />
<label>Label 1</label>
</div>
<div>
<input type="radio" name="purchaseVal" />
<label>Label 2</label>
</div>
推荐阅读
- python - 为什么在计算逻辑回归的准确性时使用`tf.reduce_mean`?
- r - R 查看器边距问题
- c# - .NET Core 使用 osascript 在 Mac 中打开新终端时出错
- reactjs - useDispatch 钩子以错误的顺序触发,
- r - 如何为其名称存储在变量中的列创建 facet_wrap()
- javascript - 如何从反应中访问环境变量/如何注入环境变量来反应
- c - 如何在 C 中解决这个 Char 矩阵问题?
- vue.js - Vue从方法中访问数据
- spring - Spring 如何将更多用户信息添加到 Principal 对象中?
- networking - 使用 Delaunay 三角剖分绘制 TIN 并找到表面积