Input with both Radio and checkbox characteristics - html

I am looking to design a input field such that it will obey radio buttons characteristics as well as checkbox characteristics.
The requirement is that there are some bars, to which when I click, a text "hello" will be shown adjacent to that bar. and when I click on another bar, the "hello" text from the previous bar should hide and the clicked bar's "hello" text should be visible.
This is happening in this fiddle
label {
display: block;
width: 100px;
height: 20px;
background-color: cornflowerblue;
position: relative;
margin-top: 10px;
cursor: pointer;
}
span {
display: none;
}
input[type="radio"] {
visibility: hidden;
position: absolute;
pointer-events: none;
}
:checked + span {
display: block;
position: absolute;
left: 100%;
}
But what I want is each bar should act as toggle button to show/hide the "hello" text. I can achieve this if I use checkboxes instead of radio button but I will lose the behavior which I achieved above using radio buttons.
I am looking for a pure css solution.
Thanks in advance.
EDIT:
This is what I want. Fiddle
But using only css.

If you can afford to edit html and really can't afford to use any javascript at all you can do it by using <input type='reset' />, but this is a hacky way to handle such functionality - javascript should always be your first choice for such tasks.
label {
display: block;
width: 100px;
height: 20px;
background-color: cornflowerblue;
position: relative;
margin-top: 10px;
cursor: pointer;
}
span {
display: none;
}
input[type="radio"] {
visibility: hidden;
position: absolute;
pointer-events: none;
}
input[type="reset"] {
position: absolute;
border:0;padding:0;margin:0;background:transparent;
width:100%;
height:100%;
display: none;
}
:checked + span {
display: block;
position: absolute;
left: 100%;
}
:checked + span + input[type="reset"] {
display: block;
opacity:0;
}
<form>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
<label>
<input type="radio" name="test"></input> <span>Hello</span>
<input type='reset' />
</label>
</form>

Related

How to overlap the input with div?

I am trying to make custom radio button and I want to overlap the <input type="radio" into div.
input[type="radio"]:checked+div {
color: #fff;
background-color: rgb(13, 50, 218);
}
<input type="radio" name="favorite_pet" value="Parrot">
<div>Parrot</div><br>
<input type="radio" name="favorite_pet" value="Dog">
<div>Dog</div><br>
The main goal is to select by text div instead of radio button.
Current Layout:
Expected Layout:
How can I select the div instead of radio button with overlapping?
You should use a label with for and hide the radio button
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
color: #fff;
background-color: rgb(13, 50, 218);
}
<input type="radio" name="favorite_pet" value="Parrot" id="rb1">
<label for="rb1">Parrot</label><br>
<input type="radio" name="favorite_pet" value="Dog" id="rb2">
<label for="rb2">Dog</label><br>
If you have issues giving them ids, you can wrap the whole thing in a label and use another element for the text like a span.
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + span {
color: #fff;
background-color: rgb(13, 50, 218);
}
<label>
<input type="radio" name="favorite_pet" value="Parrot">
<span>Parrot</span>
</label><br>
<label>
<input type="radio" name="favorite_pet" value="Dog">
<span>Dog</span>
</label><br>
Wrap the text in label elements:
input[type="radio"]:checked+span {
color: #fff;
background-color: rgb(13, 50, 218);
}
input {
display: none;
}
span {
display: block;
}
<label><input type="radio" name="favorite_pet" value="Parrot">
<span>Parrot</span></label><br>
<label><input type="radio" name="favorite_pet" value="Dog">
<span>Dog</span></label><br>
Then change your divs to spans since the divs can't exist in labels.
Wrap input inside label.
label, span{
font-family: sans-serif;
display: block;
margin-bottom: 10px;
font-size: 18px;
}
input[type="radio"] {
position: absolute;
visibility: hidden;
}
input[type="radio"]:checked + span{
background: blue;
color: #fff;
}
input[type="radio"] + span:before {
content: '\2610';
margin-right: 10px;
font-size: 20px;
}
input[type="radio"]:checked + span:before {
content: '\2611';
}
<label>
<input type="radio" name="group">
<span>Parrot</span>
</label>
<label>
<input type="radio" name="group">
<span>Dog</span>
</label>

Use images instead of radio buttons (HTML/CSS)

