如何使单选按钮看起来像切换按钮
我希望一组单选按钮看起来像一组切换按钮(但功能仍然像单选按钮一样)。它们不必看起来完全像切换按钮。
我怎样才能只使用 CSS 和 HTML 来做到这一点?
编辑:当选中/取消选中按钮时,我会满意地使小圆圈消失并更改样式。
I want a group of radio buttons to look like a group of toggle buttons (but still function like radio buttons). It's not necessary that they look exactly like toggle buttons.
How can I do this only with CSS and HTML?
EDIT: I will be satisfied making the little circle disappear and changing the style when the button is checked/unchecked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
根据您想要支持的浏览器,您可以使用
:checked 伪类
选择器除了隐藏单选按钮之外。
使用此 HTML:
您可以使用类似以下 CSS 的内容:
例如,(为了保持自定义 CSS 的简洁)如果您使用 Bootstrap,您可以将
class="btn"
添加到您的元素中,并适当地设置它们的样式以创建一个如下所示的切换:
< img src="https://i.sstatic.net/CREwq.png" alt="Bootstrap-aided radio Switch">
...这只需要以下额外的 CSS:
我已经包含了这个以及额外的后备单选按钮切换 jsFiddle 演示中的样式。请注意,
:checked
仅在 IE 9 中受支持,因此此方法仅限于较新的浏览器。但是,如果您需要支持 IE 8 并且愿意依靠 JavaScript*,您可以破解对
:checked
的伪支持,而不会有太多困难(尽管您可以直接设置类)那时的标签上)。使用一些快速而肮脏的 jQuery 代码作为解决方法的示例
:然后可以对 CSS 进行一些更改来完成操作:
*如果您使用的是 Modernizr,则可以使用
:selector
test 帮助确定您是否需要后备。我在示例代码中将我的测试称为“checkedselector”,随后仅在测试失败时才设置 jQuery 事件处理程序。Depending on which browsers you aim to support, you could use the
:checked
pseudo-class selector in addition to hiding the radio buttons.Using this HTML:
You could use something like the following CSS:
For instance, (to keep the custom CSS brief) if you were using Bootstrap, you might add
class="btn"
to your<label>
elements and style them appropriately to create a toggle that looks like:...which just requires the following additional CSS:
I've included this as well as the extra fallback styles in a radio button toggle jsFiddle demo. Note that
:checked
is only supported in IE 9, so this approach is limited to newer browsers.However, if you need to support IE 8 and are willing to fall back on JavaScript*, you can hack in pseudo-support for
:checked
without too much difficulty (although you can just as easily set classes directly on the label at that point).Using some quick and dirty jQuery code as an example of the workaround:
You can then make a few changes to the CSS to complete things:
*If you're using Modernizr, you can use the
:selector
test to help determine if you need the fallback. I called my test "checkedselector" in the example code, and the jQuery event handler is subsequently only set up when the test fails.这是我上面发布的那个漂亮的 CSS 解决方案 JS Fiddle 示例的版本。
http://jsfiddle.net/496c9/
HTML
CSS
采用彩色按钮设计:)
Here's my version of that nice CSS solution JS Fiddle example posted above.
http://jsfiddle.net/496c9/
HTML
CSS
Styled with coloured buttons :)
纯 CSS 和 HTML(按照要求)带有动画! (还有复选框!)
示例图像(您可以运行下面的代码):
在寻找真正干净、直接的东西之后,我最终建造了这是从 另一代码 进行的一个简单更改,该代码仅考虑复选框,所以我尝试了RADIOS 的功能也很有效(!)。
CSS (SCSS) 完全来自@mallendeo(根据 JS 积分建立),我所做的只是将输入类型更改为 RADIO,并为所有无线电开关指定相同的名称......瞧! !他们会自动停用一个到另一个!
非常干净,正如你所要求的,它只是 CSS 和 HTML!
这正是我在尝试和编辑十多个选项(其中大部分需要 jQuery,或者不允许标签,甚至与当前浏览器不兼容)之后 3 天以来一直在寻找的东西。这个已经拥有了一切!
我有义务在此处包含代码,以便您可以看到一个有效的示例,因此:
如果您运行该代码片段,您会看到我选中并禁用了 iOS 无线电,这样您就可以观察激活另一个无线电时它是如何受到影响的。我还为每个收音机添加了 2 个标签,一个在前面,一个在后面。还包括在同一窗口中显示工作复选框的原始代码的副本。
PURE CSS AND HTML (as asked) with ANIMATIONS! (and Checkboxes TOO!)
Example Image (you can run the code below):
After looking for something really clean and straight forward, I ended up building this with ONE simple change from another code that was built only thinking on checkboxes, so I tryed the funcionality for RADIOS and it worked too(!).
The CSS (SCSS) is fully from @mallendeo (as established on the JS credits), what I did was simply change the type of the input to RADIO, and gave the same name to all the radio switches.... and VOILA!! They deactivate automatically one to the other!!
Very clean, and as you asked it's only CSS and HTML!!
It is exactly what I was looking for since 3 days after trying and editing more than a dozen of options (which mostly requiered jQuery, or didn't allow labels, or even wheren't really compatible with current browsers). This one's got it all!
I'm obligated to include the code in here to allow you to see a working example, so:
If you run the snippet, you'll see I leave the iOS radio checked and disabled, so you can watch how it is also affected when activating another one. I also included 2 labels for each radio, one before and one after. The copy of the original code to show the working checkboxes in the same window is also included.
以下是适用于所有浏览器的解决方案(还有 IE7 和 IE8;未检查 IE6):
http:// /jsfiddle.net/RkvAP/230/
HTML
JS
CSS
使用最少的 JS(jQuery,两行)。
Here is the solution that works for all browsers (also IE7 and IE8; didn't check for IE6):
http://jsfiddle.net/RkvAP/230/
HTML
JS
CSS
Makes use of minimal JS (jQuery, two lines).
受到 Michal B. 回答的启发。如果你使用引导程序..
Inspired by Michal B. answer. If you use bootstrap..
我通常用 CSS 隐藏真正的单选按钮(或将它们放入单独的隐藏输入中),放入我想要的图像(您可以使用无序列表并将样式应用到 li 元素),然后使用单击事件来切换输入。这种方法还意味着您可以让未使用普通 Web 浏览器的用户可以访问内容 - 只需默认隐藏您的 ul 并显示单选按钮即可。
I usually hide the real radio buttons with CSS (or make them into individual hidden inputs), put in the imagery I want (you could use an unordered list and apply your styles to the li element) and then use click events to toggle the inputs. That approach also means you can keep things accessible for users who aren't on a normal web browser-- just hide your ul by default and show the radio buttons.
我知道这是一个老问题,但由于我只是想这样做,所以我想我会发布我最终得到的结果。因为我使用的是 Bootstrap,所以我选择了 Bootstrap 选项。
HTML
jQuery
我选择将值存储在隐藏字段中,以便我可以轻松地在服务器端获取值。
I know this is an old question, but since I was just looking to do this, I thought I would post what I ended up with. Because I am using Bootstrap, I went with a Bootstrap option.
HTML
jQuery
I chose to store the value in a hidden field so that it would be easy for me to get the value server-side.
HTML:
CSS:
现场演示: http://jsfiddle.net/scymE/1/
HTML:
CSS:
Live demo: http://jsfiddle.net/scymE/1/