为单选按钮设置背景颜色

huangapple go评论76阅读模式
英文:

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 -->

&lt;div class=&quot;radioButton&quot;&gt;
  &lt;input type=&quot;radio&quot; id=&quot;color1&quot; name=&quot;color&quot; checked=&quot;true&quot;&gt;
  &lt;input type=&quot;radio&quot; id=&quot;color2&quot; name=&quot;color&quot;&gt;
  &lt;input type=&quot;radio&quot; id=&quot;color3&quot; name=&quot;color&quot;&gt;
&lt;/div&gt;

<!-- 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 &lt;input&gt; then add &lt;span&gt;, styling the &lt;span&gt;.

Remember to wrap &lt;span&gt; and &lt;input&gt; in &lt;label&gt; so the radio input will be checked when &lt;span&gt; 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 -->

&lt;div class=&quot;radioButton&quot;&gt;
    &lt;label&gt;
        &lt;input type=&quot;radio&quot; id=&quot;color1&quot; name=&quot;color&quot; checked=&quot;true&quot;&gt;
        &lt;span&gt;&lt;/span&gt;
    &lt;/label&gt;
    &lt;label&gt;
        &lt;input type=&quot;radio&quot; id=&quot;color2&quot; name=&quot;color&quot; checked=&quot;true&quot;&gt;
        &lt;span&gt;&lt;/span&gt;
    &lt;/label&gt;
    &lt;label&gt;
        &lt;input type=&quot;radio&quot; id=&quot;color3&quot; name=&quot;color&quot; checked=&quot;true&quot;&gt;
        &lt;span&gt;&lt;/span&gt;
    &lt;/label&gt;
&lt;/div&gt;

<!-- 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 -->

&lt;style&gt;
  .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;
  }
&lt;/style&gt;
&lt;div class=&quot;radioButton&quot;&gt;
  &lt;input type=&quot;radio&quot; id=&quot;color1&quot; name=&quot;color&quot; checked=&quot;true&quot;&gt;
  &lt;input type=&quot;radio&quot; id=&quot;color2&quot; name=&quot;color&quot;&gt;
  &lt;input type=&quot;radio&quot; id=&quot;color3&quot; name=&quot;color&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 13:50:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定