Css - How to Style a Selected Radio Buttons Label

CSS - How to Style a Selected Radio Buttons Label?

.radio-toolbar input[type="radio"] {  display: none;}
.radio-toolbar label { display: inline-block; background-color: #ddd; padding: 4px 11px; font-family: Arial; font-size: 16px; cursor: pointer;}
.radio-toolbar input[type="radio"]:checked+label { background-color: #bbb;}
<div class="radio-toolbar">  <input type="radio" id="radio1" name="radios" value="all" checked>  <label for="radio1">All</label>
<input type="radio" id="radio2" name="radios" value="false"> <label for="radio2">Open</label>
<input type="radio" id="radio3" name="radios" value="true"> <label for="radio3">Archived</label></div>

How to style selected radio button

The CSS rule would be

input[type="radio"]:checked

Unsure what exactly what you want to do but if you were planning on doing some styling on the element which goes after each radio element in your question you'd use the following rule

input[type="radio"]:checked + span

CSS change style a label for selected radio button

Because you set the tag of your question to ExtJS, I give you an ExtJS solution.

Check out the fiddle: https://fiddle.sencha.com/#fiddle/l4i

Register a function on the change event of the radiobutton and you can access the labelEl which you can use the set the css class or directly change the style.

Main Code:

{
xtype : "radio",
fieldLabel : "Radio1",
name : "myRadio",
listeners : {
change : me.onRadioButtonChange
}
}

 

onRadioButtonChange : function(radioField) {
if(!Ext.isEmpty(radioField.labelEl)) {
if(radioField.checked) {
radioField.labelEl.setStyle("font-weight", "bold");
radioField.labelEl.addCls("custom-css-class");
} else {
radioField.labelEl.setStyle("font-weight", "");
radioField.labelEl.removeCls("custom-css-class");
}
}
}

CSS for a checked radio button's label

Since there is no previous selector in CSS, you will have to move the input element before label and use the adjacent sibling(+) selector to select the label when input is :checked.

input[type="radio"]:checked + label {
background: #000;
}

Updated Fiddle

/* Clean up the lists styles */
ul.accordion { list-style: none; margin: 0; padding: 0;}/* Hide the radio buttons */
/* These are what allow us to toggle content panes */
ul.accordion input[type='radio'] { display: none;}/* Give each content pane some styles */
ul.accordion li { background-color: #fff; border-bottom: 1px solid #DDDDDD;}/* Make the main tab look more clickable */
ul.accordion label { background-color: #666666; color: #FFFFFF; display: block; padding: 10px; font-size: 16px; font-weight: 700;}/* Set up the div that will show and hide */
ul.accordion div.content { overflow: hidden; padding: 0 20px; display: none;}/* Show the content boxes when the radio buttons are checked */
ul.accordion input[type='radio']:checked + label + div.content { display: block;}input[type="radio"]:checked + label { background: #000;}body { margin: 0;}
<ul class="accordion">  <li>    <input type="radio" name="a" id="q-1" checked="checked" />    <label for="q-1">Content 1</label>    <div class='content'>      <p>Content 1 blah blah blah blah</p>    </div>  </li>  <li>    <input type="radio" name="a" id="q-2" />    <label for="q-2">Content 2</label>    <div class='content'>      <p>Content 2 blah blah blah blah</p>    </div>  </li></ul>

Change Style of Selected Radio Button Label

Solved

The issue occurred because of the use of the + CSS selector. The 'adjacent' selector will select only the element that is immediately preceded by the former element. In this case the label directly preceded by a checked radio button. My original HTML had the label before the input, in order to use the + selector the label must be after the input.

CSS selector for a checked radio button's label

try the + symbol:
It is Adjacent sibling combinator. It combines two sequences of simple selectors having the same parent and the second one must come IMMEDIATELY after the first.

As such:

input[type="radio"]:checked+label{ font-weight: bold; } 
//a label that immediately follows an input of type radio that is checked

works very nicely for the following markup:

<input id="rad1" type="radio" name="rad"/><label for="rad1">Radio 1</label>
<input id="rad2" type="radio" name="rad"/><label for="rad2">Radio 2</label>

... and it will work for any structure, with or without divs etc as long as the label follows the radio input.

Example:

input[type="radio"]:checked+label { font-weight: bold; }
<input id="rad1" type="radio" name="rad"/><label for="rad1">Radio 1</label><input id="rad2" type="radio" name="rad"/><label for="rad2">Radio 2</label>

CSS: style radio button with label next to it?

If you're open to some HTML adjustments you might find this more flexible:

Make sure to include a focus state for the input to ensure it is accessible for keyboard users.

input[type="radio"] {  position: absolute;  opacity: 0;  cursor: pointer;}
label { position: relative; cursor: pointer; padding-left: 30px; display: inline-block; line-height: 1.6; margin-right: 15px;}
span { position: absolute; left: 0; top: 0; height: 20px; width: 20px; background: #FFF; border: 2px solid black; border-radius: 50%;}
span:after { content: ''; position: absolute; left: 5px; top: 5px; width: 10px; height: 10px; border-radius: 50%; background: black; display: none;}
input[type="radio"]:checked~span:after { display: block;}
input[type="radio"]:focus~span { outline: 2px solid orange;}
<label for="male">Male  <input type="radio" id="male" name="gender" value="M">   <span></span></label>

<label for="female">Female <input type="radio" id="female" name="gender" value="F"> <span></span></label>

How do I style (css) radio buttons and labels?

The first part of your question can be solved with just HTML & CSS; you'll need to use Javascript for the second part.

Getting the Label Near the Radio Button

I'm not sure what you mean by "next to": on the same line and near, or on separate lines? If you want all of the radio buttons on the same line, just use margins to push them apart. If you want each of them on their own line, you have two options (unless you want to venture into float: territory):

  • Use <br />s to split the options apart and some CSS to vertically align them:
<style type='text/css'>
.input input
{
width: 20px;
}
</style>
<div class="input radio">
<fieldset>
<legend>What color is the sky?</legend>
<input type="hidden" name="data[Submit][question]" value="" id="SubmitQuestion" />

<input type="radio" name="data[Submit][question]" id="SubmitQuestion1" value="1" />
<label for="SubmitQuestion1">A strange radient green.</label>
<br />
<input type="radio" name="data[Submit][question]" id="SubmitQuestion2" value="2" />
<label for="SubmitQuestion2">A dark gloomy orange</label>
<br />
<input type="radio" name="data[Submit][question]" id="SubmitQuestion3" value="3" />
<label for="SubmitQuestion3">A perfect glittering blue</label>
</fieldset>
</div>
  • Follow A List Apart's article: Prettier Accessible Forms

Applying a Style to the Currently Selected Label + Radio Button

Styling the <label> is why you'll need to resort to Javascript. A library like jQuery
is perfect for this:

<style type='text/css'>
.input label.focused
{
background-color: #EEEEEE;
font-style: italic;
}
</style>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
$('.input :radio').focus(updateSelectedStyle);
$('.input :radio').blur(updateSelectedStyle);
$('.input :radio').change(updateSelectedStyle);
})

function updateSelectedStyle() {
$('.input :radio').removeClass('focused').next().removeClass('focused');
$('.input :radio:checked').addClass('focused').next().addClass('focused');
}
</script>

The focus and blur hooks are needed to make this work in IE.

css - Input radio button to target outter label when checked

You should use JS.

When input element is inside the label then we do not need id on the element and 'for' attribute on the label, but when it is outside we need it.

<label>
Foo
<input name="foo" type="checkbox" />
</label>

Based on your HTML Code, To alter the styling of the label, would require a selector that affected the parent, which currently isn't possible.
Why? https://css-tricks.com/parent-selectors-in-css/

<input id="foo" name="foo" type="checkbox" />
<label for="foo">Foo</label>

So, to select the label of the :checked input, we need the label to be adjacent, not the parent.

But in your code, HTML's label and input is implicit connecting. So I think the solution is to use JS.

let form = document.querySelector("form");

form.addEventListener("change", (event) => {
let target = event.target;
let targetParent = target.parentElement;

if (
target.type === "radio" &&
targetParent &&
targetParent.tagName.toLowerCase() === "label"
) {
let prior = form.querySelector('label.checked input[name="' + target.name + '"]');
if (prior) {
prior.parentElement.classList.remove("checked");
}
targetParent.classList.add("checked");
}
}, false);
.switch-field {
display: flex;
margin-bottom: 36px;
overflow: hidden;
}

.switch-field input {
position: absolute !important;
clip: rect(0, 0, 0, 0);
height: 1px;
width: 1px;
border: 0;
overflow: hidden;
}

.switch-field label {
background-color: #e4e4e4;
color: rgba(0, 0, 0, 0.6);
font-size: 14px;
line-height: 1;
text-align: center;
padding: 8px 16px;
margin-right: -1px;
border: 1px solid rgba(0, 0, 0, 0.2);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
transition: all 0.1s ease-in-out;
}

.switch-field label:hover {
cursor: pointer;
}

.switch-field label.checked {
background-color: #a5dc86;
box-shadow: none;
}

.switch-field label:first-of-type {
border-radius: 4px 0 0 4px;
}

.switch-field label:last-of-type {
border-radius: 0 4px 4px 0;
}
<form class="form">
<div class="switch-field">
<label class="checked">
<input type="radio" name="switch-two" value="yes" checked />
One
</label>
<label>
<input type="radio" name="switch-two" value="maybe" />
Two
</label>
<label>
<input type="radio" name="switch-two" value="no" />
Three
</label>
</div>
</form>


Related Topics



Leave a reply



Submit