Align the radio button in the center of the input field

I have created custom radio buttons, and the last option includes a radio button with an input field. However, in the Codepen example provided, the alignment of the radio button and input field is not centered vertically. I've attempted various solutions like calculating the top position and using display flex.

Codepen: https://codepen.io/monsmado/pen/RwarYEG

<form>
    <label>Do you have an elevator?</label>
    <div class="custom-control custom-radio">
        <input type="radio" id="elevatorYes" name="elevator" class="custom-control-input">
        <label class="custom-control-label" for="elevatorYes">Yes</label>
    </div>
    <div class="custom-control custom-radio">
        <input type="radio" id="elevatorNo" name="elevator" class="custom-control-input">
        <label class="custom-control-label" for="elevatorNo">No</label>
    </div>
    <div class="custom-control custom-radio">
        <input type="radio" id="elevatorOther" name="elevator" class="custom-control-input">
        <label class="custom-control-label" for="elevatorOther">
        <input type="text" class="form-control" name="elevator" placeholder="Other">
        </label>
    </div>
</form>

Answer №1

To solve this issue, include the following CSS code:

.form-group:nth-child(4) .custom-control-label:after, .form-group:nth-child(4) .custom-control-label:before{
 margin-top: 0.5em
}

I have provided the solution on your CodePen page for reference.

https://codepen.io/r0binxp/pen/qBZbJaZ

Answer №2

To resolve your issue swiftly, you can adjust the top value of the custom radio button to 25% (since its actual height is 50% of the input, this will center it perfectly). It's important to note that using display flex on the parent element won't work as expected because the customized radio button exists within the ::before pseudo-element, preventing it from receiving the flex attribute.

