【问题标题】:different radio style inside a form表单内的不同收音机样式
【发布时间】:2018-05-15 14:41:47
【问题描述】:

我的表格很长。里面有一个颜色选择单选按钮 如何在不影响所有其他收音机的情况下为此收音机设置 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>

【问题讨论】:

  • 你不能只设置 id 的样式吗?
  • 请更新我做的sn-p
  • 使用 javascript 切换类

标签: html css forms radio


【解决方案1】:

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

以下是来自 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>

【讨论】:

    【解决方案2】:

    只需添加一个名为exemption 的类,然后仅当该类出现在最顶层时才允许应用 CSS,因此您的其他单选按钮不会受到影响。我已经使单选按钮颜色选择器工作了,请告诉我其中缺少什么!

    .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;
    }
    <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>

    【讨论】:

    • @Loef 这个答案对你有帮助吗?
    猜你喜欢
    • 2012-06-23
    • 2011-10-20
    • 2016-11-21
    • 1970-01-01
    • 2020-02-27
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多