Using the :checked pseudo-class in CSS allows for specific styling changes to occur when a radio button is

Using the code snippet below, I can modify the CSS of any adjacent div element when a checkbox is selected:-

input[type="radio"]:checked ~ div{
  display:none;
}

My goal is to target a specific div by either its Id or class when a particular radio button is checked.

I am attempting to achieve this using only CSS.

I have tried the following approach:-

input[type="radio"]:checked ~ #one.div{
display:none;
}

https://codepen.io/dyk3r5/pen/WOmxpV?editors=1111

<div class="container">


   <input type="radio" id="opt1" name="radiolist" checked>
   <label for='opt1'>New Account</label>


   <input type="radio" id="opt2" name="radiolist">
   <label for='opt2'>Existing Account</label>

  <div id="one">Lorem Ipsum 1</div>
  <div id="two">Lorem Ipsum 2</div>

</div>

html, body{
  height :100%;
}

.container{

  display:flex;
  justify-content:center;
}

label{
  color:#000;
  font-weight:600;

  height: 25px;
  padding: 5px 10px;
  border-bottom: 2px solid lightgrey;
  margin: 5px 1px;

}

input[type="radio"]{
  display:none;
}

input[type="radio"]:checked + label{
   border-bottom: 2px solid red;
}

input[type="radio"]:checked ~ div{
display:none;
}

Answer №1

After making adjustments to the CSS, I successfully implemented a feature that displays the corresponding div when a specific radio button is selected. However, there was an issue with the selector for the div: #one.div. This selector targets the element with id "one" and class "div", but it should actually be written as div#one.

html,
body {
  height: 100%;
}

.container {
  display: flex;
  justify-content: center;
}

label {
  color: #000;
  font-weight: 600;
  height: 25px;
  padding: 5px 10px;
  border-bottom: 2px solid lightgrey;
  margin: 5px 1px;
}

input[type="radio"] {
  display: none;
}

input[type="radio"]:checked+label {
  border-bottom: 2px solid red;
}

input[type="radio"]~div {
  display: none;
}

input[type="radio"][id="opt1"]:checked~div#one,
input[type="radio"][id="opt2"]:checked~div#two {
  display: block;
}
<div class="container">


  <input type="radio" id="opt1" name="radiolist" checked>
  <label for='opt1'>New Account</label>


  <input type="radio" id="opt2" name="radiolist">
  <label for='opt2'>Existing Account</label>

  <div id="one">Lorem Ipsum 1</div>
  <div id="two">Lorem Ipsum 2</div>

</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

What is causing my mobile version not to resize and adjust to fit the screen properly?

Currently utilizing Bootstrap 4 along with the viewport meta tag, %meta{:content => "width=device-width, initial-scale=1, shrink-to-fit=no", :name => "viewport"}/ Even after hiding all images with .hidden-xs-up, the display on Chrome mobile appear ...

Creating a Stylish ExtJS Container with CSS: A Step-By-Step

I've been experimenting with the ExtJS properties cls, bodyCls, and componentCls but unfortunately, I'm not achieving the desired outcome. As an illustration: Ext.create('Ext.form.Panel', { renderTo: document.body, title: &apo ...

Utilize jQuery to assign title attributes stored in one array to elements stored in another

In my project, I have twelve <span> elements with the class "has_title" and each one has a unique title. Also, there are twelve <div> elements with the class "bar". The task at hand is to assign the titles from the <span> elements in th ...

Ways to slightly tilt an image in Material-UI

I am having trouble displaying my cactus partially above my dialog without it getting cut off when I apply certain styles. Could someone please point out what I might be doing wrong? Here are the styles for the cactus: cactus: { position: 'absolut ...

Exploring the origins of CSS through patch design

Want to learn how to create a background like this using CSS? Check out this link for inspiration! ...

Is the z-index problem in Safari related to a transform issue?

I've tried different solutions for similar issues, but I'm struggling to get the text to appear properly on my custom buttons in Safari. They are displaying perfectly in Chrome and Firefox, but not on iOS devices. Does anyone have any suggestion ...

.htaccess configuration leads to CSS problem

I am encountering an issue with my .htaccess file. The content of the file is as follows: RewriteEngine On RewriteRule ^product/(.+)$ /product.php?name=$1 [L] Essentially, this code searches for "product/" within the URL and redirects to product.ph ...

Place the custom form error in various responsive layouts

I have set up a bootstrap navigation bar along with a form to the right. An additional custom error box is designed to be displayed at the bottom of the form in the browser. https://i.sstatic.net/6c6Zz.png However, when viewed in responsive mode, the layo ...

Implementing a two column stacked layout with Susy, similar to the design of Twitter's interface

Having worked extensively with Susy, I recently encountered a unique use case that has left me puzzled. To illustrate my point, let's take Twitter as an example. If we observe their website layout, they have a standard three-column design with a brea ...

Could you please provide instructions on how to manipulate the :root CSS variable using JQuery?

Is there a way to update the :root css variable using jquery? :root { --color: #E72D2D; } .box-icon { background-color: var(--color); } I tried but it doesn't seem to work $("#blueColor").click(function(e) { $(":root").css("-- ...

Aligning a series of images with individual captions in the center

I am currently working on centering a row made up of four images along with their respective captions underneath. The code I am using is as follows: <div class="clear" style="text-align: center;"> <div style="float: left; padding-right: 10px;"& ...

How can I use Jquery to slide one div over another and replace it in its position?

I am working on creating a unique animation using CSS and jQuery. The animation involves a green box sliding (or growing) over a fixed red box, and then toggling back so that the green box shrinks while the red one reappears. One issue I encountered was u ...

Toggle visibility of button based on React state

My current setup involves a button named download, which is a component integrated into my interface. The functionality associated with this button is implemented within a container. I intend for the button to remain hidden by default unless a specific co ...

Guide to creating a secure login page with the help of cookies

I'm in need of a quick guide on how to implement a login page for a website using cookies. Every user has a unique username and password. Is it necessary to store both the username and password in the cookies, or is just the username sufficient? Is ...

Why does the modal window gracefully descend when the mobile keyboard is activated?

I designed a modal window that has a fixed position: <div className={classes['UIModal'] + ' ' + classes[transition]} onClick={() => dispatch(modalHandler('offer'))} > <div className={classes['UIModal__ ...

Creating Hover Areas on Sprites: A Step-by-Step Guide

The goal of this code is to create two interconnected areas on the top 50px of a page that will trigger a hover effect when the mouse moves vertically. The intention is for an image to shift laterally as you hit these two hover regions. <html> <h ...

Issue with dropdown menu display in Internet Explorer

Having trouble with a CSS dropdown menu displaying incorrectly in IE, while working fine on other browsers. <body> <div id="container"> <header> <div id="hlogo"> <a href="index.html">title ...

Stop flash notifications from sliding below the container

Creating a web application using Bootstrap 4, I implemented user notifications with alerts according to the documentation found here. However, I encountered an issue where the content below the alert shifts when the alert is displayed and then shifts back ...

Using AJAX in JavaScript within an HTML document is a valuable skill to have

I have the following JavaScript function that I need to call the /print2 function without clicking any buttons. I attempted to use Ajax for this, but I am new to Ajax and JavaScript. Can you help me identify where the issue might be? Thank you... <scr ...

Converting HTML to PDF using AngularJS

Can anyone help me with converting HTML to PDF in Angular? I have tried using the angular-save-html-to-pdf package from npm, but encountered errors. Are there any other solutions or custom directives available for this task? ...