.custom-control-label[for=monthsOther]::before,
.custom-control-label[for=monthsOther]::after {
  top: 25%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
<form>
  <label>How many months?</label>
  <div class="custom-control custom-radio">
    <input type="radio" id="monthsYes" name="months" class="custom-control-input">
    <label class="custom-control-label" for="monthsYes">1-2</label>
  </div>
  <div class="custom-control custom-radio">
    <input type="radio" id="monthsNo" name="months" class="custom-control-input">
    <label class="custom-control-label" for="monthsNo">3-5</label>
  </div>
  <div class="custom-control custom-radio">
    <input type="radio" id="monthsOther" name="months" class="custom-control-input">
    <label class="custom-control-label" for="monthsOther">
        <input type="text" class="form-control" name="months" placeholder="Other">
        </label>
  </div>
</form>

NOTE: It's crucial to override both the ::before and ::after pseudo-elements since the radio button and its background on check action are defined within them.

Answer №3

This method seems quite unconventional for altering the look of form elements. The original question lacks any CSS code (which is crucial for addressing issues), but the linked codepen sheds light on the underlying problems. Aligning elements that are absolutely positioned can be challenging, especially when considering factors like left-to-right orientation or larger font sizes. Furthermore, attempting to visually represent a radio button using a ::before within the label is likely to lead to frustration and ultimately an unsatisfactory outcome.

A more appropriate approach may involve utilizing vertical-align: middle or vertical-align: baseline for both the radio button and label. However, these properties may not yield the desired results when the elements are absolutely positioned.

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

Vue component retrieval on the client side

I am attempting to retrieve a component using Vue with cdn in order to dynamically re-render from a public project that contains a file named cash-sale.vue with the .vue extension. <script> export default { data(){ return { "rows&quo ...

Generate a new div element dynamicaly after every 10 elements are added

I am facing the following scenario: I need to dynamically add an UNKNOWN number of checkboxes to a layout component, arranged in columns with 10 checkboxes per layout item. Although I have successfully added all the checkboxes to a single div element, I ...

The error message states: "change is not defined"

I am encountering an issue where I have 4 input boxes triggering a function on keyup using the onkeyup event. Strangely, I keep receiving an error stating that "change" (the function's name) is not defined. Despite correctly defining the function, I c ...

Pricing determined by location on a website created with HTML

We are looking to customize our HTML5/CSS3 website by displaying different pricing tables based on the location of each visitor. Our goal is to have a fixed price for users from Singapore while showing a different price for visitors from other parts of th ...

Using setTimeout with drag and drop functionality in HTML

I need a setTimeout function to be triggered only after dragging and dropping an image into a division, not before the drop occurs. ...

The fancybox scroll feature is visible when using a browser, but unfortunately, it does not appear on the

I noticed that the scrollbar in my fancybox is not showing up on iPad, but it does display when using Google Chrome on Desktop. You can find the page here. The issue seems to be with the vertical scroll bar not appearing on iPad. Has anyone encountered th ...

The issue arises when trying to use qtip2 in JavaScript upon page initialization

I successfully created a heatmap using JavaScript, utilizing a table with varying colors based on the displayed values' thresholds. To enhance user experience, I wanted to implement a popup window that shows additional information when hovering over a ...

Creating a dynamic table accordion with PHP and MySQL

I am currently working on creating an accordion table to display data retrieved from a database. I want the description data to be shown in a row below when clicking on the respective row. Despite my efforts in modifying various code snippets that I have c ...

Ignore the initial children of the highest level divs exclusively

Is there a way to apply a style to all top-level divs within a div, excluding the first one? I have tried using not(:first-child) but it seems to be recursive. How can this be achieved? <div class='want-to-skip-first-a'> <div class= ...

What is causing my HTML file path to function on my server, but not when I access the HTML file directly?

My file structure is organized as follows: The main file is located in the folder "HTML-Project/index.html". Within this folder, I have my style.css file and two other folders: one for pictures (HTML-Project/pictures) and another for categories (HTML-Proje ...

How come WhatsApp is not recognizing my og:tags correctly?

I'm having an issue with my webpage where the og:tags generated for link previews on Facebook work fine, but they can't seem to be scraped by WhatsApp. Even though the tags are correct in the source code of the site, WhatsApp shows an error messa ...

The scroll() function in jQuery does not bubble up

Recently, I encountered an issue with a Wordpress plugin that displays popups on scroll. The code I currently have is as follows: jQuery(window).scroll(function(){ //display popup }); The problem arises with a particular website that has specific CSS ...

Using Ajax to fetch project data from an API

Currently immersed in a personal coding project, I'm struggling to troubleshoot my code. Essentially, I need to retrieve data from an API and display it on my HTML page. The data type is "jsonp" due to CORS errors that required this workaround for de ...

Using Javascript to Manuever an Element via Arrow Inputs

Currently, I am in the process of developing a Tetris game with a rotating Tetris piece controlled by pressing the space bar. My next objective is to enable the movement of objects to the left and right using the arrow keys. After exploring various simila ...

What is the best way to bring a local package into another npm package and verify its functionality using Typescript?

In a scenario where there are 3 npm projects utilizing Webpack and Typescript, the folder structure looks like this: ├── project1/ │ ├── tsconfig.json │ ├── package.json │ ├── src/ │ │ └── index.ts │ ...

What strategies can I employ to prevent my div tags from shifting positions relative to each other?

I spent time formatting a div tag for my main content and then placed that div inside another wrapper div. However, when I tried to add my logo to the site, everything shifted. It appeared as though the div containing the logo had pushed the wrapper down. ...

Having trouble with the dropdown menu in Bootstrap?

I'm having trouble creating a responsive dropdown menu. When I click on the dropdown items, nothing happens. Here's the code: <div class="navbar-header"> <button type="button" class="navbar-toggel" data-toggel="collapse" data-target="#m ...

Reduce the flexbox container to match the dimensions of its child elements

I have a set of blocks with fixed dimensions that I want to arrange in a grid layout. Using flex-wrap: wrap, I can distribute as many blocks as possible in each row. However, I need the entire wrapped column to be centered on the page, similar to this: ht ...

Retrieving data from a div container on an active website

Imagine having a website similar to , where you want to automatically retrieve the time value every second and save it in a .txt file. Initially, I considered using OCR (optical character recognition) software for this task, but soon realized that relying ...

What is the best way to position a small square box over each td element containing text using css?

Currently, I am in the process of developing an HTML table where I would like to incorporate a square box around each td element using CSS <tr> <td style="width:8%; text-align:center; border: 1px solid #000; margin-left: 30px;"><?php e ...