Styling radio input without label - html

I had done with radio buttons with label customization. However, type="radio" can't show and work normally once without labels.
Is it possible to apply pure CSS style to radio input without labels or any workaround for this issue? If so, how can I do this? lots of resources are customizing radio buttons with labels to be after the input.
I have the following markup:
[type="radio"]:checked,
[type="radio"]:not(:checked) {
display: none;
}
[type="radio"]:checked+label,
[type="radio"]:not(:checked)+label {
position: relative;
padding-left: 20px;
cursor: pointer;
line-height: 16px;
display: inline-block;
color: red;
}
/* radio outer circle */
[type="radio"]:checked+label:before,
[type="radio"]:not(:checked)+label:before {
content: "";
position: absolute;
left: 0;
top: 0;
width: 16px;
height: 16px;
border: 1px solid green;
border-radius: 100%;
background: #fff;
}
/* radio inner circle */
[type="radio"]:checked+label:after,
[type="radio"]:not(:checked)+label:after {
content: "";
width: 10px;
height: 10px;
background: orange;
position: absolute;
top: 4px;
left: 4px;
border-radius: 100%;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
[type="radio"]:not(:checked)+label:after {
opacity: 0;
-webkit-transform: scale(0);
transform: scale(0);
}
[type="radio"]:checked+label:after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}
<h2>radio button <i>with</i> label</h2>
<form>
<input type="radio" id="kaios" value="kaios" name="radio-group" checked>
<label for="kaios">KaiOS</label>
<input type="radio" id="ios" value="ios" name="radio-group">
<label for="ios">iOS</label>
<input type="radio" id="android" value="android" name="radio-group">
<label for="android">Android</label>
</form>
<hr />
<h2>radio button <i>without</i> label</h2>
<form>
<input type="radio" name="options" value="KaiOS">KaiOS<br />
<input type="radio" name="options" value="Firefox">Firefox<br />
</form>

It is a problem that one can't 'go back' in CSS so we can't style the input because it has a following sibling label element.
The only things I can think of therefore require some addition. The most straightforward workaround would seem to be at the time of adding the label to the HTML just add style="display:none" (or a class) to its input element.
.dontshow[type="radio"]:checked,
.dontshow[type="radio"]:not(:checked) {
display: none;
}
[type="radio"]:checked+label,
[type="radio"]:not(:checked)+label {
position: relative;
padding-left: 20px;
cursor: pointer;
line-height: 16px;
display: inline-block;
color: red;
}
/* radio outer circle */
[type="radio"]:checked+label:before,
[type="radio"]:not(:checked)+label:before {
content: "";
position: absolute;
left: 0;
top: 0;
width: 16px;
height: 16px;
border: 1px solid green;
border-radius: 100%;
background: #fff;
}
/* radio inner circle */
[type="radio"]:checked+label:after,
[type="radio"]:not(:checked)+label:after {
content: "";
width: 10px;
height: 10px;
background: orange;
position: absolute;
top: 4px;
left: 4px;
border-radius: 100%;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
[type="radio"]:not(:checked)+label:after {
opacity: 0;
-webkit-transform: scale(0);
transform: scale(0);
}
[type="radio"]:checked+label:after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}
<h2>radio button <i>with</i> label</h2>
<form>
<input type="radio" id="kaios" value="kaios" name="radio-group" class="dontshow" checked>
<label for="kaios">KaiOS</label>
<input type="radio" id="ios" value="ios" name="radio-group" class="dontshow">
<label for="ios">iOS</label>
<input type="radio" id="android" value="android" name="radio-group" class="dontshow">
<label for="android">Android</label>
</form>
<hr />
<h2>radio button <i>without</i> label</h2>
<form>
<input type="radio" name="options" value="KaiOS">KaiOS<br />
<input type="radio" name="options" value="Firefox">Firefox<br />
</form>
Other things you could do if you absolutely know that the name of that group all need special styling is to select on that name. This wouldn't require additions but is of course more difficult from the ongoing maintenance point of view.

Related

CSS prevent rotation transform from affecting :before elements

I'm trying to have the checkboxes appear below their slanted "span" label. However, the transform applied to the "span" is also applying to the custom checkbox created with the ":before" selector. How do you prevent the 45deg rotation to the custom checkbox, and have it appear directly below it's label?
.slanted_chkbx {
margin-top: 25px;
}
.slanted_check {
margin-left: 5px;
margin-right: 15px;
display: inline-block;
position: relative;
}
.slanted_check span {
position: absolute;
top: -20px;
left: .7em;
transform-origin: bottom left;
transform: rotate(-45deg);
}
.custom_check {
display: none;
}
.custom_check + span:before {
content: '';
display: block;
cursor: pointer;
width: 10px;
height: 10px;
left: 0;
top: 0;
position: absolute;
border: 2px solid #111111;
-webkit-transition: all .2s;
transition: all .2s;
}
.custom_check:checked + span:before {
width: 5px;
top: -2px;
left: 2px;
border-radius: 0;
opacity: 1;
border-top-color: transparent;
border-left-color: transparent;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
<div class="slanted_chkbx">
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_one" value="option_one">
<span>One</span>
</label>
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_two" value="option_two">
<span>Two</span>
</label>
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_three" value="option_three">
<span>Three</span>
</label>
</div>
You need to place the reverse transform on the base state of the pseudo-element, not on the hover state.
.slanted_chkbx {
margin-top: 25px;
}
.slanted_check {
margin-left: 15px;
margin-right: 15px;
display: inline-block;
position: relative;
}
.slanted_check span {
margin-right: -25px;
display: inline-block;
transform-origin: bottom left;
transform: rotate(-45deg);
}
.custom_check {
display: none;
}
.custom_check+span:before {
content: '';
display: block;
cursor: pointer;
width: 10px;
height: 10px;
left: -15px;
bottom: -10px;
position: absolute;
border: 2px solid #111111;
-webkit-transition: all .2s;
transition: all .2s;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.custom_check:checked+span:before {
width: 5px;
border-radius: 0;
opacity: 1;
border-top-color: transparent;
border-left-color: transparent;
}
<div class="slanted_chkbx">
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_one" value="option_one">
<span>One</span>
</label>
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_two" value="option_two">
<span>Two</span>
</label>
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_three" value="option_three">
<span>Three</span>
</label>
</div>
I'm not entirely sure what effect you are going for in the checked state so I have not adjusted that.

How can I restyle inputs of type radio in css?

I want to create my own radio buttons, but it seems I cant restyle them.
I got the following html:
.segment {
all: unset;
position: relative;
border: solid 1px blue;
height: 2rem;
width: 3rem;
-webkit-tap-highlight-color: transparent;
}
<div>
<input id="segemnt0" class="segment" type="radio" name="segment">
<input id="segemnt1" class="segment" type="radio" name="segment">
<input id="segemnt2" class="segment" type="radio" name="segment">
</div>
This isn't working. Nothing displays at all. If I remove the "all: unset;" it just displays the normal styled checkbox without borders etc...
You can customise radio buttons adding <label> tag and CSS
Here is the fiddle this might help you:
[type="radio"]:checked,
[type="radio"]:not(:checked) {
position: absolute;
left: -9999px;
}
[type="radio"]:checked+label,
[type="radio"]:not(:checked)+label {
position: relative;
padding-left: 28px;
cursor: pointer;
line-height: 20px;
display: inline-block;
color: #666;
}
[type="radio"]:checked+label:before,
[type="radio"]:not(:checked)+label:before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 18px;
height: 18px;
border: 1px solid #ddd;
border-radius: 100%;
background: #fff;
}
[type="radio"]:checked+label:after,
[type="radio"]:not(:checked)+label:after {
content: '';
width: 12px;
height: 12px;
background: #F87DA9;
position: absolute;
top: 4px;
left: 4px;
border-radius: 100%;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
[type="radio"]:not(:checked)+label:after {
opacity: 0;
-webkit-transform: scale(0);
transform: scale(0);
}
[type="radio"]:checked+label:after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}
<input id="segemnt0" class="segment" type="radio" name="segment">
<label for="segemnt0">one</label>
<input id="segemnt1" class="segment" type="radio" name="segment">
<label for="segemnt1">two</label>
<input id="segemnt2" class="segment" type="radio" name="segment">
<label for="segemnt2">three</label>
Try the sample with label. You need to add label in your code to style radio.
input[type=radio] {
display: none;
}
input[type=radio] + label:before {
content: '';
display: inline-block;
border: 2px solid #0F81D5;
height: 12px;
width: 12px;
border-radius: 50%;
margin: 2px 5px 0 0px
}
input[type=radio]:checked + label:before {
border-width: 5px;
height: 6px;
width: 6px;
}
<div>
<input type="radio" name="radio" id="radio1" />
<label for="radio1">radio1</label>
</div>
<div>
<input type="radio" name="radio" id="radio2" />
<label for="radio2">radio2</label>
</div>
<div>
<input type="radio" name="radio" id="radio3" />
<label for="radio3">radio3</label>
</div>

How to style radio inputs both in checked and not checked state with different background?

I have multiple color select option, I want to highlight whatever they are selected
Here is my first image ,
Here I want to convert this to this
Thanks for time and suggestions
You can change the style of radio put likewise. For the checked state, use
[input-selector]:checked + label:after and for the unchecked state, use [input-selector]:not(:checked) + label:before.
[type="radio"]:checked,
[type="radio"]:not(:checked) {
position: absolute;
left: -9999px;
}
[type="radio"]:checked+label,
[type="radio"]:not(:checked)+label {
position: relative;
padding-left: 28px;
cursor: pointer;
line-height: 20px;
display: inline-block;
color: #666;
}
[type="radio"]:checked+label:before,
[type="radio"]:not(:checked)+label:before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 18px;
height: 18px;
border: 1px solid #ddd;
border-radius: 100%;
background: #fff;
}
[type="radio"]:checked+label:after,
[type="radio"]:not(:checked)+label:after {
content: '';
width: 12px;
height: 12px;
position: absolute;
top: 3px;
left: 3px;
border-radius: 100%;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
[type="radio"]:not(:checked)+label:after {
-webkit-transform: scale(0);
transform: scale(0);
}
[type="radio"]:checked+label:after {
-webkit-transform: scale(1);
transform: scale(1);
}
/* To change the background color, only customize the code below*/
.red-input:not(:checked)+label:before {
background: red;
}
.red-input:checked+label:after {
background: black;
}
.green-input:not(:checked)+label:before {
background: green;
}
.green-input:checked+label:after {
background: blue;
}
.black-input:not(:checked)+label:before {
background: black;
}
.black-input:checked+label:after {
background: silver;
}
.silver-input:not(:checked)+label:before {
background: silver
}
.silver-input:checked+label:after {
background: #E6B5A3;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
<form class="form-inline m-5">
<div class="form-group">
<div class="mx-2">
<input type="radio" id="test1" name="radio-group" class="red-input">
<label for="test1">Peach</label>
</div>
<div class="mx-2">
<input type="radio" id="test2" name="radio-group" class="green-input">
<label for="test2">Orange</label>
</div>
<div class="mx-2">
<input type="radio" id="test3" name="radio-group" class="black-input">
<label for="test3">Mango</label>
</div>
<div class="mx-2">
<input type="radio" id="test4" name="radio-group" class="silver-input">
<label for="test4">Lime</label>
</div>
</div>
</form>
Check this codepen
Update
To set the default background of the inputs, use [input-selector]:not(:checked)+label:before.
.red-input:not(:checked)+label:before {
background: red;
}

different radio style inside a form

I have a long form. Inside there is a color picking radio button
How can I make a css exception for this radio without effecting all other radio's?
input[type="radio"] {
display: none;
}
input[type="radio"]:checked+label span {
transform: scale(1.25);
}
input[type="radio"]:checked+label .red {
border: 2px solid #711313;
}
label {
display: inline-block;
width: 25px;
height: 25px;
margin-right: 10px;
cursor: pointer;
}
label:hover span {
transform: scale(1.25);
}
label span {
display: block;
width: 100%;
height: 100%;
transition: transform .2s ease-in-out;
}
label span.red {
background: #DB2828;
}
label span.orange {
background: #F2711C;
}
<div class="radio">
<input type="radio" name="color" id="red" value="red" />
<label for="red"><span class="red"></span></label>
<input type="radio" name="color" id="green" />
<label for="green"><span class="green"></span></label>
<input type="radio" name="color" id="yellow" />
<label for="yellow"><span class="yellow"></span></label>
</div>
You could wrap that entity in a container and specify the class for it.
Below is a sample code from w3schools.
<html>
<style>
/* The container */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196F3;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
</style>
<body>
<h1>Custom Checkboxes</h1>
<label class="container">One
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<label>Two
<input type="checkbox">
<span class="checkmark"></span>
</label><br/>
<label>Three
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="checkbox">
<span class="checkmark"></span>
</label>
</body>
</html>
Just add a class called exemption and then allow the CSS to be applied only if the class is present at the top most level, thus your other radio buttons are not affected. I have made the radio button color picker work, please let me know what's missing in this!
.exemption input[type="radio"] {
display: none;
}
.exemption input[type="radio"]:checked+label span {
transform: scale(1.25);
}
.exemption input[type="radio"]:checked+label .red {
border: 2px solid #711313;
}
.exemption input[type="radio"]:checked+label .yellow {
border: 2px solid darkyellow;
}
.exemption input[type="radio"]:checked+label .red {
border: 2px solid darkred;
}
.exemption label {
display: inline-block;
width: 25px;
height: 25px;
margin-right: 10px;
cursor: pointer;
}
.exemption label:hover span {
transform: scale(1.25);
}
.exemption label span {
display: block;
width: 100%;
height: 100%;
transition: transform .2s ease-in-out;
}
.exemption label span.red {
background: #DB2828;
}
.exemption label span.green {
background: green;
}
.exemption label span.yellow {
background: yellow;
}
<fieldset>
<div class="radio exemption">
<input type="radio" name="color" id="red" value="red" />
<label for="red"><span class="red"></span></label>
<input type="radio" name="color" id="green" />
<label for="green"><span class="green"></span></label>
<input type="radio" name="color" id="yellow" />
<label for="yellow"><span class="yellow"></span></label>
</div>
</fieldset>

place a label on top of input tag

How can I place label on top of the input tag, by default it is placed inside of the input tag when I click on it, it jumps to the top of the input tag,
This is my attempt:
input {
font-size: 18px;
padding: 10px 10px 10px 5px;
-webkit-appearance: none;
display: block;
background: #fafafa;
color: #636363;
width: 100%;
border: none;
border-radius: 0;
border-bottom: 1px solid #757575;
}
input:focus {
outline: none;
}
/* Label */
label {
color: #999;
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 5px;
top: 10px;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
/* active */
input:focus ~ label,
input.used ~ label {
top: -20px;
-webkit-transform: scale(.75);
transform: scale(.75);
left: -2px;
/* font-size: 14px; */
color: #000;
}
/* Underline */
.bar {
position: relative;
display: block;
width: 100%;
}
.bar:before,
.bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #4a89dc;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
.bar:before {
left: 50%;
}
.bar:after {
right: 50%;
}
/* active */
input:focus ~ .bar:before,
input:focus ~ .bar:after {
width: 50%;
}
/* Highlight */
.highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
/* active */
input:focus ~ .highlight {
-webkit-animation: inputHighlighter 0.3s ease;
animation: inputHighlighter 0.3s ease;
}
<div class="group">
<input type="text" name="name" placeholder="" readonly>
<span class="highlight">
</span><span class="bar"></span>
<label>FULLNAME</label>
</div>
Please how can I place the label on top of the input tag permanently. At the moment it is placed on the input tag and on clicking to it, it jumps to the top of the tag
you could try something like
<form name="message" method="post">
<section>
<div>
<label for="name">Name</label>
<input id="name" type="text" value="" name="name">
</div>
<div>
<label for="email">Email</label>
<input id="email" type="text" value="" name="email">
</div>
</section>
<section>
<div>
<label for="subject">Subject</label>
<input id="subject" type="text" value="" name="subject">
</div>
<div class="full">
<label for="message">Message</label>
<input id="message" type="text" value="" name="message">
</div>
</section>
</form>