Conceal a sibling of the parent element by utilizing CSS

I need help with a specific type of HTML code

<div class='box'>
   <input type="checkbox" id="c1">
   <label for="c1">Check me to hide</label>
</div>
<div id='con-1'>Div to hide when checked</div>

I'm looking to hide the div with id con-1 when the checkbox with id c1 is checked. However, I'm having trouble finding the correct selector to achieve this.

Disclaimer: I am unable to alter the existing HTML structure and cannot use JavaScript or jQuery.

You can experiment with the code live here: http://jsbin.com/xapaweqoju/edit?html,css,output

Answer №1

If you're hoping to achieve this with CSS, you'll need to alter your HTML layout since CSS currently lacks a parent selector.

Answer №2

To achieve this, you can utilize JavaScript or modify your HTML in the following manner:

input[type=checkbox]:checked ~ #con-1 {
  display: none;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8>
  <title>Custom Page</title>
</head>
<body>
<div class='box'>
  <input type="checkbox" id="c1">
  <label for="1">Click me to make disappear</label>
  <div id='con-1'>Content to hide once checkbox is checked</div>
</div>
</body>
</html>

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

Issue with nth-child selector not functioning as expected in unordered list

I have a straightforward <nav> setup with an unordered list inside: nav { position: absolute; right: 0px; white-space: nowrap; } nav ul { padding: 0; margin: 0; } nav ul li { clear: both; white-space: nowrap; color: white; displa ...

Bootstrap dropdown menu fails to expand

I recently attempted to implement a hamburger menu using Bootstrap on my website. After checking and adding the necessary CSS and JS files, I encountered an issue where the menu wasn't expanding as expected. I followed the example code provided on the ...

Activate the trigger event when the popover is activated remotely

I am facing a unique situation on my website where I have multiple popovers (pop1), (pop2), (pop3) that are triggered in sequence when the "Next" button is clicked. These popovers appear one after the other without direct access to them individually. If y ...

My HTML elements are not displaying in the correct order. Any suggestions on how to resolve

Hi, I am currently working on a website using HTML, CSS, and JS. I have encountered a small issue in the HTML section. Here is the code snippet: window.onscroll = function() { changeOnScroll() }; function changeOnScroll() { var winScroll = docum ...

Finding elements through their text representation

I am struggling to select an element using the text "SELECT PRODUCT" in the following HTML: <div style="font-family: franklin-gothic-urw; font-weight: 400; font-style: normal; font-size: 19px; opacity: 1; color: rgb(255, 255, 255);">SELECT&nbsp; ...

Words hovering in a section next to a captivating picture

I am looking to achieve a specific arrangement: I want two different texts (in block format) to float or display inline next to an image, all within a div element. Despite trying various display settings (such as block and inline for text), I have not be ...

What is the best approach for implementing various colors and styles using CSS?

I want to give each word in my website name a different color or style, similar to this: Currently, I am using: <a href="#">James<strong>.</strong><span>Wood</span></a> (Using "STRONG" and "SPAN" tags inside the "A" t ...

Tips for perfectly centering a light box and concealing a scrollbar

Hello, I'm a web designer and facing an issue with the alignment of my lightbox. The lightbox is not center aligned on any resolution, which is causing some trouble. I have also added a black overlay for transparency in the background, but it's s ...

Tips for making items move to the next line after floats

Here is the code snippet I'm working with: <div>First</div> <div class="second">Second</div> <a>Alink</a> div{ display: block; float:left; } .second{ clear:right; } http://jsfiddle.net/cg4gj/ I am ...

Implement a menu that can be scrolled through, but disable the ability to scroll on the body of the website

When viewed on a small screen, my website transforms its menu into a hamburger button. Clicking the button toggles a sidebar displaying a stacked version of the menu on top of the normal website (position: fixed; z-index: 5;). This sidebar also triggers a ...

How to Place Content Below a Fixed Navigation Bar?

Is there a way to insert content below a fixed navigation bar without relying on the margin-top property? Any alternative solutions? I experimented with position: relative and the top property, but I'm looking for a different approach. ...

Unable to invoke Kendo JavaScript

I'm facing an issue with my source code where I am trying to call kendo.all.min.js. Everything works fine when I call kendo.common.min.css and kendo.default.min.css, but as soon as I try to call kendo.all.min.js, I encounter the following error: http ...

Tips for making a fluid DIV expand to fit the entire width of the page

My layout features fixed-width sidebars on the left and right, with a fluid main content area that fills the space in between using float:left. Here's an example: #left-sidebar {width: 200px;} #right-sidebar {width: 300px;} #main-content {margin-left ...

Having trouble aligning input elements in the middle of a div container

Can anyone help me with centering elements inside a div? Check out this example: I created a wrapper for the number inputs and tried applying the text-center property on them, but it's not working. I also changed the display to block, but that doesn& ...

Troubleshooting: CSS Animation Not Showing up on Screen

When I was attempting to create a JavaScript game in HTML, I encountered a problem. Despite ensuring that the syntax was correct and conducting some research online, the animation refused to display. No matter how many times I tried, it just would not work ...

Struggle of UL vs. LI Designs

Currently, I am tackling a web development project that entails a CSS file known as Style.css. Inside this file, a styling series is outlined for UL and LI elements, which have been implemented across various parts and sections of the project. The setup lo ...

Struggling to populate a dropdown list with HTML, PHP, and MySQL

Here is the code snippet for creating a payment form: Everything seems to be working fine, but there is an issue with the supplier-ID dropdown. The dropdown is being created but it is not fetching data from the mysql table and no errors are being display ...

Take away the CSS class from an element after reCAPTCHA verification is complete in Next.js

I'm struggling with removing the CSS class btn-disabled from the input button element upon successful verification of a form entry. I have implemented a function called enableForm to remove the btn-disabled CSS class when reCAPTCHA is verified. Howe ...

CSS table row border displaying irregularly in Chrome and Internet Explorer

I recently created a basic table with bottom row borders. Surprisingly, the borders display perfectly in Firefox but only partially in Chrome and IE 10: <div style="display:table; border-collapse: collapse; width: 100%"> <div style="display:table ...

Color the text differently if the values are not the same (CSS and jQuery only)

In my code snippet below, you can see a set of HTML elements with specific classes and values: <div class="verizon-data-duplicate-device-key-89148000004605691537 k-content k-state-active" style="float: left; opacity: 1; display: block; ...