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: https://i.sstatic.net/2g5w8.jpg

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

Customize WPBakery Accordion Background Color

I'm currently using WPBakery's Accordion section, version 5.4.7, to showcase a Gravity Form. I am attempting to modify the background color of the active accordion section to black. After exploring various forum examples, I have attempted to add ...

Data is not displaying correctly in the Angular Material Table

I'm currently trying to build a mat table based on an online tutorial, but I'm facing a problem where the table appears empty even though I have hard coded data. As someone new to Angular and HTML/CSS, I'm struggling to understand why the ma ...

How can I load a function from the HTML file that is being loaded using .load() in jQuery?

Within my main window main.html, there is a div button that, when clicked, loads another html file into a large div. This is achieved using the .load() function: $('#mainpanel').load("search.htm"); The "search.htm" file contains a function cal ...

Bootstrap 4 alert boxes extend the text to span the entire width of the alert

How can I make the paragraph text span across most of the box width in Bootstrap 4? <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJ ...

Tips for preventing words from being split between lines in an error message

I am encountering a problem in Angular 2 where error messages are being split onto two lines when there isn't enough space for the entire word. Check out this screenshot for reference Is it possible to prevent words from being divided across lines? ...

Analyzing JSON information and presenting findings within a table

Requesting user input to generate a JSON object based on their response. Interested in showcasing specific details from the object in a table format for user viewing. Questioning the efficiency and clarity of current approach. My current progress: In HTM ...

Arrange css3 menu circles to be displayed next to each other

I have managed to figure out most aspects of these circles except for how to arrange them side by side. Currently, they are stacked on top of each other. I have successfully determined the colors, fonts, text positions, and other details with some assistan ...

What's the reason for not being able to customize classes for a disabled element in Material-UI?

Currently, I am utilizing Material-UI to style my components. However, I am facing challenges when trying to customize the label class for disabled buttons. Despite setting a reference as "&$disabled", it does not yield the desired results. import Rea ...

The onload function in jQuery is not functioning properly when the page is refreshed

I'm just starting out with jquery and I have a simple project in mind. The idea is to have two pictures stacked on top of each other, but I want the page to start showing the second picture first (at a specific scroll point). Then as the user scrolls ...

Ensure that newly added elements are aligned with the settings of the slide's

I've encountered an issue with my jQuery slider that affects a div's CSS on slide. Everything works as expected, except when I add a new div, the settings from the slider aren't applied to the new div until the slider is moved again. HTML ...

Color picker can be utilized as an HTML input element by following these steps

After trying out various color pickers, I was not satisfied with their performance until I stumbled upon Spectrum - The No Hassle jQuery Colorpicker. It perfectly met my requirements. <html> <head> <meta http-equiv="content-type" content="t ...

What are the differences between ajax loaded content and traditional content loading?

Can you explain the distinction between the DOM loaded by AJAX and the existing DOM of a webpage? Is it possible to load all parts of an HTML page via AJAX without any discrepancies compared to the existing content, including meta tags, scripts, styles, e ...

A peculiar quirk with Nuxt.js radio buttons: they appear clickable but are somehow not disabled

I’m having an issue with a radio button that won’t check. It seems to be working fine on other pages, but for some reason it just won't click here. <div class="form-group"> <label class="control-label&q ...

Why isn't my CSS button changing color on hover?

div ul li a.button:hover { text-color:#FF0000; background: #0040FF; } I have been attempting to create buttons that change color and text when hovered over. Despite trying different methods, the changes do not seem to be taking effect. Below is the co ...

Issue with internal URI directory structure affecting SEO mod_rewrite

I recently implemented a mod_rewrite rule on my website that transforms example.com/en/about/topic into example.com/en/view.php?p=pages/about/topic.php The specific rule I used is: RewriteRule ^en/([a-zA-Z0-9/]+)$ en/view.php?p=pages/$1.php In view.p ...

How to locate a div element using CSS selector in Selenium WebDriver with Ruby

My experience with CSS selectors in selenium webdriver has been positive, but I am facing difficulty in locating the element within the following div. <div class="class1 class2" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_78891a3 ...

Successfully changing the source and tracking of a video through an onclick button, but the video content remains unchanged

I apologize if this post is a duplicate, but I couldn't find the answer in previous threads. I'm attempting to change the video source using an onclick button. However, even after changing the source, the video remains the same. <video width ...

two adjacent elements filling the entire height of the window at 100% dimensions

Is there a way to place two elements side by side, with one of them (the textarea) always being 100% height of the window? I've looked at similar questions here but can't figure out how to make it work. Can you help me with this? html: <div ...

How can you vertically center an icon button in Material UI?

Looking for help with aligning elements in this code snippet: <TextField id="outlined-basic" label="22Keyword" defaultValue={"test123"} variant="outlined" /> <IconButton aria-label="delete&q ...

Identifying the position of an element as the first, nth, or last child within its parent using ReactJS

I'm currently experimenting with dynamic functionality in ReactJS and I need to determine the position of a child relative to its parent. How can I achieve this in ReactJS? I have come across this post which discusses detecting relationships between ...