Tips for aligning the dot in the middle of a radio input box

I have developed a reusable Radio group component, and I am using styled components to style it.

The goal is to position the dot right in the center of the circle, and it is somewhat achieving that in the screenshot below:

https://i.sstatic.net/Kistg.png

However, it seems like the positioning is slightly off to the bottom and right.

Moreover, I believe there is room for improvement in the styling:

import { StyledRadioButton, Radio, Wrapper } from './radio-button.styles';

...

  return (
    <Wrapper>
      <Radio type="radio" id={id} disabled={disabled} {...rest} />
      <StyledRadioButton htmlFor={id}>{label}</StyledRadioButton>
    </Wrapper>
  );

Styled:

export const Wrapper = styled.div`
  display: flex;
  gap: 1rem;
  margin-top: 0.5rem;
  margin-bottom: 0.5rem;
  align-items: center;
`;

export const Radio = styled.input`
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
  width: 1.5em;
  height: 1.5em;
  border: 1px solid grey;
  font-size: 16px;
  font-style: normal;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.1s ease-in-out;
  ::after {
    content: '';
    display: block;
    border-radius: 50%;
    width: 0.75em;
    height: 0.75em;
    margin: 4.26px; // this is the tweak to position it into the center
  }
  :checked {
    ::after {
      background-color: green;
    }
    border: 1px solid green;
  }
  border: 1px solid grey;
`;

export const StyledRadioButton = styled.label<{ disabled?: boolean }>`
  font-weight: 400;
  font-size: 16pc;
  color: black;
`;

Answer №1

Nowadays, utilizing CSS (inline-)grid makes it a breeze to center content effortlessly. Gone are the days of having to turn the radio::after into a block and employing margin for 'dot' realignment.

I revamped your code into a functioning snippet and:

  • incorporated
    display: inline-grid; place-items: center;
    into .radio
  • deactivated display and margin in .radio::after
  • crafted a responsive demonstration for experimentation
  • adjusted the radio size and border based on parent font-size.

.wrapper {
    font-size: var(--defaults-radio);
}

.radio {
    /* Disabling defaults */
    -webkit-appearance: none;
    appearance: none;
    margin: 0;

    width    : 1.5em;
    height   : 1.5em;
    font-size: var(--defaults-radio, 1em); /* starting from 16px, with fallback */
    font-style: normal;

    border-radius: 50%;
    border: 0.0625em solid grey; /* 0.0625em = 1px */

    cursor: pointer;
    transition: all 0.1s ease-in-out;

    display: inline-grid; place-items: center;
}
.radio::after {
    content: "";
/*    display: block; /**/
    border-radius: 50%;
    width : 0.75em;
    height: 0.75em;
/*    margin: 4.26px;/**/
}
.radio:checked {
    border-color: green;
}
.radio:checked::after {
    background-color: green;
}

/* Additional adjustments for the demo */
input[type="range"] { width: 100% }
label>input         { display: block; font-size: 1em }
label               { margin-bottom: 0.5rem }
<label><b>radio size</b>&nbsp;-&nbsp;[0.5 - 5em] <span id="info-radio"></span>
    <input type="range" min="0.5" max="5" step="0.5" value="1"
           oninput="document.body.style.setProperty('--defaults-radio', this.value + 'em');
                    document.getElementById('info-radio').innerHTML = ' > ' + this.value + 'em';">
</label>
<br>
<div class="wrapper">
    <input class="radio" type="radio" name="group">&nbsp;<input class="radio" type="radio" name="group" checked>
</div>

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

Tips for integrating AsyncGenerators with Kotlin/JS

