Add a border to the image when the radio button is selected

I am attempting to add a border to an image when a radio button is checked.

Here is the HTML code I am working with:

<div class="frm_radio">
  <label for="field_n9r1a2-0">
    <input type="radio" name="x" id="t" value="Betreuung">
    Betreuung 
    <img src="/wp-content/uploads/2016/03/unterichten_betreuen.jpg">
  </label>
</div>

I have tried using the CSS selector :checked but it does not seem to be working as expected.

input[type=radio]:checked img {
    border: 2px solid red;
}

Could someone provide guidance on how to properly solve this issue?

Answer №1

Remember to include the ~ (sibling) operator in your CSS code:

input[type=radio]:checked ~ img {
    border: 2px solid red;
}

If you omit the ~, the browser will interpret the radio button as the parent of the image element. In such cases where no text is present, consider using the + selector instead.

input[type=radio]:checked ~ img {
    border: 2px solid red;
}
<div class="frm_radio">
  <label for="field_n9r1a2-0">
    <input type="radio" name="x" id="t" value="Betreuung">
    Betreuung 
    <img src="/wp-content/uploads/2016/03/unterichten_betreuen.jpg">
  </label>
</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

Steps for adding a Bootstrap Navbar Dropdown1. Begin by creating

Looking for tips on how to incorporate a dropdown navbar using Bootstrap 4. I've utilized the .navbar-bottom class to achieve a horizontal layout.** The .navbar-bottom class is used to include a scrollbar in the navbar** .navbar-bottom { overflow ...

Animating a div using a changing scope variable in AngularJS

As a newcomer to Angular, I am looking to add animation to a div element using ng-click within an ng-repeat loop. Here is what I have attempted: app.js var app = angular.module( 'app', [] ); app.controller('appController', function($ ...

Position a div element after another using the :after pseudo-element

My goal is simple to explain, but I have exhausted all my efforts trying to achieve it. I am hoping for the ★ symbol to appear immediately after the number 06 using jQuery. Any assistance would be greatly appreciated. The numbers are generated by a s ...

The "initialized" event in angular2-tree-component fires prior to the data being loaded

Utilizing the angular2-tree-component, my goal is to display an already expanded tree. According to Angular docs, the initialized event should be used for expanding the tree after the data has been received: This event triggers after the tree model has ...

Searching for "available accommodations" requires guidance on logic

Imagine you have a website where users can search for "hotel rooms to rent". I am not familiar with how the current systems operate, so I am reaching out for guidance. Currently, the form on my website has two fields: Date available from: //for example ...

Centered at the bottom of the screen lies a Bootstrap button

As a beginner with bootstrap, I am currently exploring new concepts and experimenting with different features. One thing I am attempting is to center a bootstrap button at the bottom of the screen. Here is my current code: .bottom-center { position: a ...

JavaScript is unable to bring in an npm module

Having trouble importing an npm module using import fs from 'fs'; in my main.js file that is linked with index.html. The script tag connecting the JS file has the attribute type="module". However, the browser console shows the error: Un ...

Creating a centered vertical border in Tailwind between two divs can be achieved by following these steps

Hey there, I'm working on creating a timeline and I'd like to have a line intersecting each year similar to this example. My tech stack includes tailwind CSS and next.js. Although I've written some code, I can't seem to get the line ce ...

Achieving a full-width navigation element allows for other elements to be positioned below it, rather than directly beside it

Apologies for my not-so-great English. To better understand my issue, please refer to this fiddle and this fiddle. Focus on the <nav></nav> section. If there are only a few elements in the menu, the next block <section></section> ...

What is the best way to retrieve data from a Python file and display it in HTML?

I am struggling to transfer data from my Python file to my HTML. Below is the form I am using: <div class="form-group "> <label class="col-sm-3 col-sm-3 control-label">IP Address: </label> <div class="col-sm-9"> & ...

The webpage freezes when attempting to run jQuery with Selenium

I'm currently facing an issue where my selenium script hangs the webpage whenever I try to find an element using jQuery. The script doesn't execute and a pop up appears in the browser with the message "A script on this page may be busy, or it may ...

Using Python's Beautiful Soup library to retrieve text from h1 and id tags

I've been attempting to retrieve the text from an HTML id called "itemSummaryPrice," but I'm having trouble figuring it out. html = "" <div id="itemSummaryContainer" class="content"> <div id=&quo ...

HTML5 Audio Recording Tools (including a built-in Audio Player)

While exploring Universal HTML5 players such as , I have noticed that none of them seem to include a record button. Is there any player out there that has one, similar to the image below? https://i.sstatic.net/Lpg2s.png ...

Animating the appearance and behavior of radio buttons using CSS transitions and JavaScript

My jQuery code allows me to fetch attributes from the .item element, but I'm having trouble with my CSS transitions not working as intended. To see the desired transition effect, try replacing .item with .item-tile. How can I make my CSS transitions ...

Navigate to a hyperlink using an HTML tag on an Android device

Hi there, I have an HTML string that includes a tag with a href attribute. After setting this HTML string in a text view on Android, I want only the tag with the href attribute to be clickable and redirect to the appropriate link. Can someone please advise ...

What are some ways to customize the appearance of my ReactJS application while it's being viewed on Chrome on

I'm currently in the process of running my ReactJS app on a Panasonic Vierra Smart TV browser. After importing core-js and babel-polyfill, I managed to load the webpage but encountered an issue with the UI alignment. The styling does not display corre ...

Is my utilization of Flexbox and media queries incorrect?

Currently, I am practicing flexbox and breakpoints in material UI using React.js and I am attempting to achieve a layout similar to this: https://i.sstatic.net/vAkdo.png My goal is to have the product images and type displayed in columns, while the produc ...

Analyzing an HTML document

My current challenge involves extracting the username from an HTML Page based on who is accessing it. The username is displayed on the page, but its location may vary depending on the user. I'm curious to hear how others would approach this issue. My ...

Calculate the total sum of selected values in a multiple select dropdown using jQuery

Is there a way to calculate the sum of selected items in a multiple selection dropdown menu? For instance, if I select "X12SO" and "X13SO", their values should add up to 30. let total = 0; $("select[name='myselect[]'] option").each(function(){ ...

Arrange content from database in collapsible Bootstrap 4 cards

Hey there! I'm currently working on developing a shopping cart for a website that will contain products ordered from different companies. You can see an example here: screenshot My goal is to group all products from the same company together, creatin ...