What are the best ways to customize radio buttons with CSS?

Hey everyone, I'm currently working on creating radio buttons similar to the one shown in this image. Could you please provide me with some guidance on styling the radio buttons and drawing the connecting line between them? Thank you for all your help. This is for my website and I want to achieve the same effect. I am facing difficulty with just this aspect so any suggestions are welcome.

/* Styling for dog photo background */
.sizeVisual-image {
  background-color: #00263e;
}

/* Hover effect for label text */
ul li:hover label {
  color: #f2a900;
}

/* Selected label color */
input[type=radio]:checked ~ label {
  color: #f2a900;
}

/* Horizontal layout for radio buttons */
.radioGroup {
  display: flex;
  background-color: #00263e
}
<body>

<!-- INSERT IMAGE  -->
<div class="sizeVisual-image">
  <img class="img img_larger" src="//cdn.shopify.com/s/files/1/1577/4333/files/Fit-Guide-Measurement-Girth.svg?v=3289972504356902595" width="400" height="300" alt="">
</div>
<!-- END INSERT IMAGE  -->


<!-- Create a radio button set for size options -->
<fieldset>

  <legend class="isVisuallyHidden">Select Size</legend> <!-- Title of the button set -->
  <ul class="radioGroup radioGroup_scale"> <!-- List of buttons -->

    <!-- XXSMALL button -->
    <li>
      <input type="radio" name="size" id="xxsmall" value="XXSmall" class="radio-input isVisuallyHidden js-product-size" checked="">
      <label class="radio radio_size" for="xxsmall">
        <span class="radio-inner_size"><span></span></span>
        <div class="set">
          <div #FF0000class="set-hd">
            <div class="radio-text_size">
              XXSmall
            </div>
          </div>
          <div class="set-bd">
            <div class="radio-text_size isHiddenTablet">
              13-17 in (33-43 cm)
            </div>
          </div>
        </div>
      </label>
    </li>
    <!-- End of XXSMALL button -->

    <!-- XSMALL button -->
    <li>
      <input type="radio" name="size" id="xsmall" value="XSmall" class="radio-input isVisuallyHidden js-product-size">
      <label class="radio radio_size" for="xsmall">
        <span class="radio-inner_size"><span></span></span>
        <div class="set">
          <div class="set-hd">
            <div class="radio-text_size">
              XSmall
            </div>
          </div>
          <div class="set-bd">
            <div class="radio-text_size isHiddenTablet">
              17-22 in (43-56 cm)
            </div>
          </div>
        </div>
      </label>
    </li>
    <!-- End of XSMALL button -->

    <!-- SMALL button -->
    <li>
      <input type="radio" name="size" id="small" value="Small" class="radio-input isVisuallyHidden js-product-size">
      <label class="radio radio_size" for="small">
        <span class="radio-inner_size"><span></span></span>
        <div class="set">
          <div class="set-hd">
            <div class="radio-text_size">
              Small
            </div>
          </div>
          <div class="set-bd">
            <div class="radio-text_size isHiddenTablet">
              22-27 in ( 56-69 cm)
            </div>
          </div>
        </div>
      </label>
    </li>
    <!-- End of SMALL button -->

    <!-- MEDIUM button -->
    <li>
      <input type="radio" name="size" id="medium" value="Medium" class="radio-input isVisuallyHidden js-product-size">
      <label class="radio radio_size" for="medium">
        <span class="radio-inner_size"><span></span></span>
        <div class="set">
          <div class="set-hd">
            <div class="radio-text_size">
              Medium
            </div>
          </div>
          <div class="set-bd">
            <div class="radio-text_size isHiddenTablet">
              27-32 in (69-81 cm)
            </div>
          </div>
        </div>
      </label>
    </li>
    <!-- End of MEDIUM button -->

    <!-- XLARGE button -->
    <li>
      <input type="radio" name="size" id="large-xlarge" value="Large/XLarge" class="radio-input isVisuallyHidden js-product-size">
      <label class="radio radio_size" for="large-xlarge">
        <span class="radio-inner_size"><span></span></span>
        <div class="set">
          <div class="set-hd">
            <div class="radio-text_size">
              Large/XLarge
            </div>
          </div>
          <div class="set-bd">
            <div class="radio-text_size isHiddenTablet">
              32-42 in (81-107 cm)
            </div>
          </div>
        </div>
      </label>
    </li>
    <!-- End of XLARGE button -->
  </ul>
</fieldset>
<!-- End of radio button set for size options -->

</body>

Answer №1

To achieve the desired outcome, it is recommended to utilize a blend of techniques. One effective approach is placing the input within the label as shown below:

<label>
  <input type="checkbox">
  <div>{ label stuff }</div>
</label>

By structuring the markup in this way, not only does it simplify the code but also enhances accessibility.

In order to style the radiobutton itself, you can make use of :before and :after pseudoclasses. A detailed guide on how to accomplish this can be found here on w3schools.

For creating the line, apply an :before pseudoclass on the label with a background or border similar to the following example:

label {
  position: relative;
}

label:before {
  border: 2px solid white;
  content: '';
  height: 0;
  left: 0;
  position: absolute;
  top: 20px;
  width: 100%;  
}

