带有 CSS appearance 属性的自定义单选按钮
我们使用 appearance 属性根据用户的操作系统以本机样式来设计元素。
语法
CSS appearance 属性的语法如下 −
Selector { appearance: /*value*/; -webkit-appearance: /*value*/; /*for Safari and Chrome */ -moz-appearance: /*value*/; /*for Firefox */ }
创建自定义单选按钮
要创建自定义自选框,我们设置了带 border-radius 和 appearance 的以下样式。appearance 设置为 none −
input[type=radio] { appearance: none; -webkit-appearance: none; -moz-appearance: none; padding: 10px; background-color: orange; border-radius:50%; }
在单选按钮选中后,背景色将发生变化 −
input[type=radio]:checked { background-color: magenta; }
将光标悬停在单选按钮上时,光标将发生变化 −
input[type=radio]:hover { cursor: pointer;
示例
以下示例说明了自定义单选按钮 −
<!DOCTYPE html> <html> <head> <style> input[type=radio] { appearance: none; -webkit-appearance: none; -moz-appearance: none; padding: 10px; background-color: orange; border-radius:50%; } input[type=radio]:checked { background-color: magenta; } input[type=radio]:hover { cursor: pointer; } </style> </head> <body> <h1>Gender</h1><br/> <form> <label for="r1">Male</label> <input type="radio" id="r1" name="btn" value="v1"> <input type="radio" id="r2" name="btn" value="v2"> <label for="r2">Female</label> </form> </body> </html>
创建动画自定义单选按钮
要创建动画自定义单选按钮,我们将使用 transition 属性设置过渡效果。还将使用 transform 属性。在选中任何单选按钮之前,使用 transition-duration、property 和 timing 设置以下样式 −
input[type=checkbox] { appearance: none; -moz-appearance: none; -webkit-appearance: none; border: 3px ridge currentcolor; border-radius: 60px; box-sizing: content-box; color: lightblue; height: 60px; padding: 2px 2px 2px 2px; transition-duration: 280ms; transition-property: border-color, color; transition-timing-function: ease; width: 20px; }
在选中自选框时和选中自选框后,设置以下样式。方向和颜色都将改变 −
input[type=checkbox]:checked { color: lightgreen; } input[type=checkbox]::after { background-color: currentcolor; border-radius: 10px 10px 10px 10px; content: ""; display: block; height: 20px; transform: translateY(0px); transition: transform 300ms ease-in; width: 20px ; } input[type=checkbox]:checked::after { transform: translateY(40px); }
示例
以下示例说明了动画自定义单选按钮 −
<!DOCTYPE html> <html> <head> <style> label { display: flex; align-items: top; } label input { margin: 0px 10px 0px 10px; } input[type=checkbox] { appearance: none; -moz-appearance: none; -webkit-appearance: none; border: 3px ridge currentcolor; border-radius: 60px; box-sizing: content-box; color: lightblue; height: 60px; padding: 2px 2px 2px 2px; transition-duration: 280ms; transition-property: border-color, color; transition-timing-function: ease; width: 20px; } input[type=checkbox]:checked { color: lightgreen; } input[type=checkbox]::after { background-color: currentcolor; border-radius: 10px 10px 10px 10px; content: ""; display: block; height: 20px; transform: translateY(0px); transition: transform 300ms ease-in; width: 20px ; } input[type=checkbox]:checked::after { transform: translateY(40px); } </style> </head> <body> <p> Custom radio button example <label for="crb"> Blue <input id="crb" type="checkbox"/> <br/><br/><br/>Green </label> </p> </body> </html>
广告