How to put text inside radio button? - css

I am trying to put a letter inside a radio button (with Materialize framework) like the A and B in this image.
How do I do this?

Here is the customize radio button.
see the comments given in css.
input[type="radio"] {
width: 30px;
height: 30px;
border-radius: 15px;
border: 2px solid #1FBED6;
background-color: white;
-webkit-appearance: none; /*to disable the default appearance of radio button*/
-moz-appearance: none;
}
input[type="radio"]:focus { /*no need, if you don't disable default appearance*/
outline: none; /*to remove the square border on focus*/
}
input[type="radio"]:checked { /*no need, if you don't disable default appearance*/
background-color: #1FBED6;
}
input[type="radio"]:checked ~ span:first-of-type {
color: white;
}
label span:first-of-type {
position: relative;
left: -27px;
font-size: 15px;
color: #1FBED6;
}
label span {
position: relative;
top: -12px;
}
<label>
<input type="radio" name="options"/>
<span>A</span><span>Option A</span>
</label>
<br/>
<label>
<input type="radio" name="options"/>
<span>B</span><span>Option B</span>
</label>
<br/>
<label>
<input type="radio" name="options"/>
<span>C</span><span>Option C</span>
</label>
<br/>
<label>
<input type="radio" name="options"/>
<span>D</span><span>Option D</span>
</label>

