radio在html页面放大,<input type="radio">

本文展示了如何使用HTML和CSS来增强网页表单元素的样式和用户体验。通过添加`<fieldset>`和`<legend>`元素提升语义化,使用`-webkit-appearance`和`:checked`伪类创建自定义样式的单选按钮,并利用CSS过渡效果实现平滑的交互变化。此外,还特别强调了浏览器兼容性和自定义按钮及表单元素颜色的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The following example shows a slightly more thorough version of the example we've seen throughout the article, with some additional styling, and with better semantics established through use of specialized elements. The HTML looks like this:

Please select your preferred contact method:

name="contact" value="email" checked>

Email

name="contact" value="phone">

Phone

name="contact" value="mail">

Mail

Submit

There's not much new to note here except for the addition of

and elements, which help to group the functionality nicely and in a semantic way.

The CSS involved is a bit more significant:

html {

font-family: sans-serif;

}

div:first-of-type {

display: flex;

align-items: flex-start;

margin-bottom: 5px;

}

label {

margin-right: 15px;

line-height: 32px;

}

input {

-webkit-appearance: none;

-moz-appearance: none;

appearance: none;

border-radius: 50%;

width: 16px;

height: 16px;

border: 2px solid #999;

transition: 0.2s all linear;

outline: none;

margin-right: 5px;

position: relative;

top: 4px;

}

input:checked {

border: 6px solid black;

}

button,

legend {

color: white;

background-color: black;

padding: 5px 10px;

border-radius: 0;

border: 0;

font-size: 14px;

}

button:hover,

button:focus {

color: #999;

}

button:active {

background-color: white;

color: black;

outline: 1px solid black;

}

Most notable here is the use of the -moz-appearance property (with prefixes needed to support some browsers). By default, radio buttons (and checkboxes) are styled with the operating system's native styles for those controls. By specifying appearance: none, you can remove the native styling altogether, and create your own styles for them. Here we've used a border along with border-radius and a transition to create a nice animating radio selection. Notice also how the :checked pseudo-class is used to specify the styles for the radio button's appearance when selected.

Compatibility note: If you wish to use the appearance property, you should test it very carefully. Although it is supported in most modern browsers, its implementation varies widely. In older browsers, even the keyword none does not have the same effect across different browsers, and some do not support it at all. The differences are smaller in the newest browsers.

Notice that when clicking on a radio button, there's a nice, smooth fade out/in effect as the two buttons change state. In addition, the style and coloring of the legend and submit button are customized to have strong contrast. This might not be a look you'd want in a real web application, but it definitely shows off the possibilities.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值