表单内的不同无线电样式

时间:2017-12-01 13:17:11

标签: html css forms radio

我的表格很长。里面有一个颜色挑选单选按钮 如何在不影响所有其他无线电的情况下为此收音机制作css例外?



input[type="radio"] {
  display: none;
}

input[type="radio"]:checked+label span {
  transform: scale(1.25);
}

input[type="radio"]:checked+label .red {
  border: 2px solid #711313;
}

label {
  display: inline-block;
  width: 25px;
  height: 25px;
  margin-right: 10px;
  cursor: pointer;
}

label:hover span {
  transform: scale(1.25);
}

label span {
  display: block;
  width: 100%;
  height: 100%;
  transition: transform .2s ease-in-out;
}

label span.red {
  background: #DB2828;
}

label span.orange {
  background: #F2711C;
}

<div class="radio">
  <input type="radio" name="color" id="red" value="red" />
  <label for="red"><span class="red"></span></label>

  <input type="radio" name="color" id="green" />
  <label for="green"><span class="green"></span></label>

  <input type="radio" name="color" id="yellow" />
  <label for="yellow"><span class="yellow"></span></label>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您可以将该实体包装在容器中并为其指定类。

以下是w3schools的示例代码。

<html>
<style>
/* The container */
.container {
display: 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;
}

/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
}

/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196F3;
}

 /* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
</style>
<body>

<h1>Custom Checkboxes</h1>
<label class="container">One
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>
<label>Two
  <input type="checkbox">
  <span class="checkmark"></span>
</label><br/>
<label>Three
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<label class="container">Four
   <input type="checkbox">
  <span class="checkmark"></span>
</label>

</body>
</html>

答案 1 :(得分:0)

只需添加一个名为exemption的类,然后只有当类位于最顶层时才允许应用CSS,因此您的其他单选按钮不会受到影响。我已经让单选按钮颜色选择器工作了,请让我知道这里缺少什么!

&#13;
&#13;
.exemption input[type="radio"] {
  display: none;
}

.exemption input[type="radio"]:checked+label span {
  transform: scale(1.25);
}

.exemption input[type="radio"]:checked+label .red {
  border: 2px solid #711313;
}

.exemption input[type="radio"]:checked+label .yellow {
  border: 2px solid darkyellow;
}
.exemption input[type="radio"]:checked+label .red {
  border: 2px solid darkred;
}

.exemption label {
  display: inline-block;
  width: 25px;
  height: 25px;
  margin-right: 10px;
  cursor: pointer;
}

.exemption label:hover span {
  transform: scale(1.25);
}

.exemption label span {
  display: block;
  width: 100%;
  height: 100%;
  transition: transform .2s ease-in-out;
}

.exemption label span.red {
  background: #DB2828;
}
.exemption label span.green {
  background: green;
}
.exemption label span.yellow {
  background: yellow;
}
&#13;
<fieldset>
  <div class="radio exemption">

    <input type="radio" name="color" id="red" value="red" />
    <label for="red"><span class="red"></span></label>

    <input type="radio" name="color" id="green" />
    <label for="green"><span class="green"></span></label>

    <input type="radio" name="color" id="yellow" />
    <label for="yellow"><span class="yellow"></span></label>
  </div>
</fieldset>
&#13;
&#13;
&#13;