Styling Radio Buttons with CSS - Customizing the Look of Individual Radio Buttons

How can I customize the appearance of each radio button?

This is my attempt at styling them:

div.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 #radio1 [type="radio"]:checked + label {
    background:url("../images/radio1.png") no-repeat;
    color:#FFF;
}
.radio-toolbar #radio2 input[type="radio"]:checked + label {
    background:url("../images/radio2.png") no-repeat;
    color:#FFF;
}
.radio-toolbar #radio3 input[type="radio"]:checked + label {
    background:url("../images/radio3.png") no-repeat;
    color:#FFF;
}

This is the HTML code:

<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>

Answer №1

Give this a shot

    .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;
}
.radio3 input[type="radio"]:checked + label {
    background:url("../images/radio3.png") no-repeat;
    color:#FFF;
}

and in your HTML code

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

Answer №2

While the best solution may require more than just CSS, achieving a CSS-only solution is indeed possible.

The initial code problem involves hiding radio buttons:

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

However, by doing so, the control becomes no longer visible and not clickable to the user, meaning that the :checked state never occurs.

To hide the default radio button, most developers tend to use opacity (although visibility could work as well).

A working solution would be:

.radio-toolbar input[type="radio"] {
    opacity:0;
}

As for the label:

.radio-toolbar input[type="radio"] + label{
    marign-left:-20px; /* adjust based on default radio button size */
    padding-left:20px; /* adjust based on background size to cover default radio */
}

The only drawback of this approach is that it restricts the replacement image size to be close to the original radio button. For larger controls, JavaScript will be needed to trigger the radio button's .checked property.

I hope this solution proves helpful.

Answer №3

Boy Girl

input[type=radio] {

visibility: hidden;  

}

label {

display: block;  

cursor: pointer;  

position: relative;  

padding-left: 20px;  

margin-right: 10px;  

font-size: 12px;  

}

input[type=radio]:checked + label:before {

content: "\2022";  

color: #eaeaea;  

font-size: 25px;  

text-align: center;  

line-height: 16px;  

}

label:before {

content: "";  

display: inline-block;  



width: 14px;  

height: 14px;  



margin-right: 8px;  

position: absolute;  

left: 0;  

bottombottom: 1px;  

background-color: #ccc;  

box-shadow: inset 0px 1px 2px 0px rgba(0, 0, 0, .2), 0px 1px 0px 0px rgba(255, 255, 

255, .6);

}

