我的意思是,单选按钮本身由一个圆形和中心的一个点组成(当按钮被选中时)。我想改变的是两者的颜色。使用CSS可以做到这一点吗?
当前回答
简单的方法是使用重音色
CSS属性的作用是:为某些元素生成的用户界面控件设置强调色
支持重音颜色的浏览器目前将其应用于以下HTML元素:
<input type="checkbox">
<input type="radio">
<input type="range">
<progress>
一个可运行的示例
body { display: grid; padding: 3rem 0; } .accent { accent-color: #30cc7e; } form { display: grid; grid-auto-columns: fit-content(50%); grid-template-areas: "a a"; margin: auto; padding: 0; gap: 1rem; } form { display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin: auto; } form section:first-child { color-scheme: light; } form section:last-child { color-scheme: dark; } fieldset { border-radius: 8px; color-scheme: light; display: flex; flex: 1; flex-direction: column; gap: 1rem; padding: 1rem; } .dark { color-scheme: dark; } .dark fieldset { background: #100f33; border-color: #100f33; color: #fff; } .dark .accent { accent-color: hsla(180, 100%, 70%, 1); } h2 { margin: 0; } .notice { background: #fff9c4; border-radius: 6px; margin: 1.5rem auto; padding: 0.5rem; text-align: center; } @supports (accent-color: #fff) { .notice { display: none; } } <div class="notice"> Your browser does not support the <code>accent-color</code> property. </div> <form action=""> <fieldset> <h2>Checkboxes</h2> <div> <label for="checkbox"> Default </label> <input id="checkbox" type="checkbox" checked> </div> <div> <label for="checkbox-accent"> Accent </label> <input id="checkbox-accent" type="checkbox" class="accent" checked> </div> </fieldset> <fieldset> <h2>Radio</h2> <div> <input id="radio" type="radio" checked> <label for="radio"> Default </label> </div> <div> <input id="radio-accent" type="radio" class="accent" checked> <label for="radio-accent"> Accent </label> </div> </fieldset> <fieldset> <h2>Progress</h2> <div> <label for="progress"> Default </label> <progress id="progress" min="0" max="100" value="50"></progress> </div> <div> <label for="progress-accent"> Accent </label> <progress id="progress-accent" class="accent" min="0" max="100" value="50"></progress> </div> </fieldset> <fieldset> <h2>Range</h2> <div> <label for="range"> Default </label> <input id="range" type="range"> </div> <div> <label for="range-accent"> Accent </label> <input id="range-accent" class="accent" type="range"> </div> </fieldset> </form>
其他回答
正如其他人所说,没有办法在所有浏览器中实现这一点,所以跨浏览器实现这一点的最好方法是使用javascript。基本上你必须把你的单选按钮变成链接(完全可自定义通过CSS)。每次点击链接都将绑定到相关的单选框,切换他的状态和其他所有状态。
单选按钮是特定于每个操作系统/浏览器的原生元素。没有办法改变它的颜色/风格,除非你想实现自定义图像或使用自定义Javascript库,其中包括图像(例如这个-缓存链接)
您可以通过两种纯CSS方式实现自定义单选按钮
Via removing standard appearance using CSS appearance and applying custom appearance. Unfortunately this was doesn't work in IE. Demo: input[type="radio"] { /* remove standard background appearance */ -webkit-appearance: none; -moz-appearance: none; appearance: none; /* create custom radiobutton appearance */ display: inline-block; width: 25px; height: 25px; padding: 6px; /* background-color only for content */ background-clip: content-box; border: 2px solid #bbbbbb; background-color: #e7e6e7; border-radius: 50%; } /* appearance for checked radiobutton */ input[type="radio"]:checked { background-color: #93e026; } /* optional styles, I'm using this for centering radiobuttons */ .flex { display: flex; align-items: center; } <div class="flex"> <input type="radio" name="radio" id="radio1" /> <label for="radio1">RadioButton1</label> </div> <div class="flex"> <input type="radio" name="radio" id="radio2" /> <label for="radio2">RadioButton2</label> </div> <div class="flex"> <input type="radio" name="radio" id="radio3" /> <label for="radio3">RadioButton3</label> </div> Via hiding radiobutton and setting custom radiobutton appearance to label's pseudoselector. By the way no need for absolute positioning here (I see absolute positioning in most demos). Demo: *, *:before, *:after { box-sizing: border-box; } input[type="radio"] { display: none; } input[type="radio"]+label:before { content: ""; /* create custom radiobutton appearance */ display: inline-block; width: 25px; height: 25px; padding: 6px; margin-right: 3px; /* background-color only for content */ background-clip: content-box; border: 2px solid #bbbbbb; background-color: #e7e6e7; border-radius: 50%; } /* appearance for checked radiobutton */ input[type="radio"]:checked + label:before { background-color: #93e026; } /* optional styles, I'm using this for centering radiobuttons */ label { display: flex; align-items: center; } <input type="radio" name="radio" id="radio1" /> <label for="radio1">RadioButton1</label> <input type="radio" name="radio" id="radio2" /> <label for="radio2">RadioButton2</label> <input type="radio" name="radio" id="radio3" /> <label for="radio3">RadioButton3</label>
将单选按钮绑定到样式标签可能会有所帮助。进一步的细节在这个回答中。
这对我很有效,
简单添加css属性:
输入(type = "电台"]{强调色:红色;}
这里是资源链接
推荐文章
- 在notepad++中格式化代码
- 表单中包含表单,可以吗?
- 如何创建表只使用<div>标签和Css
- html5 - canvas元素-多层
- 如何确保<select>表单字段被禁用时提交?
- 如何在HTML文本换行?
- 如何在JavaScript中转义单引号(')?
- Chrome开发工具:如何找出什么是覆盖CSS规则?
- 如何使用HTML/CSS在文本中插入空格/制表符
- 用背景图像填充SVG路径元素
- 将JSON显示为HTML
- HTML被认为是一种编程语言吗?
- 如何修复favicon.ico错误:“Failed to load resource: net::ERR_EMPTY_RESPONSE”
- 大Glyphicons
- 这个CSS是如何产生一个圆的?