Conceal all components on a webpage with the exception of a designated div

I am looking to selectively hide elements on the page, revealing only the contents of div.k1. The page contains numerous elements. How can I achieve this using just CSS?

<div>1-this will be hidden</div>
<div class="k1">
  2-this div will be shown
  <p>3-this will be shown</p>
  <p>4-this div will be shown</p>
  <div>
    5-this will be shown
    <p>6-this will be shown</p>
    <div>
      7-this will be shown
      <p>8-this will be shown</p>
    </div>
  </div>
</div>
<div>9-this will be hidden</div>
<div>10-this will be hidden</div>

<div>1-this will be hidden</div>
<div class="k1">
  2-this div will be shown
  <p>3-this will be shown</p>
  <p>4-this div will be shown</p>
  <div>
    5-this will be shown
    <p>6-this will be shown</p>
    <div>
      7-this will be shown
      <p>8-this will be shown</p>
    </div>
  </div>
</div>
<div>9-this will be hidden</div>
<div>10-this will be hidden</div> 11,12,13..

Answer №1

To hide all the specific elements within the body tag that are div's, excluding those with the class name k1, you can utilize the following JavaScript code snippet.

var items = document.querySelectorAll("body>div:not(.k1)");
for (var i = 0; i < items.length; i++) {
    items[i].style.display = 'none';
}

This script selects all the div tags directly under the body tag, except for those with the class name k1. It then loops through these selected elements and hides each one by setting its display property to 'none'.


If a CSS approach is preferred and the criteria remain the same as described above, you can achieve the same result using the following CSS rule.

body>div:not(.k1) {
    display: none;
}

If you wish to delve deeper into CSS selectors, I recommend exploring the topic on the W3 schools page.

Answer №2

If you're looking for a quick solution to hide divs nested up to 2 layers deep, here's a simple approach. By targeting all div elements and checking their classNames and parent classNames, we can selectively hide them based on specified conditions. This method involves adding multiple conditional checks to ensure only the desired elements remain visible. For more complex nesting structures, additional parentNode references can be included in the script. However, it may be more efficient to assign specific classes for hiding elements and revealing others using IDs. This alternative strategy avoids modifying the original HTML structure.

Cheers!

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

Hyperlink -> Boundary

Our homepage features various elements as part of the menu. Each element consists of a picture, title, and small description. The issue is that these menu items always display a border when the mouse hovers over them, despite attempts to hide it. You can ...

CSS Flexibility in Action

Presently, my tab bar has a fixed look as shown here: https://codepen.io/cdemez/pen/WNrQpWp Including properties like width: 400px; etc... Upon inspecting the code, you'll notice that all the dimensions are static :-( Consequently, I am encountering ...

Is there a way to ensure that the header is centered at the top and the footer is centered at the bottom of every printed page, even when

Currently, I am utilizing spatie/browsershot to generate PDF documents within a Laravel project. While it offers convenient methods such as headerHtml and footerHtml for customization, there seems to be an issue with handling images. Specifically, only bas ...

Tips for eliminating flutter for static menu with easyResponsiveTabs.js

Experiencing a flickering issue with the fixed menubar and easyResponsiveTabs.js in my project when scrolling down. Attempted to resolve it using jquery.noConflict(), but without success. Would appreciate any guidance on how to address this problem. ...

CORS issue encountered by specific user visiting the hosted website

I recently developed a bot chatting website using Django and React, which I have hosted on HOSTINGER. The backend is being hosted using VPS. However, some users are able to see the full website while others encounter CORS errors where the APIs are not func ...

Aligning the stars with CSS

One of the components I have deals with a star rating system, and I thought it would be cool to use Font Awesome icons for half stars. Everything is working well except for the CSS styling aspect. While I managed to position some of the stars correctly by ...

Tips for transitioning from custom CSS to Material UI's CSS in JS

I came across a project where someone implemented components with custom CSS. One interesting thing I noticed was a wrapper component, similar to Material UI's Container or just a simple div with applied styles. export const Container = styled.div` ...

Get rid of the <h1> tag surrounding the Wordpress logo

Currently, I am utilizing an Ultimatum child theme that was not created or selected by me. Upon inspection, I discovered that the logo is enclosed in an <h1> tag. For the sake of SEO, I require a different, more optimized logo as it seems the curre ...

Guidelines for aligning a form in the middle of the screen (positioned between the navigation bar and footer

Before asking this question, I made sure it's not a duplicate by researching previous similar issues with no success. I'm currently utilizing Bootstrap 4. You can view my app at (I'm unable to post the CSS and HTML of my React app due to S ...

Unexpected issue with sliding menu toggle implementation

I have been struggling to make a menu slide to the left when a button is clicked, and then slide back to its original position when the same button is clicked again. Although I have been using the toggle method, it doesn't seem to be working for me. I ...

Attempting to save MongoDB data into a variable for integration with Handlebars.js

Having an issue with storing MongoDB data in a variable to display in HTML using hbs. The specific error message is TypeError: Cannot read property 'collection' of undefined. Here's the code snippet I have written: const express = require(& ...

CSS - Retain z-index until the transition is complete

Looking at this basic grid layout: <html> <head> <style> .grid { display: grid; grid-gap: 5px; grid-template-columns: repeat(13, 3fr); } .box { position: relat ...

Issue with AnimeJS Motion Path causing element to deviate from desired SVG path

I'm attempting to use an SVG element and the AnimeJS library to make the orange marker follow the course of this RC car race track. https://i.stack.imgur.com/8FKHC.png Despite my efforts, I am encountering strange and undesirable outcomes. At times ...

Issues with the menu pop-up functionality arise when utilizing the CSS class .active

When I have this code, the ".active" function doesn't work when clicked. However, if I change the div class from "top-menu-popup" to "top-menu-popup active" when opening the page, the menu is already activated. <div class="top-menu-popup"> ...

Is there a way to delay rendering the html until all the content has been fully downloaded?

Whenever I visit my webpage, I notice that the content starts loading without the animation applied to it. It seems like the CSS hasn't finished downloading yet. To solve this issue, I added a preloading bar overlay to signal that the content is still ...

The webpage failed to show the hamburger menu

I'm trying to create a hamburger menu and have written the following code: <header class="header"> <nav class="flex flex-jc-sb flex-ai-c"> <a href="/" class="header__logo"> <img src="im ...

Filter search results to display only posts

My website uses a custom Wordpress theme that has a search functionality. The issue I'm facing is that when I perform a search, it redirects me to the search.php page. However, instead of just displaying posts, it also shows pages and events from the ...

Use JavaScript to generate an HTML element that has an attribute mirroring its text content

Trying to figure out how to create an HTML element similar to this: <option value="Replaced">by this</option> For example: <option value="ThisIsTest">ThisIsTest</option> UPDATE Using jQuery, I need to achieve something like thi ...

I need to adjust the alignment of the text within my list item that includes a time element

I am struggling with aligning my elements vertically downward as the text following the "-" appears disorganized. I attempted to enclose the time tags within div elements and align them so that they evenly occupy space, but this approach did not work. Ins ...

AInspector WCAG evaluation found that mat-select does not meet level A compliance standards

As I work on making my website WCAG level A compliant, I've encountered an issue with the mat-select component in Angular material. After running checks with the AInspector extension for Firefox, it appears that the mat-select component lacks aria-con ...