英文:
Styling radio button - background color
问题
我希望单选按钮的背景内部颜色与容器的背景颜色相同,并且不要有选定的边框。
我尝试过使用背景颜色和颜色,但没有成功。在HTML中,这些按钮不在form标签中,目前我不需要它,我甚至尝试添加它以查看是否会有变化,但没有结果,有什么想法吗?
我知道我可以简单地使用普通按钮样式,但我希望实现“仅选择一个”的选择。我可以用JS编写它,但如果可能的话,样式化单选按钮会更好!
当前情况:
期望结果:
.radioButton {
  background-color: rgb(37, 45, 68);
  border-radius: 50px;
}
input {
  accent-color: red;
}
<div class="radioButton">
  <input type="radio" id="color1" name="color" checked="true">
  <input type="radio" id="color2" name="color">
  <input type="radio" id="color3" name="color">
</div>
英文:
I want that the background inner color of the radio buttons are the same as the background color of the container, and no border around the selection.
I tried with background color, color, but didn't work.
In the HTML the buttons are not into a form tag, by now I don't need it, I've even tried to add it to see if something changed but without results, any idea?
I know i can just put normal buttons styling easier, but i want the "one-only" selection. I could code it in JS but if possible to style the radio button is better!
Current situation: 
Desired result: 
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.radioButton {
  background-color: rgb(37, 45, 68);
  border-radius: 50px;
}
input {
  accent-color: red;
}
<!-- language: lang-html -->
<div class="radioButton">
  <input type="radio" id="color1" name="color" checked="true">
  <input type="radio" id="color2" name="color">
  <input type="radio" id="color3" name="color">
</div>
<!-- end snippet -->
答案1
得分: 3
隐藏 <input> 然后添加 <span>,对 <span> 进行样式设置。
记得将 <span> 和 <input> 包裹在 <label> 中,这样当点击 <span> 时将选中单选按钮。
.radioButton {
  background-color: rgb(37, 45, 68);
  border-radius: 50px;
  width: max-content;
  padding: 2px 6px;
}
input {
  display: none;
}
input+span {
  display: inline-block;
  border-radius: 50%;
  width: 12px;
  aspect-ratio: 1;
  background-color: rgb(37, 45, 68);
}
input:checked+span {
  background-color: red;
}
<div class="radioButton">
    <label>
        <input type="radio" id="color1" name="color" checked="true">
        <span></span>
    </label>
    <label>
        <input type="radio" id="color2" name="color" checked="true">
        <span></span>
    </label>
    <label>
        <input type="radio" id="color3" name="color" checked="true">
        <span></span>
    </label>
</div>
英文:
Hide <input> then add <span>, styling the <span>.
Remember to wrap <span> and <input> in <label> so the radio input will be checked when <span> was clicked.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.radioButton {
  background-color: rgb(37, 45, 68);
  border-radius: 50px;
  width: max-content;
  padding: 2px 6px;
}
input {
  display: none;
}
input+span {
  display: inline-block;
  border-radius: 50%;
  width: 12px;
  aspect-ratio: 1;
  background-color: rgb(37, 45, 68);
}
input:checked+span {
  background-color: red;
}
<!-- language: lang-html -->
<div class="radioButton">
    <label>
        <input type="radio" id="color1" name="color" checked="true">
        <span></span>
    </label>
    <label>
        <input type="radio" id="color2" name="color" checked="true">
        <span></span>
    </label>
    <label>
        <input type="radio" id="color3" name="color" checked="true">
        <span></span>
    </label>
</div>
<!-- end snippet -->
答案2
得分: 1
你可以使用 CSS 的 appearance: none 来移除浏览器的默认样式,然后添加自己的样式:
<style>
  .radioButton {
    background-color: rgb(37, 45, 68);
    border-radius: 50px;
  }
  
  input {
    appearance: none;
    -webkit-appearance: none;
    background: transparent;
    width: 1em;
    height: 1em;
    border-radius: 50%;
    border-color: transparent;
  }
  
  input:checked {
    background: red;
  }
</style>
<div class="radioButton">
  <input type="radio" id="color1" name="color" checked="true">
  <input type="radio" id="color2" name="color">
  <input type="radio" id="color3" name="color">
</div>
英文:
You can use CSS appearance: none to remove the browser default styling and then add in your own:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<style>
  .radioButton {
    background-color: rgb(37, 45, 68);
    border-radius: 50px;
  }
  
  input {
    appearance: none;
    -webkit-appearance: none;
    background: transparent;
    width: 1em;
    height: 1em;
    border-radius: 50%;
    border-color: transparent;
  }
  
  input:checked {
    background: red;
  }
</style>
<div class="radioButton">
  <input type="radio" id="color1" name="color" checked="true">
  <input type="radio" id="color2" name="color">
  <input type="radio" id="color3" name="color">
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论