Tips on how to decorate a radio button using borders and colors

How can I change the color of my radio button and its border color?

I attempted to modify the code below, but it is not producing the desired result.

li.payment_method_bacs input[type='radio']:checked:before {
    background: black !important;
    content: "";
    display: block;
    position: relative;
    top: 19px;
    left: 3px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    border: 3px solid black;
}

li.payment_method_bacs input[type='radio']:checked:before {
    background: black !important;
    content: "";
    display: block;
    position: relative;
    top: 19px;
    left: 3px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    border: 3px solid black;
}
<input id="payment_method_bacs" type="radio" class="input-radio" name="payment_method" value="bacs" data-order_button_text="" checked="checked">

Answer №1

Application of :after and :before on the input element in CSS is considered invalid. To achieve this effect, you will need to enclose other elements within a div container. The div will utilize the checked value of the radio button and apply the desired functionality using :after and :before pseudo-elements. Two different approaches are demonstrated below.

Approach 1: using div as a faux element:

li {
  list-style: none; display:inline-block; margin-right:20px;
}

.radioBox {
  position: relative;
  background: transparent;
  z-index: 1;
  width: 13px;
  height: 13px;
  cursor: pointer;
}

.radioBox>div {
  position: absolute;
  z-index: 0;
  top: 0;
  left: 0;
  width: 13px;
  height: 13px;
  display: inline-block;
  border: 2px solid black;
  border-radius: 12px;
}

.radioBox>input {
  position: absolute;
  z-index: 2;
  width: 13px;
  height: 13px;
  opacity: 0;
}

.radioBox > input:checked + div:after {
    position: absolute;
    content: " ";
    font-size: 0;
    display: block;
    left: 1px;
    top: 1px;
    width: 10px;
    height: 10px;
    background: black;
    border-radius: 10px;
}
<ul>
  <li class="payment_method_bacs">
    <div class="radioBox "><input id="payment_method_bacs" type="radio" class="input-radio" name="payment_method" value="bacs" data-order_button_text="" >
      <div></div>
    </div>

  </li>
  
  <li>
<div class="radioBox ">
  <input id="payment_method_bacs1" type="radio" class="input-radio" name="payment_method" value="bacs" data-order_button_text="" checked="checked" >
    <div></div>

</div>
</li>
</ul>

Approach 2: Using label as a faux element.

ul,li{list-style:none; display:inline-block;}
label {
  display: inline-block;
  cursor: pointer;
  position: relative;
  padding-left: 25px;
  margin-right: 15px;
  font-size: 13px;
}
input[type=radio] {
  display: none;
}
label:before {
  content: "";
  display: inline-block;
 
  width: 16px;
  height: 16px;
 
  margin-right: 10px;
  position: absolute;
  left: 0;
  bottom: 1px;
  background-color: #fff;
 border:1px solid #000;
 border-radius:50%;
}
.radio label:before {
  border-radius: 8px;
}
input[type=radio]:checked + label:before {
    content: "\2022";
    color: #000;
    font-size: 30px;
    text-align: center;
    line-height: 18px;
}
<ul>
<li>
<div class="radio">
  <input id="payment_method_bacs" type="radio" class="inp...
....;>
  <label for="payment_method_bacs1">Paypal</label>

</div>
</li>
</ul>

Answer №2

When adding HTML within input and label tags, the input will remain hidden until the checkbox is checked. You can then customize the style of the label as desired.

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


input[type="radio"]:checked + label::before {
  background: red none repeat scroll 0 0;
  border: 0 solid #333;
  content: "";
  font-size: 0;
  height: 20px;
  width: 20px;
}
<span class="input-lif in-lidt">
  <input class="contact_radio" id="rbsrt1" name="contact" value="individual" checked="checked" type="radio">
  <label for="rbsrt1"> Individual </label>
</span>

 

    

Answer №3

Modifying the appearance of form inputs can be a challenge, as they are rendered differently by each browser.

To achieve consistent results across browsers, you may want to consider using an image (either inline or as a background image) or another element to display your input.

  <label id="myLabel" for="payment_method_bacs">
    <input id="payment_method_bacs" 
      type="radio" 
      class="input-radio" 
      name="payment_method" 
      value="bacs" 
      data-order_button_text="" 
      checked="checked">
  </label>

  #myLabel{
      width: 20px;
      height: 20px;
      background: white;
      -moz-border-radius: 50px;
      -webkit-border-radius: 50px;
      border-radius: 50px;
      border:2px solid #ccc;
      display:inline-block;
  }

  #myLabel input{
      display:none;
  }

You can view the Fiddle here: https://jsfiddle.net/omfv8qhj/

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

Guide on implementing Observables in Angular 2+ to update a single entry in a table in real-time

I'm facing a challenge with my Angular4 and Node.js application. I have a table that displays data with 7 columns using *ngFor. The issue arises when I need to update the last column dynamically and in real-time. Specifically, I want to show a green c ...