I have a div of radio buttons:
<div class="radio-line">
<label>
<img src="img/black.jpeg" />
<input type="radio" name="choice1" value="1"/>
</label>
<label>
<img src="img/blue.jpg" />
<input type="radio" name="choice1" value="2"/>
</label>
</div>
And here's my CSS:
img {
width: 20vw;
height: 20vw;
padding: 2vw;
}
input[type=radio] {
display: none;
}
img:hover {
opacity: 0.4;
cursor: pointer;
}
img:active {
opacity:0.4;
cursor: pointer;
}
input[type=radio]:checked + img {
border: 20px solid rgb(228, 207, 94);
}
When I hover over a square, the square opacity will change, but nothing happens when I click on the square (when it should be "checked", a border should surround it). I've been playing around with different divs and labels, but nothing is working.
img {
width: 20vw;
height: 20vw;
padding: 2vw;
}
input[type=radio] {
display: none;
}
img:hover {
opacity:0.6;
cursor: pointer;
}
img:active {
opacity:0.4;
cursor: pointer;
}
input[type=radio]:checked + img {
border: 20px solid rgb(228, 207, 94);
}
<label>
<img src="img/black.jpeg" />
<input type="radio" name="choice-1" value="1"/>
</label>
<label>
<img src="img/blue.jpg" />
<input type="radio" name="choice-1" value="1"/>
</label>
You can used the :checked pseudo-class and sibling selectors to achieve the effect.
So first thing is to move into a structure where inputs are siblings of the labels instead of descendants, and include a "for" attribute that links them
img {
width: 20vw;
height: 20vw;
padding: 2vw;
}
input[type=radio] {
display: none;
}
img:hover {
opacity:0.6;
cursor: pointer;
}
img:active {
opacity:0.4;
cursor: pointer;
}
input[type=radio]:checked + label > img {
border: 20px solid rgb(228, 207, 94);
}
<input type="radio" name="choice" id="choose-1" value="1"/>
<label for="choose-1">
<img src="https://placehold.it/200/200/" />
</label>
<input type="radio" name="choice" id="choose-2" value="2"/>
<label for="choose-2">
<img src="https://placehold.it/200/200/" />
</label>
You can use jQuery to set an onClick function on the images. Assign the images a custom class e.g: radioImage and in jQuery:
$( ".radioImage" ).click(function() {
$( this ).css("border", "20px solid rgb(228, 207, 94)");
});
You should set for property of label to the name of input you want.
In this case, it maybe looks like this:
<input type="radio" name="choice-1" id="choice-1" value="1"/>
<label for="choice-1">
<img src="img/black.jpeg" />
</label>
<input type="radio" name="choice-1" id="choice-2" value="1"/>
<label for="choice-2">
<img src="img/blue.jpg" />
</label>

Removing hover state of disabled custom radio button

