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

I'm puzzled as to why my fixed element is gravitating towards the right side of the entire screen instead of aligning with the right side of its parent element

I have inserted a code snippet that highlights my issue. I am attempting to ensure that the .navigation element remains fixed at the top of the colored divs, however, when using 'right: 0;', the .navigation element seems to shift to the right sid ...

Resolving odd SVG anomalies

I recently exported three SVGs from Sketch to include in HTML, but one of them (specifically the Twitter icon) is presenting a mystery with its appearance. All three SVGs were created in the same manner, using #999999 as the stroke color and having a stro ...

Deletion of ::before and ::after pseudo-elements upon addition of the class is-sticky

One issue I'm facing is that when the class is-sticky is applied to my menu, the ::before and ::after pseudo-elements on my logo become unnecessary. As I am not very proficient in JQuery, I have been unable to resolve this by searching online. The HT ...

What is the best way to prevent content from spilling over into the div on the right

I'm having trouble with a two column div where the text in the left column is overflowing into the right column. How can I prevent this? http://example.com <div id="wrapper-industry"> <div id="wrapper-form"> <div id="form_row"> ...

Customize Cell Styling with Bootstrap Full Calendar CSS

I am attempting to implement a Bootstrap calendar feature where cells are colored green if the timestamp is greater than today's date. This can be achieved by: $checkTime > $today cell.css = green background I came across this code snippet on St ...

Is there a way to connect CSS files from a different directory?

I'm currently in the process of developing a website and I encountered an issue with linking my CSS file, which is stored in a different folder. I originally had it in the same directory as my HTML code, but now I need to figure out how to link it pro ...

Is it safe to use handlebars' default escaping in HTML attributes?

Currently, I am working on a project where various HTML escaping methods are being utilized. Some properties are escaped in the backend and displayed as raw strings using triple handlebars {{{escaped-in-backend}}}, while others are passed from the backend ...

Elements displaying outside of block 'a' tag

I have a set of <a> elements that are styled with display:block. These contain various nested <divs>, for example: <a href="#" class="sliderframe"> <div class="container-caption"> <div class="slider ...

Issue encountered while attempting to attach an event listener to an element within a class

Upon running the code, I encountered an error message that reads: Uncaught TypeError: this.startButton.addEventListener is not a function and I'm unsure of how to resolve it. Although I can successfully console.log the button inside the class, adding ...

Generating safe HTML output in PHP requires adding 4 spaces before the code

I'm currently designing a form that generates basic HTML and other plain text output for users to copy and paste into a content management system. For setting up the variables, I follow this structure: $date1 = $_POST['date1']; $city1 = $_ ...

How can I make a 100vh div stick to the screen?

Is there a way to fix a Google map in a div to the screen so that the map on the left side remains fixed while the content on the right side can scroll? I've tried using position:fixed but it doesn't seem to be working. See the pictures below for ...

Tips on displaying the entire text when hovering over it

I'm facing an issue with a select element that retrieves options from an API. The problem is that some options are too large for the text box and get cut off, making them hard to read for users. <div class="form-group my-4"> <lab ...

Sending a CSS class to an Angular library

In my development process, I am currently working on creating a library using Angular CDK specifically for custom modals. One feature I want to implement is the ability for applications using the library to pass a CSS class name along with other modal conf ...

Tips for adjusting the width of an HTML element that is set to position: fixed

<div class="ui-datatable-scrollable-view" style=" width: 100%; position: relative; "> <div class="ui-widget-header ui-datatable-scrollable-header" style="position:fixed;top:50px"> </div> </div> ...

Using jQuery to toggle visibility on click within WordPress

Looking for a way to make three buttons change color on hover and display different content when clicked? You're not alone! Despite searching through tutorials and forums, the solution remains elusive. The buttons are structured like this: <div i ...

Is it possible to utilize HTML as a platform for interfacing with a C/C++ program?

As I work on a new product with USB interface, I need to create a control app for it. However, my GUI programming skills are limited, so I have thought of using a local web page within the app's installation directory as the interface for the program. ...

What is causing justify-content to not function properly in React?

Struggling to center two divs within a container using flexbox. Flex-wrap and align-items are working fine, but justify-content just won't cooperate. :( <div className="container"> <div className="card- ...

The <a> tag does not lead to a different webpage and cannot be clicked on

I have developed a web component that includes a method to generate a copyright string: '<p>Copyright © 2020 John Doe<a href="https://www.example.com">. Terms of Use</a></p>' After creating the string, I conver ...

Employing Bootstrap, incorporate a vertical divider in the center to split three icons on the left and three icons on the right, all while ensuring the design remains responsive

I have been struggling with a layout issue for the past couple of days and I am in need of assistance from someone who can help me. Unfortunately, I am unable to upload an image here as I am new and do not yet have enough reputation points. The Layout Str ...

Activate validation checks when the Div is loaded

I have a situation where I have multiple divs on my page, with only one visible at a time due to the use of an update panel. Within the third div, I need to implement validations using jQuery. <script src="http://ajax.aspnetcdn.com/ajax/jquery.valida ...