Yogesh Chauhan's Blog

How to use images instead of HTML radio buttons using CSS?

in CSS & HTML on July 26, 2021

Step 1: HTML

Step 2: Hide radio buttons

To hide radio buttons there are few options such as display or visibility or opacity CSS properties. Using opacity is a better option for a11y.

But, that will leave the space for the element on the page so what you can do is set the position for radio buttons to absolute.

Step 3: Make images clickable

Radio buttons you can select automatically. With image being the face of the radio buttons, we need to make images clickable.

The trick is to set the cursor property value to pointer.

We can use adjacent sibling combinator (+) to select an img element that is directly after the input radio.

At the same time, let’s add some margin-right to add space between the images.

Step 4: Make selection visible

As you can see the radio buttons are getting clicked but the clicks are not visible. Let’s add a CSS rule for :checked radio buttons.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How does AdSense calculate page loading time?JavaScriptColleague UI Basics: The Context AreaColleagueSolution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)MiscellaneousHow to check if a link has http or https in it in JavaScript?JavaScript5 Ways to Loop Through JavaScript ArraysJavaScript3 JavaScript methods to use when you want to go back and forth in historyJavaScriptCreate dynamic selectors using SCSS (Sass)SCSSHow to convert an object from API to JSON array in Angular 9?AngularHow to Validate User Name, Email Address and URL in PHP?PHP2 Ways We Can Write Multiple Line Commands in PHPPHPControl rendering using CSS content-visibility propertyCSSThe SQL UNION OperatorSQL/MySQL