I am currently exploring the use of IPFS with Kotlin/JS, but my issue is not limited to that specific context. The functions ipfs.cat() and ipfs.get() return an AsyncGenerator, and I am uncertain about how to properly iterate over it using Kotlin (I am als ...

Utilizing Angular Filters to Assign Values to an Object within a Controller

I have a JSON example with multiple records, assuming that each name is unique. I am looking to filter out an object by name in the controller to avoid constant iteration through all the records using ng-repeat in my HTML. { "records": [ { "Name" : ...

Is it possible to incorporate HTML and CSS into Kivy and Python 3 development?

Currently in the process of creating a multi-touch kivy program using Python 3.52 on Linux. While Kivy is effective, I've encountered challenges with GUI development and experienced laggy animations. I've noticed that my program tends to slow do ...

Experiencing Issues with the Email Button Functionality on My Website

I am looking to create an "Email" button that will send the user-entered text to my email address. Here is the code snippet: <form method="post" action="malto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cb ...

Issue with sticky navbar in parallax design

I have been working on a website where all pages feature a header and a navbar located just below it. As the user scrolls down the page, I want the navbar to stick to the top of the screen once it reaches that position. However, on one specific page, I am ...

Regular expression in Javascript to match a year

I'm still learning javascript and I have a question. How can I determine if a specific piece of text includes a four digit year? Here's an example: var copyright = $('#copyright').val(); if \d{4} appears in copyright: take ac ...

Searching recursively for keys with empty values in an array using Javascript

I've written a function that recursively locates empty values in a nested array. The function initially produces the correct value, but it seems to reset it to the input value before returning the result. What could I be overlooking? Below is my co ...

What could be causing the malfunction of the map function, despite the code appearing to be correct?

I have encountered an issue in my React functional component where I am trying to display a list of notifications. The useEffect() function is being called to generate the "data" which should then be displayed on the page. The code seems to be running fine ...

Choosing elements for react select within a modal using SeleniumIn this guide, we will discuss

In my current project, I am using ReactJS and Selenium WebDriver with Java. The task at hand involves the following steps: Click on a button (completed) This action triggers a modal containing a react select component The goal is to select an elemen ...

Exploring the Interaction of Users with HTML Table Cell <td>

I am currently analyzing the code for a web page. Within this code, users have the ability to double-click on a cell in a table (<td> as shown below) and input a value. Is there a specific attribute or element within this HTML that indicates user in ...

It is impossible to resolve Npm vulnerabilities

My journey with learning React began when I decided to create my first app using the command: 'npx create-react-app my-app' However, upon building the app, I encountered a warning in the terminal: 22 vulnerabilities (9 moderate, 13 high) In ...

Change the background color of all cells in a Bootstrap table by hovering over a single cell

Here is the code for a bootstrap table: <body leftmargin="0" topmargin="0" bgcolor="#ffffff" marginheight="0" marginwidth="0"> <div class="container-fluid h-100"> <div class="row float-right align-items-center" style="height: 5%;"> ...

Different dimensions of the <select> element with the help of Ryan Fait's jQuery design script

New to java and struggling on my own, I am seeking advice on how to customize Ryan Fait's jQuery input styles to accommodate 2 unique types of elements. For the code reference, please visit: While the code is efficient, I am unsure how to create two ...

Extracting the token from a cookie for a server-side API request in Next JS: A guide

After following the guidelines provided in the Next.js repository to configure an Apollo client, the resulting code structure is as follows: import { ApolloClient } from 'apollo-client' import { InMemoryCache } from 'apollo-cache-inmemory&a ...

Retrieve the response retrieved from AJAX/jQuery and insert it into the designated form input field

When a user inputs an address into this form and clicks on the verify address button, the following script is executed: $('#verify').click(function () { var address = $('input[name=address]'); var city = $('input[name= ...

Error encountered with Jest: SyntaxError - React Navigation - Unexpected export token found in [node_modules eact-navigationsrc eact-navigation.js:1]

Error Encountered with Jest:- The main issue arises from react navigation. There is an error occurring in jest related to the react-navigation node_modules folder. Error at: node_modules\react-navigation\src\react-navigation.js:1 Error Det ...

I want to develop a Shopify app that engages users directly from the product detail page. How can I go

Hi there, I am currently working on developing a Shopify app. Once the app is installed, I aim to have a button displayed on the selected product detail page that prompts users to answer some questions, with the collected data being stored in a database. ...

Manipulate the content of any webpage without using external extensions by injecting your own HTML, CSS, and JavaScript code

I am currently following a tutorial on how to draw shapes on a local webpage using Canvas. I came across this helpful article on Html5 canvas paint. Everything is going smoothly, but now I am interested in replicating the drawing functions/CSS/HTML and "i ...

Unpacking Functions within Embedded Redux Reducer

My current challenge involves dispatching an action that has the following structure: { type: "TOGGLE_FARA", fara: true, id: "5d20d019cf42731c8f706db1" } The purpose of this action is to modify the "enabled" property within my "fara" state. The configura ...

Looking to create an Ajax Control Toolkit AutoCompleteExtender with results that are "similar"?

The Ajax AutoCompleteExtender is all set up and functioning properly, linked to a webservice that fetches results from SQL. Now, I want to enhance the user experience by providing similar results in case they can't recall the exact name of what they& ...