How can I customize the appearance of a disabled radio button within spans?

While attempting to make my radio button square using CSS by incorporating spans and before and after elements, I encountered a new client request. The client now wants the radio buttons to be disabled with a unique style when in this state. Unfortunately, due to the use of spans, I faced a challenge in styling the radio button when it is disabled. Is there a CSS-based solution to this problem rather than resorting to JavaScript?

CSS

body {
    background: #fbfbfb;
    font-family: Arial;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.7);
}

.radio-button
{
    display: none;
}

.radio[disabled] {
    display: none;
}

.radio-label  {
    display: inline-block;
    padding: 5px 10px;
    cursor: pointer;
}

.radio-label .radio-span {
    position: relative;
    line-height: 16px;
}

.radio-label .radio-span:before {
    font-family: "FontAwesome";
    content: "\f0c8";
    display: inline-block;
    padding-right: 3px;
    vertical-align: middle;
    font-weight: 900;
}

.radio-label .radio-span:after {
    position: absolute;
    top: 1px;
    left: 0px;
    transition: 300ms;
    opacity: 0;
    font-family: "FontAwesome";
    content: "\f00c";
    display: inline-block;
    padding-right: 3px;
    vertical-align: middle;
    font-weight: 900;
}

.radio-label .radio-button:checked+.radio-span:after {
    opacity: 1;
}

/* 
.radio-span needs to have this style when the radio button is disabled
{
    font-family: "FontAwesome";
    content: "\f04d";
    display: inline-block;
    padding-right: 3px;
    vertical-align: middle;
    font-weight: 900;
} */

HTML

<!DOCTYPE html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="./style.css">

        <script src="https://kit.fontawesome.com/mycode.js" crossorigin="anonymous"></script>
    </head>
    <body>
        <form>
            <label class="radio-label"><input id="toDisable" type="radio" name="gender" value="male" class="radio-button"> <span class="radio-span"> Male </span></label><br>
            <label class="radio-label"><input type="radio" name="gender" value="female" class="radio-button"> <span class="radio-span"> Female </span> </label><br>
            <label class="radio-label"><input type="radio" name="gender" value="other" class="radio-button"> <span class="radio-span"> Other </span></label> <br>
        </form>
        <script>
            document.getElementById("toDisable").disabled = true;
        </script>
    </body>
</html>

Answer №1

If the radio buttons are followed by span elements, you can use this selector:

.radio-button[disabled] + span {
  ...
}

This code utilizes the disabled attribute on the input element.

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

Following the submission of a message, the textarea automatically inserts a line-break

Can someone please help me troubleshoot an issue with my chat app? Every time I try to send a message, the textarea adds a line break instead of just focusing on the textarea so I can send a new message smoothly. I have recorded a video demonstrating the ...

The Angular Google map fails to show up when embedded into a SweetAlert2 popup

I am attempting to showcase a segment of Google Maps within a popup using sweetalert2. The HTML code snippet provided below works fine when directly placed in the HTML page, but fails to work within the alert. export function showGoogleMap() { swal({ ...

Challenge with Filter Functionality when Activating Button

Can you help me implement a search filter using buttons with the Isotope Plugin? For example, entering a search value in an input field and then clicking a search button to display the search results. How can I achieve this using buttons? Below is the H ...

Alert: A notification when navigating away from your site

Is there a way to notify users when they click on an external link that they are leaving your site? <div class="row"> <div class="col-lg-12"> <div class="form-group"> *If you need information on other applicable forms, you ...

Issue with PHP retrieving initial value of post data

Hi there, I am facing an issue with my PHP code where the first value of the input field is not being displayed. However, when I check the console.log, it shows correctly. Here is my console.log output: https://i.sstatic.net/eZvg6.png PHP Output: https ...

AngularJS | Using ngAnimate to Display Form Validation Errors

My form errors are currently being displayed successfully using CSS and conditional classes, but I recently discovered ng-message and ng-animate. Would it be possible to use ng-message to contain the error messages? Also, I have been unable to find any tu ...

Place the select option within the input field

input { width: 100%; } .input-currency:after { position: absolute; right: 0; top: 10px; content: 'USD'; border-left: 1px solid black; padding: 0 7px; padding-right: 4em; } <div class="input-currency"> <input id="value" ...

Transitioning Between Div Elements Using Jquery

I am stuck with the following HTML code: <section> <div id="first"> ---content </div> <div id="second"> ---content </div> </section> When the page loads, the second div and its contents are not visible (I'm uns ...

Swap out HTML lists for a PHP array structure

For my assignment, I was tasked with creating an HTML list from an array and generating a drop-down menu based on another array. The requirement is that when a particular subject is selected from the drop-down menu, only that subject along with its teacher ...

The POST request did not yield an HttpResponse object; instead, it returned None

When I submit a selection from a dropdown form to views as a POST request, and then use this selection to query data from Django, I encounter an issue while trying to map Django models data to Highcharts following this approach. The error message "the view ...

Modifying object properties in JavaScript

Looking to update a boolean attribute for an object in JavaScript (in this case, the object is part of fullPage.js library). I am interested in turning off the navigation attribute by setting it to false, and ideally would like to do this from a separate f ...

Formatting that differs based on whether the content appears on the left

I'm in search of a solution to align specific items to the left on left-hand pages and to the right on right-hand pages. I have come across @page :left/:right, but it seems to only affect the page layout and margins, not the actual content on the page ...

Placing a pair of buttons on the border of a div container with the help of the bootstrap grid

Could you please assist me in making these two buttons stick to the edge of the div using bootstrap grid? If possible, could you also provide some explanation? I am currently trying to grasp how bootstrap grid works... Here is my progress so far: https:/ ...

Please indicate a width of 3 characters for the HTML input text

Is there a way to create an HTML text box that can only fit 3 characters exactly? I came across this code online: <input type="text" style="width: 10px; padding: 2px; border: 1px solid black"/> This code creates a text box with a width of 10px ...

javascript tabs not functioning as expected in HTML

I am currently putting the finishing touches on a basic website for a project I'm involved in. The site features two tab controls - one on the right side of the page and the other on the left. Each control has 3 tabs that, when clicked, display differ ...

The appearance of HTML is acceptable in a browser but appears differently in an email

I'm encountering an issue with email encoding. After reading an HTML file from disk and sending it through Gmail, the content appears distorted when received. Even the list bullets are messed up! Despite encoding the file as UTF-8, everything looks fi ...

Make sure your website design is responsive by setting the body to the device's

https://i.sstatic.net/3xHcx.png I'm struggling to make the body of my page responsive for mobile users. Whenever I try using this line of code, it just creates a messy layout. Any advice on how to fix this? <meta name="viewport" cont ...

What process allows for this Twitter API response to be converted into HTML format automatically?

It is common knowledge that Twitter operates in an API-centric manner, meaning all Twitter apps retrieve their data through the API. Upon accessing (or .xml for those who prefer), a JSON formatted result with plaintext content and no additional formattin ...

Attempting to extract the text within a table row cell by clicking on the cell directly following it

I am trying to extract the data from a table row when I click on a specific div within that row HTML: <td class="ms-cellstyle ms-vb2">10</td> <td class="ms-cellstyle ms-vb2"> <div class="ms-chkmark-container"> <div ...

Tips for automatically resizing a canvas to fit the content within a scrollable container?

I have integrated PDF JS into my Vue3 project to overlay a <canvas id="draw_canvas"> on the rendered pdf document. This allows me to draw rectangles programmatically over the pdf, serving as markers for specific areas. The rendering proces ...