Display the accurate duration based on the dates selected in an HTML form automatically

If someone has office hours on Monday, Wednesday, and Friday from 7:00 am to 7:00 pm, and on Tuesday and Thursday from 10:00 am to 9:00 pm, the dropdown menu should display only the timings of 7:00 AM to 7:00 PM if the selected date is a Monday, Wednesda ...

Using Jquery to set values for css properties

I've recently started using jquery and I have a task related to a drop-down menu that I'm struggling with: if (scrnwidth <= 761) { if (display was block) { //Defaultly testi has display:none property. testi = m ...

Minimizing Jitter in HTML5 and JavaScript Applications

I am currently working on developing a metronome as a hobby using JavaScript/HTML5 with the intention of turning it into a FirefoxOS app. One issue I've encountered is Jitter, which is unacceptable for Metronomes. As JavaScript is single-threaded and ...

Display all information associated with the class that matches the clicked ID

Can anyone help me with a Jquery question? I am trying to display all data with the same class when I click on it. I have created a list of clickable items. When I click on an item, the corresponding data should be shown. However, the code I wrote is not ...

Creating a visual that when clicked, reveals an enlarged version at a different location

Is there a way to make an image appear in a different location on the page when it's hovered over? I've searched online but couldn't find a solution using just HTML and CSS. Does anyone know how to achieve this effect? Image not hovered: ht ...

Textview is cluttering space with Html.fromhtml that is not needed

Situation 1 Here is the code I used to display HTML-formatted text in a TextView. However, it seems to be reserving space for a link that isn't visible. holder.textView.setMovementMethod(LinkMovementMethod.getInstance()); holder.textView.setText(Htm ...

Numerous sections of content

Struggling to align these two pieces of content side by side. While I've had success displaying content like this in the past, I'm hitting a roadblock this time around. Any assistance would be greatly appreciated. HTML <div class="block-one" ...

A guide to placing tooltips dynamically in highcharts column charts

I am encountering an issue with tooltips in Highcharts column charts. The problem arises when the series fill up my chart, causing the tooltip to be hidden below the series and cut off by the end of the div. You can see an example here: https://i.stack.i ...

Angular allows for the creation of a unique webpage layout featuring 6 divs

I am working on a project where I have an image of a car and I need to overlay 6 divs onto the image which can be selected with a mouse click. When a user clicks on one of the divs, it should change color. I've attempted using z-index, but it doesn&ap ...

A demonstration of VueJS Nested Builder functionality

I am currently exploring VueJS and working on a project to create a simple page builder application that allows users to add sections and subsections. I am seeking guidance on how to properly set up the Vue data property for this functionality. Any advice ...

Failure to display masonry arrangement

I am working on creating a stunning masonry layout for my webpage using some beautiful images. Take a look at the code snippet below: CSS <style> .masonryImage{float:left;} </style> JavaScript <script src="ht ...

Obtaining the Div ID After Clicking on a Link

I'm attempting to fade out a box once the X in that box is clicked. I haven't been able to make it work even after searching on Google. I managed to get it working when clicking the div itself using $(this), but my goal is for it to work when the ...

PHP jQuery buttons for popovers

With a simple click of a button, I can generate 8 presentations and then edit each one individually by clicking on its respective name. Within this editing process, there is another form where I would like to include additional buttons that allow me to cus ...

Hover over this area to reveal the hidden text as it slides into view

My goal is to hover over truncated text and reveal the full length of the dynamic text. However, I am struggling with removing extra space at the end of shorter text when hovered and displaying all the text for longer ones. I have a solution that almost w ...

Enhancing React-Admin with custom Material-UI theme overrides for targeted CSS selectors on a global scale

When working with React-Admin, I encountered a challenge in applying specific CSS code globally within my Material UI theme. As I set up my theme, I included the following lines in the overrides section: overrides: { "@global": { &quo ...

What is the best way to conceal a div when there are no visible children using AngularJS?

I need help with hiding a parent div only if all the child items are invisible. I have successfully hidden the parent when there are no children, but I am struggling to figure out how to hide it based on visibility. <div ng-repeat="group in section.gro ...

Angular encountering issues with loading external JavaScript files due to the error: ENOENT - indicating that the specified file or directory does

I am attempting to incorporate a bootstrap template into Angular. The template requires some external JavaScript and CSS files that need to be linked. I have placed these files in the assets folder and referenced them in the styles and scripts arrays of an ...

The alignment of elements in the div seems to be slightly skewed

Currently, I am in the process of creating a website with the temporary name yeet.io. I am facing an issue where I am attempting to center both an input and h1 element inside a div vertically and horizontally, but they keep appearing misaligned for some r ...

"Ajax validation is causing the duplication of the HTML page content within an HTML

My PHP username validation with Ajax code seems to be duplicating my HTML page inside an HTML div element, which is used for displaying Ajax errors. Despite trying various solutions and searching on Google, I have not been able to find a resolution yet. Th ...