简体   繁体   中英

How to create radio button as buttons?

How to create regular radio buttons to like normal buttons styled with css?

Here are my radio buttons:

<div class="radio-toolbar">

    <input type="radio" id="radio1" name="radios" value="all" checked>
    <label for="radio1">Radio1</label>

    <input type="radio" id="radio2" name="radios"value="false">
    <label for="radio2">Radio2</label>

    <input type="radio" id="radio3" name="radios" value="true">
    <label for="radio3">Radio3</label>
</div>

And my css:

.radio-toolbar {width:410px;}

.radio-toolbar input[type="radio"] {
        display:none;
}

.radio-toolbar label {
        display:inline-block;
        background:#FFF;
        font-family:"Arial Black", sans-serif;
        font-size:12px;
        color:#666666;
        width:106px;
        padding-left:4px;
}

.radio-toolbar [type="radio"]:checked + label {
        background:url("../images/radiochecked.png") no-repeat;
        color:#FFF;
}
.radio1 [type="radio"]:checked + label {
        background:url("../images/radio1.png") no-repeat;
        color:#FFF;
}
.radio2 input[type="radio"]:checked + label {
        background:url("../images/radio2.png") no-repeat;
        color:#FFF;
}



.radio-toolbar label:hover {background-color:#bbb;
background:url("../images/radiohover.png") no-repeat;
color:#FFF;
}
.radio2 label:hover {background-color:#bbb;
}

It involves hiding the radio button element and putting in a different element instead (at runtime, to maintain backward compatibility with non-JS-enabled browsers), then echoing changes to the new element to the hidden radio button's state.

As you're already using jQuery, you might consider jQuery UI, which has exactly this functionality .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM