How can you style a radio button in CSS while displaying a label next to it?

Trying to customize a radio button and align its label next to it using CSS has proven to be more challenging than expected.

The current setup is not producing the desired outcome as the radio button appears distorted and the label is not perfectly inline with it.

The ultimate goal is to achieve a layout similar to this:

view image for reference

This is what has been accomplished thus far:

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

input[type="radio"] + label
{
    background: #fff;
    border: solid 2px #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
border-radius:50%;
}

input[type="radio"]:checked + label
{
    background: #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
border-radius:50%;
}
<input type="radio" id="male" name="gender" value="M">
<label for="male">Male</label>

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

Edit:

The main challenge now is to ensure all the radio buttons and labels are displayed in a single line.

Answer №1

Make sure to follow the instructions below to customize the appearance of your label:

input{
  display: none;
}

label{
  float: left;
  margin-right: 10px
}

label span{
  width: 16px;
  height: 16px;
  background-color: #fff;
  border-radius: 50%;
  border: 1px solid #222;
  float: left;
  margin-right: 5px;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
     -moz-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
     -moz-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}

input:checked + label span:before{
  content: '';
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #222;
}
<input type="radio" id="male" name="gender" value="M"/>
<label for="male"><span></span>Male</label>

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

Answer №2

If you're considering some HTML tweaks, you may discover that this approach offers more versatility:

Ensure to incorporate a focus state for the input to enhance accessibility for users who rely on keyboards.

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>

Answer №3

In case you are using your label for both displaying the radio button and as the actual label, there might be an issue: Currently, you are setting your <label> to be a 20px width circle while also including text within it. You can try the following approach instead:

input[type="radio"] + label::before
{
    background: #fff;
    border: solid 2px #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
    border-radius:50%;
    margin-right:5px;
}

This code snippet adds a pseudo-element to your label that can be styled similar to a <div> or <span>. Additionally, for the checked radio button, you can simplify the styling like this:

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

Answer №4

To achieve this effect, simply insert a ::before pseudo-element after the input + label selector.

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

input[type="radio"] + label::before
{
    content:'';
    background: #fff;
    border: solid 2px #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
    border-radius:50%;
}

input[type="radio"]:checked + label::before
{
    background: #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
    border-radius:50%;
}
<input type="radio" id="male" name="gender" value="M">
<label for="male">Male</label>

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

Answer №5

.custom-container {
    display: flex;
    align-items: center;
    position: relative;
    padding-left: 25px;
    margin-bottom: 15px;
    cursor: pointer;
    font-size: 20px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
.custom-container input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
}
.custom-checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 22px;
    width: 22px;
    background-color: #ccc;
    border-radius: 50%;
}
input:checked ~ .custom-checkmark {
    background-color: #1478A2;
}
.custom-checkmark:after {
    content: "";
    position: absolute;
    display: none;
}
input:checked ~ .custom-checkmark:after {
    display: block;
}
.custom-checkmark:after {
 top: 7px;
left: 7px;
width: 6px;
height: 6px;
border-radius: 50%;
background: white;
}
<label class="custom-container">Option One
  <input type="radio" checked="checked" name="option"></span>
  <span class="custom-checkmark"></span>
</label>
<label class="custom-container">Option Two
  <input type="radio" name="option"></span>
  <span class="custom-checkmark"></span>
</label>

Hope you find this information helpful! :)

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

Having difficulty handling text overflow in Angular4 and HTML

I'm facing an issue with displaying a very large text in a table. Despite trying various attributes such as - { text-overflow: clip; } { text-overflow: ellipsis; } { text-overflow: ellipsis-word; } { text-overflow: "---"; } { text-overflow: ellip ...

Using AngularJS to bind radio buttons to ng-model

Here is a snippet of my HTML code where I attempted to bind the "formFriendsAll" scope to the ng-model. <form ng-submit="submitForm()" > <div class="col-sm-3"> <div class="form-group"> <label>Which Persons to show?< ...

After applying sorting, jqGrid displays an empty space below the final row

Having an unusual problem with sorting in the jqGrid. When I sort in descending order, there is extra space below the last row, but when I sort in ascending order, the table ends abruptly at the bottom without allowing any further scrolling. I have includ ...

When a button is clicked, the event is not triggered within a FirefoxOS application

Despite functioning perfectly on Firefox for desktop, my application encounters issues when tested on my ZTE Open C (Firefox OS 1.3). Pressing the button does not trigger any action, while onmouseup and onclick events work flawlessly. Even attempts to bin ...

Revamp the HTML table with JavaScript headers

I imported data from a CSV file into an HTML table and here is how it appears: <div id="myTable" style="-ms-overflow-x: auto;"> <table> <tbody> <tr class="rownum-0"> <td>Make</td> ...

Preventing hyperlinks within a table from being recognized as the selected table row

I encountered a small issue while working on a webpage where clicking on a link within a table cell causes the entire row to be marked as clicked. However, I want only the link to trigger the click event, not the entire cell. You can view the jsfiddle he ...

Guide on uploading multiple images using AJAX and jQuery

Here is the code snippet I am currently working on, which allows users to upload one or multiple files and delete them. All the file data is stored in the variable form_data. Unfortunately, I have not been successful in getting the file upload functionali ...

Using HTML to design interactive buttons that can send API requests and display their current status

Seeking assistance with creating buttons to control a remote light's ON and OFF states while reflecting their status as well. The specific http API calls for the light are necessary to toggle its state. Turn On = http://192.168.102.22:3480/data_requ ...

Web view that remains fixed regardless of resolution, compatible with both iPhone and iPhone4 devices

Looking to create a webview with one HTML and one CSS file that displays graphics at the same size, but in their native resolution. Currently, my webviews built for 320x480 appear sharp when scaled up (text and border-radius), but images are displayed at ...

What is the process of integrating SCSS into an Angular2 Seed Project?

Is there a recommended method for incorporating SCSS into an Angular2 Seed Project? Are there any informative tutorials or reference sites available? I attempted to implement SCSS following instructions from this link https://github.com/AngularClass/angu ...

Tips for bringing in pictures from outside directories in react

I am new to React and trying to import an image from a location outside of the project's root folder. I understand that I can store images in the public folder and easily import them, but I specifically want to import them from directories outside of ...

Running system commands using javascript/jquery

I have been running NodeJS files in the terminal using node filename.js, but now I am wondering if it is possible to execute this command directly from a JavaScript/jQuery script within an HTML page. If so, how can I achieve this? ...

Different div elements are clashing with each other when it comes to hiding and

Having added multiple div elements with different IDs, I am facing an issue where clicking one div displays it while hiding the others. I am looking for a solution to prevent conflicts between the divs. Any suggestions on what might be causing this problem ...

Guide to switching a button using JavaScript

I am trying to create a button that will toggle the font size between 16px and 14px when clicked. The button text should also change from "Increase Font" to "Decrease Font" accordingly. UPDATE: I managed to get it working, but it only toggles twice and th ...

Switch from HTML symbol to Java symbol

Is it possible to change an HTML symbol into a Java symbol? For instance, if I have &#xe000, is there a way to obtain the Java char representation like \ue000? What steps should I take to achieve this conversion? ...

Is it possible to create a CSS design where alternating table rows have two different heights?

Looking to design a table where the height of every other row is approximately one-third of the preceding row. I've managed to style this with an inline method, but I'm curious if it's possible to create separate tags for different tr styles ...

What is preventing me from utilizing sliding images within a modal popup?

When I click on a button, a modal window opens with a carousel slider inside. However, I am experiencing issues with sliding on the chevron left and right buttons. On my computer, the images slide every 5 seconds, but on the JSFiddle platform, the sliding ...

Equal height elements - Bootstrap

Is there a way to make all elements in a list the same height, even if one element has more text than the others? I've been using divs for this purpose, but I'm open to switching to li items. Any advice on the best approach? I'm currently w ...

If a <section> element contains a <header>, must it also include a <footer>?

Here is the current state of my code: <section id="one"> <h2>Section Title</h2> <p>Lorem ipsum...</p> <p>Lorem ipsum...</p> <p>Lorem ipsum...</p> </section> <section id="two"&g ...

If the user confirms it with a bootstrap dialog box, they will be redirected to the corresponding links

Firstly, I am not interested in using javascript confirm for this purpose. Please read on. For example, adding an onClick attribute to each link with a function that triggers a system dialog box is not what I want. Instead of the standard system dialog bo ...