html - 如何自定义样式图像和单选按钮?
问题描述
如何使用单选按钮和图像制作类似的网页?
在此,我制作了一个标签和 din,我为该名称添加了一个单选按钮、一个图像标签和垃圾邮件。然后我尝试使用引导程序对其进行样式设置。
html:
<div>
<h2> Choose Accomodation Type </h2>
<div >
<label className="btn">
<input type="radio" name="test" id="option1" autocomplete="off" checked />
<img src={appartment} />
<span class="checkmark"></span>
<p>Apartment</p>
</label>
<label className="btn">
<input type="radio" name="test" id="option1" autocomplete="off" checked />
<img src={house} />
<span class="checkmark"></span>
<p>house</p>
</label>
</div>
<div>
<button className="btn btn-light btn-lg"> Cancel</button>
<button className="btn btn-outline-primary btn-lg"> Next </button>
</div>
img {
width:150px;
height: 150px;
margin:30px;
padding: 20px;
}
.btn{
margin: 15px;
padding: 5px;
}
[type=radio] {
margin:0;padding:0;
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
}
解决方案
- 为您的输入添加标签。
- 为输入添加
visibility: hidden
。 - 在标签内放置一个 div 并开始设置样式。您可以使用
:checked
伪类来查看单选按钮是否被选中。
HTML
<label for="my-btn">
<div></div>
</label>
<input id="my-btn" type="radio"/>
CSS
input {
visibility: hidden;
}
div {
/* custom styles */
}
input:checked div {
/* custom styles */
}
现在,一旦您单击包含自定义 div 的标签,就会选中单选按钮。