Radio button - arranging the background image and text in alignment

I'm struggling to align my custom radio buttons alongside text. Here is an example image:

Despite using CSS for styling, I am unable to properly align the radio buttons. Below is a snippet of my HTML:

label.item {
  display: inline-block;
  position: relative;
}
label.item > input {
  visibility: hidden;
  position: absolute;
}
label.item > .radio-image::before {
  content: url("../tools/unchecked.png");
}

label.item > input[type="radio"]:checked + .radio-image::before {
  content: url("../tools/checked.png");
}
label.item > input[type="radio"]:hover + .radio-image::before{
  cursor: pointer;
}
<div class = "container">
  <div class = "form-group">
    <label>Body</label>
    <label class="item">
      <input type="radio" value="Good/Minor Flaws" name="size" required="required" /> 
      <div class="radio-image text-center">Good / Minor Flaws</div>
    </label>
    <label class="item">
      <input type="radio" value="Major Damage" name="size" required="required"/> 
      <div class="radio-image text-center">Major Damage </div>
    </label>
  </div>
</div>

https://jsfiddle.net/hollzpn7/#&togetherjs=ZHGRhnkYVK

Answer №1

Here is a solution that may be useful for you: https://jsfiddle.net/h2q7y4uj/4/

div.item {
  display: inline-block;
  position: relative;
}

div.item > input {
  visibility: hidden;
  position: absolute;
}