You cannot do that. But instead, you can make something similar using CSS:
label {display: block; padding: 5px; position: relative; padding-left: 20px;}
label input {display: none;}
label span {border: 1px solid #ccc; width: 15px; height: 15px; position: absolute; overflow: hidden; line-height: 1; text-align: center; border-radius: 100%; font-size: 10pt; left: 0; top: 50%; margin-top: -7.5px;}
input:checked + span {background: #ccf; border-color: #ccf;}
<h3>Select One</h3>
<label><input type="radio" name="select" /><span>a</span> Item 1</label>
<label><input type="radio" name="select" /><span>b</span> Item 2</label>
Preview

Related

Style radio buttons once checked/selected

I'm trying to change the colour of radio buttons and button label when the radio button has been selected. Have tried this.
input[type='radio']:checked {
background: yellow !important;
}
<label
className='tag-item'
key={tag}
htmlFor={`filter-${tag}`}
clickable='true'>
<span>{tag}</span>
<input
type='radio'
name='tag'
id={`filter-${tag}`}
value={tag}
checked={tag === currentTag}
onChange={e => setCurrentTag(e.target.value)}
className='tag-radio'
/>
</label>
You can't change the default Radio button's color.
But you can do this with some trick.
input[type=radio]{
display: none;
}
.fake-radio{
position: relative;
display: inline-block;
width: 12px;
height: 12px;
border: 1px solid gray;
border-radius: 100%;
}
.fake-radio::before{
content: '';
display: block;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: 6px;
height: 6px;
background-color: gray;
border-radius: 100%;
}
input[type=radio]:checked + .fake-radio{
border-color: red;
}
input[type=radio]:checked + .fake-radio::before{
background-color: red;
}
<label>
<span>Tag</span>
<input type="radio" />
<span class="fake-radio"></span>
</label>

Styling form checkboxes with font awesome, <input> inside of <label>

After following this question and getting the idea of styling which is shown below, I tried to replace the default checkboxes and style them.
Currently, the problem is that the checkboxes don't tick and I don't understand why.
Link to JsFiddle:
Fiddle Link Here
HTML
<div class="col-md-6 checkboxes">
<label for="id_tags_0"><input type="checkbox" name="tags" value="4" id="id_tags_0">
Tag 1</label>
<label for="id_tags_1"><input type="checkbox" name="tags" value="1" id="id_tags_1">
Tag 2</label>
</div>
CSS
.checkboxes label {
/*display: inline-block;*/
cursor: pointer;
position: relative;
}
/*Hide default checkbox*/
.checkboxes input[type=checkbox] { display: none; }
/*Show an empty box before the our label by default*/
.checkboxes label:before {
content: "";
display: inline-block;
margin-right: 10px;
position: absolute;
left: 0;
top: 3px;
background-color: #fff;
border: 2px solid #d0d0d0;
border-radius: 4px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
transition: all 0.25s;
font-family: "FontAwesome";
color: #fff;
text-align: center;
}
/*When checkbox input is checked, add a tick to our box */
.checkboxes label>input[type=checkbox]:checked:before {
/* Content: https://fontawesome.com/icons/check?style=solid */
content: "\f00c";
background-color: #66676b;
border: 2px solid #66676b;
}
.checkboxes label>input[type=checkbox]:checked {
/* Content: https://fontawesome.com/icons/check?style=solid */
content: "\f00c";
background-color: #66676b;
border: 2px solid #66676b;
}
You are trying to use parent selector, which is not possible through css.
Instead you can try this.
add a span after the checkbox and style it.
.checkboxes label {
/*display: inline-block;*/
cursor: pointer;
position: relative;
padding-left: 28px;
margin-right: 20px;
margin-bottom: 0;
line-height: 24px;
font-size: 16px;
}
/*Hide default checkbox*/
.checkboxes input[type=checkbox] { display: none; }
/*Show an empty box before the our label by default*/
.checkboxes label span:before {
content: "";
display: inline-block;
width: 19px;
height: 19px;
margin-right: 10px;
position: absolute;
left: 0;
top: 3px;
background-color: #fff;
border: 2px solid #d0d0d0;
border-radius: 4px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
transition: all 0.25s;
font-family: "FontAwesome";
font-size: 12px;
color: #fff;
text-align: center;
line-height: 15px;
}
/*When checkbox input is checked, add a tick to our box */
.checkboxes label>input[type=checkbox]:checked + span:before {
/* Content: https://fontawesome.com/icons/check?style=solid */
content: "\f00c";
background-color: #66676b;
border: 2px solid #66676b;
}
.checkboxes label>input[type=checkbox]:checked {
/* Content: https://fontawesome.com/icons/check?style=solid */
content: "\f00c";
background-color: #66676b;
border: 2px solid #66676b;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://use.fontawesome.com/releases/v5.0.9/css/all.css" rel="stylesheet"/>
<div class="col-md-6 checkboxes">
<label for="id_tags_0">
<input type="checkbox" name="tags" value="4" id="id_tags_0">
<span></span>
Tag 1</label>
<label for="id_tags_1"><input type="checkbox" name="tags" value="1" id="id_tags_1">
<span></span>
Tag 2</label>
<label for="id_tags_2"><input type="checkbox" name="tags" value="2" id="id_tags_2">
<span></span>
Tag 3</label>
<label for="id_tags_3"><input type="checkbox" name="tags" value="3" id="id_tags_3">
<span></span>
Tag 4</label>
<label for="id_tags_4"><input type="checkbox" name="tags" value="5" id="id_tags_4">
<span></span>
Tag 5</label>
</div>

Radio buttons as tabs

I want to display radio buttons as tabs. Following this codepen, I've attempted this in my project. But when I click on pseudo tab, nothing happens. It doesn't get checked.
How can I achieve the effect?
input.tgl-radio-tab-child {
position: absolute;
left: -99999em;
top: -99999em;
opacity: 1;
z-index: 1;
}
input.tgl-radio-tab-child+label {
cursor: pointer;
float: left;
border: 1px solid #aaa;
margin-right: -1px;
padding: .5em 1em;
position: relative;
}
input.tgl-radio-tab-child+label:hover {
background-color: #eee;
}
[type=radio]:checked+label {
background-color: #c30;
z-index: 1;
}
<label for="abc">ABC<span style="color: red;">*</span></label>
<div class="form-check">
<div class="tgl-radio-tabs">
<input type="radio" class="form-check-input tgl-radio-tab-child" name="abcorigin"><label class="radio-inline">ABCDEFGHIJKL</label>
<input type="radio" class="form-check-input tgl-radio-tab-child" name="abcorigin"><label class="radio-inline">MNOPQRSTUVWXYZ</label>
</div>
</div>
You need to bind your labels to your inputs via the for attribute which should reference the respective input's ID:
input.tgl-radio-tab-child {
position: absolute;
left: -99999em;
top: -99999em;
opacity: 1;
z-index: 1;
}
input.tgl-radio-tab-child+label {
cursor: pointer;
float: left;
border: 1px solid #aaa;
margin-right: -1px;
padding: .5em 1em;
position: relative;
}
input.tgl-radio-tab-child+label:hover {
background-color: #eee;
}
[type=radio]:checked+label {
background-color: #c30;
z-index: 1;
}
<label for="abc">ABC<span style="color: red;">*</span></label>
<div class="form-check">
<div class="tgl-radio-tabs">
<input id="x" type="radio" class="form-check-input tgl-radio-tab-child" name="abcorigin"><label for="x" class="radio-inline">ABCDEFGHIJKL</label>
<input id="y" type="radio" class="form-check-input tgl-radio-tab-child" name="abcorigin"><label for="y" class="radio-inline">MNOPQRSTUVWXYZ</label>
</div>
</div>

customize radio button using css

I've been trying to customize the default radio button to look something like on/off button.
I've already created one using 2 images (using javascript) for an on and off state, but there's no animation for transition between the two states.
Is there a way to achieve this using pure css(+ animation)?
I need buttons like these-
Here you go you can also do this with checkbox
#mc, #mc2, #mc3 {
display: none;
}
.switch {
background: gray;
width: 40px;
height: 20px;
border-radius: 10px;
display: block;
position: relative;
margin: 10px;
}
.switch:hover {
cursor: pointer
}
.switch:after {
content: "";
height: 18px;
width: 18px;
border-radius: 9px;
display: block;
position: absolute;
top: 1px;
left: 1px;
background: lightblue;
transition: ease-out 0.5s;
}
input[type=radio]:checked + label:after,
input[type=checkbox]:checked + label:after{
left: calc(100% - 19px);
background: lightgreen;
}
<p>Radio</p>
<input type="radio" id="mc" name="Switch" />
<label class="switch" for="mc"></label>
<input type="radio" id="mc2" name="Switch" />
<label class="switch" for="mc2"></label>
<p>Checkbox</p>
<input type="checkbox" id="mc3" name="Switch" />
<label class="switch" for="mc3"></label>
To get transition between the two state (on/off), style the :checked pseudo class.
The :checked pseudo class matches every checked <input> element (radio, checkbox and option).
/* The container */
.switch {
position: relative;
display: inline-block;
vertical-align: middle;
margin-right: .5em;
width: 46px;
height: 24px;
}
/* Hide the browser's default radio */
.switch input[type=radio] {
position: absolute;
opacity: 0;
}
/* Create a custom switch */
.switch .slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius: 34px;
border: 1px solid #daa2e6;
transition: .2s;
}
/* Create the indicator/dot */
.switch .slider::before {
position: absolute;
top: 2px;
left: 4px;
content: "";
height: 18px;
width: 18px;
border-radius: 50%;
background-color: #bababa;
transition: .2s;
}
/* When the switch is checked, add a blue background */
.switch input[type=radio]:checked + .slider {
background-color: #007bff;
}
/* When the switch is checked, change position of the indicator/dot */
.switch input[type=radio]:checked + .slider::before {
background-color: #fff;
-webkit-transform: translateX(18px);
-ms-transform: translateX(18px);
transform: translateX(18px);
}
/* When the switch gets focus, add a shadow */
.switch input[type=radio]:focus + .slider {
box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
border-color: #80bdff;
}
<p>
<label class="switch">
<input type="radio" name="country">
<span class="slider"></span>
</label> United States
</p>
<p>
<label class="switch">
<input type="radio" name="country">
<span class="slider"></span>
</label> Canada
</p>
<p>
<label class="switch">
<input type="radio" name="country" checked>
<span class="slider"></span>
</label> United Kingdom
</p>
<p>
<label class="switch">
<input type="radio" name="country">
<span class="slider"></span>
</label> India
</p>

css for checkbox while hover

I wanna change color for each checkbox while hover.I have four checkbox.I wanna change the color, If I hover the 'all' checkbox it shows red,blue color for 'cold',orange for 'warm' and green for 'Active'.
#ck-button {
margin: 0px;
background-color: #EFEFEF;
border-radius: 4px;
border: 1px solid #D0D0D0;
overflow: auto;
float: left;
}
#ck-button label {
float: left;
width: 4.0em;
}
#ck-button label span {
text-align: center;
padding: 3px 0px;
display: block;
border-radius: 4px;
}
#ck-button label input {
position: absolute;
top: -20px;
}
input:checked +span {
background-color: #911;
color: #fff;
}
#ck-button input:hover #all + span {
background-color: #efE0E0;
}
#ck-button input:hover #o1 + span {
background-color: blue;
}
#ck-button input:hover #o2 + span {
background-color: orange;
}
#ck-button input:hover #o3 + span {
background-color: green;
}
<div id="ck-button">
<label>
<input type="radio" name="sta_choice" id=all value="All" checked><span>All</span>
</label>
</div>
<div id="ck-button">
<label>
<input type="radio" name="sta_choice" id="o1" value="Cold" onclick=handleClick1(this.val);><span class="o1">Cold</span>
</label>
</div>
<div id="ck-button">
<label>
<input type="radio" name="sta_choice" id="o2" value="Warm" onclick="handleClick1(this.val);"><span>Warm</span>
</label>
</div>
<div id="ck-button">
<label>
<input type="radio" name="sta_choice" id="o3" value="Active" onclick="handleClick1(this.val);"><span>Active</span>
</label>
</div>
In the last four lines use label:hover instead of input:hover. Input is positioned to top and isn't hovered (is outside the label).
#ck-button label:hover #all + span {
background-color:#efE0E0;
}
#ck-button label:hover #o1 + span {
background-color:blue;
}
#ck-button label:hover #o2 + span {
background-color:orange;
}
#ck-button label:hover #o3 + span {
background-color:green;
}
http://jsfiddle.net/3q4uuvum/3/
even as an option
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
ul{
text-align: center;
width: 300px;
margin: 25px auto;
}
ul > li{
display: inline-block;
*display: inline;
zoom: 1;
vertical-align: top;
width: 100px;
height: 100px;
border: 1px solid #ccc;
position: relative;
}
input{
display: none;
}
label{
display: block;
width: 100px;
height: 100px;
}
label:hover{
background: #f7f7f7;
}
input:checked ~ label{
position: absolute; top: 0; left: 0;
width: 100%;
height: 100%;
background: url(http://html5doctor.com/wp-content/themes/html5doctor2/images/HTML5_Badge_64.png) no-repeat center center;
border: 1px solid #000;
}
<ul>
<li>
<input type="checkbox" id="c1" name="checkbox" />
<label for="c1"></label>
<li>
<input type="checkbox" id="c2" name="checkbox" />
<label for="c2"></label>
<li>
<input type="checkbox" id="c3" name="checkbox" />
<label for="c3"></label>
<li>
<input type="checkbox" id="c4" name="checkbox" checked />
<label for="c4"></label>
<li>
<input type="checkbox" id="c5" name="checkbox" />
<label for="c5"></label>
<li>
<input type="checkbox" id="c6" name="checkbox" />
<label for="c6"></label>
<li>
<input type="checkbox" id="c7" name="checkbox" />
<label for="c7"></label>
<li>
<input type="checkbox" id="c8" name="checkbox" />
<label for="c8"></label>
<li>
<input type="checkbox" id="c9" name="checkbox" />
<label for="c9"></label>
</ul>
I couldn't find online how to display different colors for hover depending if the box is checked, but after tirelessly tweaking the code I figured it out.
.ck-button-class:hover input:checked + span {
background-color: green;
}
This is assuming you assigned the class to these buttons, <div id="ck-button" class="ck-button-class">.

Resources