I have a custom radio button using some radio button icons from Font Awesome. I have a hover state on on .radio-spacer, which wraps the radio button. The radio button may be in a disabled state based on some previous selections.
My problem is I want no hover state when the radio button is disabled. But, my hover styles exist on the wrapper and not the button itself and don't reflect whether the button is disabled or not. I can't use, for example, .radio-spacer:disabled as it's just the div wrapping the button and has no disabled state. Any ideas?
.radio-spacer {
float: left;
border: 1px solid #ccc;
border-radius: 5px;
padding: 5px;
width: 103px;
}
.radio-spacer label {
margin-bottom: 0;
}
.radio-spacer:hover {
background-color: lightblue;
}
.radio-spacer:hover span {
color: #fff;
}
input[type="radio"] {
display: none;
}
input[type="radio"]~span {
cursor: pointer;
}
input[type="radio"]:disabled~span {
cursor: not-allowed;
color: #aeaeae !important;
opacity: .65;
}
input[type="radio"]+.fa-dot-circle-o:before {
content: "\f10c";
}
input[type="radio"]:checked+.fa-dot-circle-o:before {
content: "\f192";
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="radio-spacer">
<label>
<input type="radio" class="hide select-plan" name="select-plan" />
<span class="fa fa-fw fa-dot-circle-o"></span><span class="radio-label">Select</span>
</label>
</div>
<div class="radio-spacer">
<label>
<input type="radio" class="hide select-plan" name="select-plan" disabled />
<span class="fa fa-fw fa-dot-circle-o"></span><span class="radio-label">Disabled</span>
</label>
</div>
You can make the hover style with an empty <span> element and place it after the radio/checkbox or use an :after pseudo element, example below. You will need javascripts otherwise to check the radio/checkbox status and add a class to the container then style it.
.radio-spacer {
display: inline-block;
vertical-align: top;
padding: 4px 8px;
position: relative;
}
.select-plan:after {
content: "";
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
z-index: -1;
}
.select-plan:disabled ~ .radio-label {
color: silver;
cursor: not-allowed;
}
.select-plan:not([disabled]):hover:after {
background: lightblue;
}
<div class="radio-spacer">
<label>
<input type="radio" class="hide select-plan" name="select-plan" />
<span class="radio-label">Select</span>
</label>
</div>
<div class="radio-spacer">
<label>
<input type="radio" class="hide select-plan" name="select-plan" disabled />
<span class="radio-label">Disabled</span>
</label>
</div>

Style bootsrap radio buttons via images

I am attempting to style some radio buttons using images, via background-color property.
Using bootstrap, the code sequence looks somewhat like this:
<div id="something">
<div class="radio">
<label>
<input id="some_id" value="Yes" type="radio">
Yes
</label>
</div>
<div class="radio">
<label>
<input id="some_id2" checked="" value="No" type="radio">
No
</label>
</div>
</div>
What I want to do is edit it's icons for checked, unchecked, focused, checked-focused and disabled states. I don't want the text to have a background, just the icon in front of it.
css:
.radio label {
line-height: 40px;
min-height: 40px;
}
.radio label{
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/1024px-Google_%22G%22_Logo.svg.png");
background-size: 40px 40px;
background-repeat: no-repeat;
background-position: center;
width: 40px;
height: 40px;
}
.radio label:checked{
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Apple_logo_black.svg/170px-Apple_logo_black.svg.png");
}
Fiddle: https://jsfiddle.net/qL2wab24/
You can use CSS3 radio button styles,
radio{
position: reletive;
overflow: hidden;}
.radio label {
line-height: 40px;
min-height: 40px;
}
.radio label{
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/1024px-Google_%22G%22_Logo.svg.png");
background-size: 40px 40px;
background-repeat: no-repeat;
width: 40px;
height: 40px;
display: block;
padding: 0 0 0 50px;
}
.radio input[type="radio"]:checked~label{
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Apple_logo_black.svg/170px-Apple_logo_black.svg.png");
}
.radio label:focus{
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Apple_logo_black.svg/170px-Apple_logo_black.svg.png");
}
.radio input[type="radio"]:checked~label:focus{
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/1024px-Google_%22G%22_Logo.svg.png");
}
.radio input[type="radio"]{position: absolute;top: 0; left: 0; z-index: -1; opacity: 0; visibility: hidden;}
<div id="something">
<div class="radio">
<input id="some_id" value="Yes" type="radio" name="name">
<label for="some_id">
Yes
</label>
</div>
<div class="radio">
<input id="some_id2" checked="" value="No" type="radio" name="name">
<label for="some_id2">
No
</label>
</div>
</div>
Just add a css:
input[type=radio]
{
opacity: 0;
}
I have updated your js fiddle please check:
https://jsfiddle.net/qL2wab24/4/

Styled radio button, needs to link on click while maintaining style

I am using styled radio buttons as a simple nav for something. I just need to know how to have them link to something while maintaining the styles I applied.
In the code below, the 'Home' button is linking to something, but no longer has the properties of a radio button.
How can I keep the 'hover' and 'clicked' styles while having it link?
<!DOCTYPE html>
<html>
<head>
<style>
.navText {
font-family: helvetica, sans-serif;
font-size: 15px;
font-weight: 400;
display: table-cell;
text-align: center;
vertical-align: middle;
color: white;
}
input[type=radio] {
display: none;
}
.overlay {
display: inline-block;
position: relative;
float: left;
width: 100px;
height: 50px;
margin-left: 5px;
display: table;
background-color: grey;
}
input[type=radio]:hover + .overlay {
background-color: red;
}
input[type=radio]:checked + .overlay {
background-color: red;
}
</style>
</head>
<body>
<label><input type="radio" name="nav"><span class="overlay"><h1 class="navText">Home</h1></span></label>
<label><input type="radio" name="nav"><span class="overlay"><h1 class="navText">Postings</h1></span></label>
<label><input type="radio" name="nav"><span class="overlay"><h1 class="navText">About</h1></span></label>
<label><input type="radio" name="nav"><span class="overlay"><h1 class="navText">Contact</h1></span></label>
<label><input type="radio" name="nav"><span class="overlay"><h1 class="navText">Other</h1></span></label>
</body>
</html>
Rather than using an anchor (<a>) tag, set the input's onclick function to change the value of document.location to your desired URL:
<label>
<input type="radio" name="nav" onclick="document.location='#';">
<span class="overlay">
<h1 class="navText">Home</h1>
</span>
</label>
See the following working jsfiddle example: http://jsfiddle.net/fry3gyp7/