Adjust the top value accordingly to align it neatly with the radiobutton.

Please note that while some details may have been omitted, these instructions should provide guidance for you to successfully implement the solution independently.

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

The onchange function in JavaScript is malfunctioning

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Product page</title> <!-- Fonts -- ...

Capture the beauty of Safari with images displayed in the input file element

I'm facing an issue with the file upload element on my website. Most browsers, like Chrome and FF, display the image name when a file is chosen. However, Safari seems to show a tiny thumbnail instead. My goal is to create a custom upload button that ...

JQuery Scroll Spy Implementation

I have structured the page with a header at the top and 3 columns below. The left column contains a menu, the middle column is a text container, and the right column is another section. As the page scrolls up, the menu becomes fixed in position which func ...

Bootstrap mobile menu logo design

Currently, I am working on a bootstrap template where I have created a div class containing a logo. However, I have noticed that when I resize the Chrome window, the logo overlaps with the mobile menu. Here is the mobile version of my site: https://i.sst ...

Error: Definition Already Exists

I'm currently debugging a layout and encountering some peculiar errors. The page is being served as DTD XHTML 1.0 Strict. The error message looks like this: ID "OFFICENAME" already defined: div class="office" id="officename" ID "OFFICENAME" origin ...

Automatically showcase images from a directory upon webpage loading

Is there a way to modify my code so that the images from the first directory are displayed on the page when it loads, instead of waiting for a menu option to be clicked? The page looks empty until a menu option is selected, and I would like the images to s ...

Experiencing difficulties with the alignment of Bootstrap columns

I'm encountering an issue with displaying 3 Bootstrap columns in the ContentArea. Even though I can locate them using Developer tools, they seem to be positioned at the bottom of the ContentArea, making them not visible. I've attempted adjusting ...

Tips for placing a checkbox above a DIV element when generating checkboxes within a foreach loop

This is the output I have generated This HTML code belongs to me <td class="category" colspan ="2" id="tdcategory1"> <div id="category1" style="border:none !important; overflow:hidden; width:auto; height:auto"> </div& ...

Learn the method for switching between exclusive visibility using Bootstrap class toggles

I've set up a jQuery function that toggles the visibility of different divs when clicking on folder icons: $(document).ready(function() { $("#techfolder").click(function(){ $("#txt").toggleClass("d-none"); }); $("#persfolder").click(functio ...

The Navbar's Toggle Icon Causes Automatic Window Resize

I implemented a mobile navigation bar using React and JS that slides in from the left when clicked. However, I encountered two issues: whenever I click on a menu button in the navbar or when my ad carousel moves, the window resizes for some reason. Additio ...

Is it possible to conceal specific sections of a timeline depending on the current slide in view?

For my product tour, I've structured a timeline with 4 main sections, each containing 4-5 subsections. Here's how it looks: <ul class="slideshow-timeline"> <li class="active-target-main main-section"><a href="#target">Targe ...

Ways to stop the parent container from causing the child element set as fixed to scroll

When I have a parent and children with the property 'overflow:auto', scrolling the children causes the parent to start scrolling after it reaches the end. How can this be prevented? I do not want the parent container to scroll even after the chi ...

What is the process for changing the output paper size to A4 in React Native (expo android)?

Using React Native to develop an Android app for billing purposes, I encountered an issue with the output paper size being 216mmX279mm instead of the standard PDF size of 210mmX297mm. Utilizing expo-print and printToFileAsync from expo, I aim to achieve a ...

The JavaScript else statement is failing to execute as intended

Hello, I am new to JavaScript and could really use some assistance. In the code snippet below, I am having trouble with the logic for the submit button. The line _btn.addEventListener seems to be causing an issue where only the "if" part is being executed ...

Alignment issues in buttons within Bootstrap

I'm facing an issue with the code snippet below <div className="row"> <div className="col-md-3"> <DateTimePickerComponent id="dateFrom" ...

Issues with Ul List Alignment (CSS and HTML)

<div class="companies"> <ul class="logos"> <li><img src="images/image1.png" alt="" height="25px" /></li> <li><img src="images/image2.png" alt="" height="25px" /></li> <li> ...

Ways to decrease font size and insert a line break within a constrained input space?

I am facing an issue where the input box remains static with old numbers when it overflows, and newly typed numbers do not appear at all. <!DOCTYPE html> <html> <body> <input type="text" id="output-area" placehold ...

Encountering issues with the interaction between Bootstrap and my custom CSS file in a Flask app with

I am encountering an issue with my template where I have included both Bootstrap and CSS files, with Bootstrap loaded before CSS. However, when I try to apply custom CSS using IDs or classes, the changes do not reflect. Strangely, styling for h1 elements i ...

Ways to eliminate gaps between div elements with CSS

I am currently working on a webpage that consists of one main div housing two inner divs (left_menu and content), along with an additional footer div outside the main container. When viewing the page, there seems to be a noticeable gap between the footer ...

Adjusting the width of speech balloon in CSS

This question might seem basic to those who are familiar with CSS (unlike me lol). I'm currently working on incorporating speech balloons into my website using this code from jsfiddle. However, I encountered an issue when the text inside the balloon i ...