div.item > .radio-image::before {
  content: url(http://via.placeholder.com/50x50);
}

div.item > input[type="radio"]:checked + .radio-image::before {
  content: url(http://via.placeholder.com/51x51);
}

div.item > input[type="radio"]:hover + .radio-image::before {
  cursor: pointer;
}

.radio-image {
  position: relative;
  width: 200px;
}

.radio-image > span{
  position: absolute;
  top: 45%;
  transform: translateY(-50%);
}
<div class="container">
  <div class="form-group">
    <label>Body</label>
    <div class="item">
      <input type="radio" value="Good/Minor Flaws" name="size" required="required" />
      <div class="radio-image text-center">
        <span>Good / Minor Flaws</span></div>
    </div>
    <div class="item">
      <input type="radio" value="Major Damage" name="size" required="required" />
      <div class="radio-image text-center">
        <span>Major Damage</span> </div>
    </div>
  </div>
</div>

Feel free to use this as needed!

Answer №2

Simply make changes to this CSS snippet:

.radio-image {
            display: inline-block;
            line-height: 30px;
}

label.item>.radio-image::before {
           content: url("../pictures/unchecked.png");
            float: left;
            line-height: 30px;
            display: inline-block;
            padding-right: 5px;
 }

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

Identifying whether a Alphabet or a Digit has been Pressed - JavaScript

I understand that it is possible to detect if a key has been pressed and identify which key was pressed using JavaScript. In order to check if a key is down or pressed, jQuery can be utilized with ease: $( "#some id" ).keydown(function() or $( "#m" ). ...

The -webkit-background-clip feature seems to be malfunctioning in Safari

I am experiencing an issue where the code works perfectly on Chrome but fails to function properly on Safari. I have been unable to pinpoint the cause of this discrepancy and any assistance or insights would be greatly appreciated. For those interested, a ...

Implementing multiple content changes in a span using javascript

Having an issue with a pause button where the icon should change on click between play and pause icons. Initially, the first click successfully changes the icon from a play arrow to a pause icon, but it only changes once. It seems like the else part of the ...

Is there a way to change the fill color of an SVG circle using Thymeleaf?

I am struggling to change the background color of a circle in an SVG image on my HTML page using Thymeleaf. I attempted to use inline style tags, but it resulted in the circle's background color turning black. <svg id="svg" height="50" width=" ...

Combining two flex elements with auto-growing capabilities in react-native

I am excited about using react-native to have a TextInput aligned with a MaterialIcons.Button in the same line. I have been trying to center these elements horizontally but haven't been successful with the code below: import React from 'react&apo ...

HTML5 - Ajax - puzzling behavior that I am unable to comprehend

Need Help Clarifying My Issue: I currently have a Div with Page 1 content. Page 1 contains a button that transitions the div content to Page 2. Page 2 has a button that switches the div content back to Page 1. The Problem: If Page 1 is loaded first, t ...

What is the best way to create titles with a background?

My goal is to have a title overlay an image with specific width and the text displayed in blocks. To better illustrate, here's an example: I prefer to achieve this effect using CSS; however, I am open to utilizing Javascript if needed. ...

What is the reason behind why decimals don't automatically pre-fill like integers do?

Seeking help with an HTML issue - I have a field pre-filled with a decimal number, but it's not displaying the value when loaded. The field should accept both integers and decimals, but only integer values are showing up correctly. I've spent ho ...

VSCode - when indenting, spaces are added around strings

Currently, I am utilizing Vue 3 along with Prettier in VS Code. One issue I am encountering is that when a string is on its own line, it is formatted correctly to my liking. However, the problem arises when my browser in Dev Tools renders strings with extr ...

What is the best way to incorporate a percentage-based width scrollbar?

Help needed: I want to implement a scroll feature for the parent div but here's the catch - the width of the text inside the div needs to be in percentage. So, if there is a long name, it should scroll within the parent div. HTML: <div id="list"& ...

Having trouble retrieving the default selected value using the index in Angular Material's mat-button-toggle functionality

I'm encountering an issue with setting the default value for a toggle button group. The code is simple and the toggle buttons are correctly fetching values from the index, but I can't seem to get one of them to be default selected. I tried settin ...

What are the steps to create a hamburger drawer menu that slides out to the right?

After making the modifications specified in the comments labeled 'modify' for this codepen, the result is that the hamburger icon moves to the right and slides from the right as intended. However, I would like the menu content to also slide out ...

The horizontal scrollbar in Chrome is unexpectedly activated, despite elements being set to occupy the full screen width (100vw) using Tailwind and NextJS 13.4+

It took some time to identify the root cause of this issue within a larger project. Hopefully, this Question and Answer will be beneficial to someone else. Here is the scenario -- in a NextJS/Tailwind project, there is only one large vertical element on t ...

Tips for resizing a larger image to display only a specific portion in CSS (and incorporating JS if needed)

I need to resize an image that measures 1024x1024 and is segmented into 4 quadrants: My goal is to reduce the size of this image so that quadrant 2 is 256x256 while masking out or hiding the remaining 3 quadrants, displaying only the desired section on th ...

Navigating between different pages using react-router-dom version 6.3

Currently in the process of refactoring a website and updating the rrd to v5, since the old version had components that no longer exist. We now need to work with the new component, as many are aware. Previously, I utilized framer-motion for transitioning ...

The surprising way Next.js disabled the production build and transformed my rgba color into hsla

I created CSS code to display grid patterns like this: background-image { repeating-linear-gradient( 0deg, rgba(255, 255, 255, 0.43) 0px 1px, transparent 1px 20px ), repeating-linear-gradient( 90deg, rgba(255, 255, 255, 0.43) 0px 1px, transparent 1px 20 ...

Bootstrap allows for word wrapping on either one or three lines

I am currently utilizing Bootstrap 3 for my website, and I have a sentence composed of three parts at a certain point on the page. For example, it might read "Bud Spencer walks with Terence Hill." where X="Bud Spencer", Y="walks with", Z="Terence Hill." T ...

Attempting to reach <p> from numerous web pages

Currently, I am working on a project where I need to extract text data from various websites. I have successfully managed to retrieve the text data using <p> tags. I would really appreciate any assistance with this task. Thank you! ...

Fixing the slide bar in React using styled components

In the early stages of creating a slider where cards (divs) can be moved left and right with a click, I encountered an issue. The onClick handler is functioning properly. However, upon running the project, I noticed that the cards start 230px away from the ...

I'm curious as to why my form fields disappear even when they have not been selected

Illustration of Form Fields Disappearing When Unselected Greetings, I'm encountering an issue where my form fields disappear when not selected. I'm utilizing Crispy Forms to render the form, following a tutorial on YouTube. This functionality w ...