.radio label:before {

border-radius: 7px;  

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to stop custom Mailchimp form from redirecting upon submission

I have integrated a custom Mailchimp signup form into a webpage I am developing. The form works well, but I would prefer to avoid the automatic redirect to a new tab upon submission, which displays a confirmation message from Mailchimp about joining the ma ...

Issue with bouncing dropdown menu

I am in the process of enhancing a Wordpress website by implementing a jQuery menu with sub-menus. Below is the jQuery code snippet: $(document).ready(function(){ $('.menu > li').hover(function(){ var position = $(this).position(); ...

Add a border to the image when the radio button is selected

I am attempting to add a border to an image when a radio button is checked. Here is the HTML code I am working with: <div class="frm_radio"> <label for="field_n9r1a2-0"> <input type="radio" name="x" id="t" value="Betreuung"> B ...

Utilize Bootstrap 4 to create a full screen desktop layout with columns that automatically adjust to

I am struggling to create a full-screen layout using Bootstrap 4, similar to the one shown in this image. I'm facing issues with the left and right columns not taking up the remaining space between the navigation and footer sections. Additionally, I n ...

Looking to conceal the children of other parents when clicking on a specific parent element?

Is there a way to hide all child elements of every Parent Element except for the one that has been clicked? For example, when I click on one parent element to toggle its children, all other child items should be hidden. Unfortunately, I can't make any ...

Guide on incorporating a rating and review feature into your Windows 8 store app with JavaScript and HTML

Looking for assistance on integrating a rate and review feature into a Windows 8 store app using javascript and html. Specifically, I would like to add this option to both the charm settings and the app bar. When a user clicks on it, they should be directe ...

What is the best way to implement rounded borders using CSS?

Can we use the latest CSS standards to create rounded borders? Unfortunately, this feature is only available in CSS level 3 and above. ...

Is there a method to develop an application utilizing the local web page as the interface?

Just a few days ago, I came up with the idea to create my own user-friendly "interface" for organizing and accessing some of my personal files. Having all related data, images, and links easily accessible with just a few clicks sounded very convenient to m ...

Remove the outline on a particular input and retain only its border

Even after trying to use "outline: 0", nothing seems to change. #input_field { outline: 0; width: fit-content; margin: auto; margin-top: 20%; border-radius: 30px; border: 2px solid turquoise; padding: 6px; } <div id="input_field"> ...

Unable to achieve horizontal alignment with DIV element set as inline-block

Currently, I have five columns that are aligned using the display:inline-block. Everything seems to be working fine, but there's a peculiar issue that arises when I refresh the page multiple times. Occasionally, while the first column remains at the t ...

Prevent Code Execution on Mobile Devices' Browsers

Could someone provide me with the necessary code to prevent certain elements from displaying on a mobile browser? I am specifically looking to exclude particular images and flash files from appearing on a website when accessed via a mobile browser. Somet ...

Is it possible to create a popup without using JavaScript?

Is it feasible to create a popup window without utilizing JavaScript by using the href tag or a similar method? For example, could something like <a href="popup:http://stackoverflow.com">Stackoverflow Popup</a> be used where the popup: functio ...

How can I display data saved from step 2 in a multi-step form with 4 steps, when I return from step 3 to step 2?

Let's consider this scenario: Imagine the user is at step 2 and types their name into <input type="text" class="form-control input-xl" ngModel name="firstName"> They proceed to step 3 but then decide to return to step 2. The information entere ...

Is it Necessary to Create a Separate Stylesheet for This, And if so, How Should it be Implemented? (Using PHP

Creating a customized WordPress options panel is an essential task. One of the key functionalities it offers is allowing users to select their preferred colors. To achieve this, I currently follow a method where I utilize a default stylesheet and then appl ...

Extract the identifier of the class and subsequently pass it to the PHP script

How do I retrieve the id of a dynamically created class within a while loop, where the id corresponds to the user who posted it, and then send this id to PHP? Here's exactly what I'm trying to achieve: HTML: <div class='lol' id=&a ...

The Chrome browser's default stylesheet is overriding the styles of my website

I'm experiencing an issue with the CSS styling on one of my website pages. Here is the CSS code for formatting links: a:link, a:visited, a:active { color: white; text-decoration: none; font-weight: bold; } However, despite these styles, ...

Incorrectly colored buttons upon clicking

I am experiencing an issue with my website where the color of the container div is not changing to the correct color when a button representing a color is clicked. Instead, it seems to be displaying the next color in the array of color codes that I have se ...

Eliminate any vacant areas within a container and shift the container, along with its contents, on the webpage

I want to eliminate the empty space within the container while maintaining some spacing around the black box, and ensure responsiveness. Additionally, how can I shift the container with its content downwards and to the right of the webpage, while still ke ...

Include a query parameter each time a page is added to bookmarks

Is there a way to automatically add a query parameter to a page URL when bookmarked using Chrome? For example, if I bookmark https://www.example.com, can it be saved as https://www.example.com/?bookmarked? I'm thinking I might need to use JavaScript ...

Trying to find the definition of calc(1.25rem + 0.30vw)

While working with Bootstrap, I came across calc(1.25rem + 0.30vw) and I'm unsure of its meaning. This code is being used to design a responsive webpage on a website. Can someone please explain the calculations involved in this? Any help would be gre ...