Issue with displaying personalized radio buttons

I added a custom radio button, but it's not showing up. It seems to work when arranged like this:

<label>   
<input type="radio" name="gender" />
<span>Female</span>
</label>

Why isn't it working? Is there something wrong with this code?

This is my HTML Code:

<div class="radio_btns">
<label><span>Gender</span></label>
<label>
  <span>Female</span>
  <input type="radio" name="gender" />
</label>
<label>
  <span>Male</span>
  <input type="radio" name="gender" />
</label>
</div>

This is the CSS Code:

.radio_btns {width:100%; float:left;}
.radio_btns [type="radio"] {border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px;}
.radio_btns label {display: block; cursor: pointer; line-height: 2.5;}
.radio_btns [type="radio"] + span {display: block;}
.radio_btns [type="radio"] + span:before {content: ''; display: inline-block; width: 1em; height: 1em; vertical-align: -0.25em; border-radius: 1em; border: 0.125em solid #fff; box-shadow: 0 0 0 0.15em #000; margin-right: 0.75em; transition: 0.5s ease all;}
.radio_btns [type="radio"]:checked + span:before {background: #07eb07; box-shadow: 0 0 0 0.25em #000;}
.radio_btns label {float:left; width:150px;}

Here is what I get with this code

Answer №1

The benefit of using an adjacent sibling selector is that it targets the element immediately following another one. In your situation, you will need a span placed after each input for the styles to take effect:

.radio_btns {
  width: 100%;
  float: left;
}

.radio_btns [type="radio"] {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

.radio_btns label {
  display: block;
  cursor: pointer;
  line-height: 2.5;
}

.radio_btns [type="radio"]+span {
  display: inline-block;
}

.radio_btns [type="radio"]+span:before {
  content: '';
  display: inline-block;
  width: 1em;
  height: 1em;
  vertical-align: -0.25em;
  border-radius: 1em;
  border: 0.125em solid #fff;
  box-shadow: 0 0 0 0.15em #000;
  margin-right: 0.75em;
  transition: 0.5s ease all;
}

.radio_btns [type="radio"]:checked+span:before {
  background: #07eb07;
  box-shadow: 0 0 0 0.25em #000;
}

.radio_btns label {
  float: left;
  width: 150px;
}
<div class="radio_btns">
  <label><span>Gender</span></label>
  <label>
  <span>Female</span>
  <input type="radio" name="gender" />
  <span></span>
</label>
  <label>
  <span>Male</span>
  <input type="radio" name="gender" />
  <span></span>
</label>
</div>

Answer №2

There seems to be an issue with the css styling for the radio buttons. The class .radio_btns [type="radio"] has custom properties such as

height: 1px; width: 1px; and position: absolute;
which are causing the radio buttons to not display properly. To fix this, try removing these css properties so that the radio buttons become visible.

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

Interacting with JQuery UI Button causes it to shift position on mouseover

In my table, there are two large cells. The left cell contains two drop-downs and a JQuery UI button that is causing trouble, while the right cell displays a list generated from an AJAX database query. As the list grows longer, the button gradually moves u ...

Can I create a div with a rounded right side?

Is there a way to give a div a slightly rounded right side using CSS? If so, can you show me how in this fiddle template: http://jsfiddle.net/z2hejemu/ HTML <div class="rounded-side"> <p>By default, this div is rectangular. I would like t ...

Retrieve the text located within the span tag using Selenium

When I try to inspect the element using Google Chrome, I come across the following code: <div class="form-style-abc"> <fieldset> <legend><span class="number">*</span>Search Zone</legend> I am trying to retrieve ...

Ensure you have the right zoom in and out percentages to properly test how your website responds on various browsers across different

Currently, I am using Google Chrome to test the responsiveness of a web application that I am developing. As of today, my browser versions are as follows: Google Chrome - Version 88.0.4324.96 (Official Build) (64-bit) Mozilla Firefox - Version 84.0.2 (64- ...

Refresh the CSS inheritance and hover effects

Facing an issue with my web project where I am using CSS. At times, I need to reset the inheritance from the parent class for certain elements. Although I have defined some classes to style my elements, I want to completely reset all styles except for hove ...

Issue with the pluses and minuses in the Bootstrap accordion

Can someone explain why the panel-header displays all "-" instead of "+"? When I click on the panel, it changes all "-" to "+". This happens consistently, not just the first time the page loads. My CSS code: .panel-heading a:after { font-family: &ap ...

Incorporate a header into the <img> request

Is it possible to include a header in a standard HTML <img> tag? Currently, we have: /path/to/image.png However, this path is actually a RESTful endpoint that requires a userID header. GET /path/to/image.png Header userId: BobLoblaw This endpoin ...

The color attribute in a Div container remains the same even when hovering over it

Hello everyone, I am new to stack overflow so please bear with me. I am currently working on this small webpage for my IT course and I have encountered a significant issue. .float1 { float:right; background-color:#A3635C; margin-top: 150px; ...

Running nodejs scripts within my HTML and JavaScript code

Using express, I send an HTML file to incoming GET requests: app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/public/index.html')); }); Within that HTML file, I included another JavaScript file, script.js, us ...

Filtering rows of an HTML table that contain links in the `<href>` column data in real time

When using HTML and JavaScript, I have found a solution that works well for many of the columns I am working with. You can see the details on how to dynamically filter rows of an HTML table using JavaScript here. var filters=['hide_broj_pu',&apo ...

Is there a way for the React select component to adjust its width automatically based on the label size?

After defining a React select component, it looks like this: <FormControl> <InputLabel id="demo-simple-select-label">Age</InputLabel> <Select labelId="demo-simple-select-label" id=&quo ...

Is there a way to dynamically update a game's highscore from a database without having to refresh the page?

I developed a JS snake game using HTML5 canvas. In the event that the user loses, the score is transmitted to the database through an AJAX call in my PHP script. The PHP code then compares this score to the current highscore and updates it if needed. Howev ...

Exploring Partial Views in Bootstrap Single Page View and AngularJS

Currently, I am utilizing Bootstrap's single page view design, specifically the one found at this link: http://www.bootply.com/85746. As my code in the view has grown to nearly 500 lines and is expected to expand further, I am seeking a method to crea ...

Tips for ensuring that all children within a flex-container have equal heights

I seem to be facing an issue with this problem. My goal is to ensure that all the child divs within a flex container have the same height as the tallest child div, which in this case is 100px. Additionally, I want them to be aligned at the center. I&apos ...

Struggling to align your image properly?

My attempt to center the image "Logo_Home.svg" on my page has failed, as it is currently positioned in the top left corner. Can anyone help me identify what I am doing wrong and guide me in the right direction? Thank you. EDIT 1 I realized that I forgot ...

Having trouble displaying several thumbnails side by side

I am looking to display thumbnails in a row, but they are currently showing up in a single column. I have tried correcting the row class in the HTML template provided, but it is not displaying as desired. Here is how it looks like now {% extends "ba ...

Activate the horizontal scrollbar within each flex item when it is stretched beyond its original width

My goal is to have both the child-1 (blue) and child-2 (black) retain their original width when the sidebar appears by enabling horizontal scrolling upon clicking anywhere in the demo below. document.body.onclick = () => document.querySelector(&apos ...

The Super Sub menu is failing to display correctly as intended

I encountered an issue while trying to create a Super sub-menu. The problem is that the super sub-menu appears when I hover over the main menus, instead of the sub-menus. I suspect there may be something wrong with the display: none; attribute, but I' ...

Grayscale to color image slider using JQuery, HTML, and CSS

Seeking assistance from the talented individuals here. Currently in the process of constructing a website for a client, and one of the key components on the index page is an image slider/fader that showcases a series of images. The client has requested a u ...

The function `jQuery .html('<img>')` is not functional in Firefox and Opera browsers

This particular code snippet jq("#description" + tourId).html('<b>Opis: </b> '+ data); has been tested and functions correctly in Internet Explorer, Firefox, and Opera. However, when it comes to this specific piece of code jq("#i ...