Choosing a Radio Button with a Labeled Option

I'm experimenting with a method to style radio buttons by concealing them and inserting an alternative image as the background of the corresponding label (similar to this technique). In my test, I attempted to apply CSS to the radio button label:

input[type="radio"] + label {
    display:inline-block;
    width:19px;
    height:19px;
    margin:-1px 4px 0 0;
    vertical-align:middle;
    background:#000;
    cursor:pointer;
    color:red;
}

Regrettably, none of the styles from the aforementioned CSS are affecting the label.

The radio buttons are generated using the following:

<%= f.input :trademark_search, as: :radio_buttons, label: 'Would you like us to do a trademark search and provide advice regarding any issues we identify in relation to the name you have selected?', input_html: { class: 'form-control' } %>

This code renders the following HTML:

<span class="radio">
  <label for="incorporation_trademark_search_true">
    <input id="incorporation_trademark_search_true" class="radio_buttons optional form-control" type="radio" value="true" name="incorporation[trademark_search]">
    Yes
  </label>
</span>
<span class="radio">
  <label for="incorporation_trademark_search_false">
    <input id="incorporation_trademark_search_false" class="radio_buttons optional form-control" type="radio" value="false" name="incorporation[trademark_search]">
    No
  </label>
</span>

I am determined to discover what I might be overlooking here. Any insights would be greatly valued.

Answer №1

One useful CSS selector to know is the + symbol, which selects a sibling element that immediately follows another.

Here is an example of how to correctly use this selector:

div {
  background: red;
  height: 10px;
}
div + span {
  display: block;
  height: 10px;
  background: blue;
}
<div></div>
<span></span>


On the other hand, attempting to select a parent element using the + symbol will not work, as demonstrated below:

div {
  background: red;
  height: 10px;
}
div + span {
  display: block;
  height: 10px;
  background: blue;
}
<div>
  <span></span>
</div>

As shown above, selecting a parent element with the + symbol does not produce the desired result.


In this last demo, I added an <i> element in between the div and span elements to illustrate that the + selector only targets immediate sibling elements and cannot skip over others:

div {
  background: red;
  height: 10px;
}
div + span {
  display: block;
  height: 10px;
  background: blue;
}
<div></div>
<i></i>
<span></span>

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

Stunning CSS Image Showcase

When hovering over my image gallery, it works fine. However, I would like the first image in the gallery to automatically appear in the enlarged section instead of displaying as a blank box until hovered over. <!DOCTYPE html> <html lang="en"> ...

Form data is not being displayed by the Private Params method

Currently, I am in the process of developing a playercard feature that serves as a profile page for users. The hurdle I am facing lies within my backend code where my private method, playercard_params, is solely returning the user_id instead of all the inf ...

Avoid the resetting of chosen value following a POST or submission

I am currently working on creating a "web dashboard" that requires a year_from parameter from an HTML form. Despite setting up my Flask backend to return data within this specified range, I am encountering a problem where every time I hit submit, the selec ...

What is the best way to insert HTML elements in front of other HTML elements using a form and jQuery?

I am looking to insert new HTML elements before existing ones using a form and jQuery: Here is the initial HTML Code: <div class="main-content"> <h2 class="main-content-header">Projekte</h2> <div class="project-content"> ...

Struggled to Find a Solution for Code Alignment

Is there a tool or software that can align different types of codes with just one click? I've tried using the Code alignment plugin in Notepad++, but it hasn't been effective. For example, when aligning HTML code using tags, I couldn't find ...

"Utilizing an ellipsis within a span element in a table cell to achieve maximum cell

Update Note: Primarily targeting Chrome (and maybe Firefox) for an extension. However, open to suggestions for a universal solution. Additional Information Note: Discovered that lack of spaces in text for A causing overflow issues with B. Situation seem ...

What factors must be considered before transitioning from an HTML4 web application to HTML5?

As a newer web apps developer, I've created various applications when HTML 5 was on the rise as the new standard. Now, I'm considering migrating some of those apps to HTML 5 and wondering if it's a good idea. Despite my experience in web d ...

Which tag should I use, <b> or <mark>, to emphasize important marketing terms within a paragraph?

Trying to emphasize specific keywords in a paragraph of text can be challenging. I'm currently puzzled about the distinction between the <b> and <mark> tags. These words are highlighted because they play a crucial role in marketing, prompt ...

Determine the vertical dimension of a child division once it has been integrated into an HTML document

My goal is to create a website with multiple pages without having to recreate the toolbar for each page. To achieve this, I have created a separate HTML file and CSS file specifically for the toolbar. On each page, I simply import the toolbar using the fo ...

Enhance your message inbox experience with jQuery/Javascript features inspired by Gmail, including the ability to select all messages with a checkbox and

It is truly satisfying to be here working on developing a private message inbox for my website, especially after successfully implementing a complete user signup/login and activation system. A few months ago, I never believed I had enough patience to grasp ...

What could be causing Prettier code formatter to suddenly stop formatting in VS Code?

I've been having trouble formatting my code using Prettier in VS Code. Even after reinstalling it, the issue persists and I can't seem to format my HTML/CSS code properly. The screenshot I provided shows that the code remains unformatted even aft ...

Should I use several if get statements on one page, or spread them out across multiple pages in PHP structure?

After working with PHP for a few years, one of my ongoing dilemmas is how to best structure my pages. Should I follow the current approach: if(getValue('action') == "jobsFilter" && getValue('jobType') == "open") ...

What is the best method for incorporating CSS into an Angular application's embedded PowerBI report, which is displayed

Is there a way to incorporate external CSS or another method to apply styles to a PowerBI report embedded in an Angular application as an iframe? Specifically, I want to remove the scrollbar to ensure that the report seamlessly fits within the angular page ...

Verifying the angular form without the need for a submit button

I'm currently utilizing angular.js form validation and I'm looking to implement validation without the use of a form submit button. Below is my HTML code: <form name="userForm"> <input type="hidden" ng-model="StandardID" /> < ...

Tips for centering links in the navigation bar on Bootstrap 5 when viewing in a small size

I had previously inquired about customizing the Bootstrap 5 navbar in this post: Adjust navbar height to be smaller and I received some helpful answers. Thank you to everyone who responded. However, I now have a follow-up question: I am interested in ch ...

Position the text so that it is aligned below and to the right of the text box

I'm currently working on a project in ASP.NET that involves a resizable textbox with a character limit display positioned below and to the right of the box. The challenge I am facing is ensuring this text remains properly aligned as the size of the te ...

How can you determine the class of an element that was clicked within an iframe?

Is it possible to retrieve the class of an element that is clicked within an iframe? Here is the HTML code: <input id="tag" type="text"> <iframe id="framer" src="SameDomainSamePort.html"></iframe> This is the JavaScript code: $(docum ...

Unable to switch the text option

[Fiddle] I'm currently working on a project where I want pairs of buttons to toggle text by matching data attributes. While I can successfully change the text from "Add" to "Remove" on click, I am facing an issue with toggling it back to "Add" on the ...

The problem of Twitter Bootstrap 2 top navigation dropdown not functioning properly on mobile devices

I’ve spent a significant amount of time researching the issue prior to reaching out here, but unfortunately I haven’t been able to find a solution yet. Website Link: Currently, I am using Twitter Bootstrap 2.3.2 and have integrated a form in the drop ...

Eliminate any unnecessary gaps that may exist between the cards within the deck

Utilizing Jinja2 with Flask to render a list on an HTML page, I aim to produce card decks containing data from the list using a Jinja2 loop. This is my current HTML code: <div class="container-fluid"> <div class="row"> <div clas ...