Choose all elements excluding the information within one specific div tag

Here is the structure of my DOM:

  • body
    • several elements such as divs
    • div.Popup with multiple child nodes nested deeply.
    • additional elements like divs

I am looking for a CSS selector that can apply cursor: default to everything except div.Popup and its child nodes.

Is there a way to achieve this?

The :not selector does not seem to work in this case.

For example:

<div>Cursor should be 'default' here.
    <input type="text"/> 
    <a href="#">Link</a> 
    <span class="Action">Action</span>
</div>
<div class="Popup">Cursor should remain unchanged here.
    <input type="text"/> 
    <a href="#">Link</a> 
    <span class="Action">Action</span>
</div>
<div>Cursor should be 'default' here.
    <input type="text"/> 
    <a href="#">Link</a> 
    <span class="Action">Action</span>
</div>

Check out the Fiddle Demo

Answer №1

Give this a shot:

* {
   cursor: default
}
div.Popup, div.Popup * {
   cursor: auto
}

Answer №2

Assuming the element with class='Popup' is a direct child of the body, using the below selector would fulfill the requirement:

body >:not(.Popup),
body >:not(.Popup) * {
    cursor: default;
    /* Add any other necessary styles */
}
.Action {
  cursor: pointer;
}
body >:not(.Popup),
body >:not(.Popup) * {
  cursor: default;
  color: green; /* for better visualization of selected elements */
}
<div>Cursor should be 'default' here.
  <input type="text" /> 
  <a href="#">Link</a>  
  <span class="Action">Action</span>
</div>
<div class="Popup">Cursor should remain unchanged here.
  <input type="text" /> 
  <a href="#">Link</a>  
  <span class="Action">Action</span>
</div>
<div>Cursor should be 'default' here.
  <input type="text" /> 
  <a href="#">Link</a>  
  <span class="Action">Action</span>
</div>

This approach would also work if the element with class='Popup' is not a direct child of the body, as long as the element has a single defined selector path. Below is an example in such a scenario:

.Action {
  cursor: pointer;
}
body >:not(.container),
body > .container >:not(.Popup),
body > .container >:not(.Popup) * {
  cursor: default;
  color: green;
  /* for better visualization of selected elements */
}
<div class="container">
  <div>Cursor should be 'default' here.
    <input type="text" />
    <a href="#">Link</a> 
    <span class="Action">Action</span>
  </div>
  <div class="Popup">Cursor should remain unchanged here.
    <input type="text" />
    <a href="#">Link</a> 
    <span class="Action">Action</span>
  </div>
  <div>Cursor should be 'default' here.
    <input type="text" />
    <a href="#">Link</a> 
    <span class="Action">Action</span>
  </div>
</div>
<div>abcd</div>

The key difference in this selector method, which may have been the reason for previous unsuccessful attempts, is the use of the children selector (>) rather than normal descendant or everything selectors (*).

If the selector were written as body :not(.Popup) or body *:not(.Popup), it would select all elements without the class='Popup', including the input, a, and span elements. This can be demonstrated in the following code snippet to visualize the selection:

.Action {
  cursor: pointer;
}
body :not(.Popup) {
  cursor: default;
  color: green; /* for better visualization of selected elements */
}
<div>Cursor should be 'default' here.
  <input type="text" value="dummy"/> 
  <a href="#">Link</a>  
  <span class="Action">Action</span>
</div>
<div class="Popup">Cursor should remain unchanged here.
  <input type="text" value="dummy"/> 
  <a href="#">Link</a>  
  <span class="Action">Action</span>
</div>
<div>Cursor should be 'default' here.
  <input type="text" value="dummy"/> 
  <a href="#">Link</a>  
  <span class="Action">Action</span>
</div>
<div>Cursor should be 'default' here. <!-- Selected -->
  <input type="text" value="dummy"/>  <!-- Selected -->
  <a href="#">Link</a>                <!-- Selected -->
  <span class="Action">Action</span>  <!-- Selected -->
</div>
<div class="Popup">Cursor should remain unchanged here. <!-- Not Selected -->
  <input type="text" value="dummy"/>  <!-- Selected -->
  <a href="#">Link</a>                <!-- Selected -->
  <span class="Action">Action</span>  <!-- Selected -->
</div>
<div>Cursor should be 'default' here. <!-- Selected -->
  <input type="text" value="dummy"/>  <!-- Selected -->
  <a href="#">Link</a>                <!-- Selected -->
  <span class="Action">Action</span>  <!-- Selected -->
</div>

Note: The selectors :not(.Popup) and *:not(.Popup) do not seem to work effectively (in Chrome v24). It appears that selectors like element:not(x), element :not(x), or element > :not(x) are required.

By using the children selector, only elements (and their descendants) without class='Popup' that are children (not descendants) of the body tag are specifically targeted. This ensures that elements whose parent is not body are not selected.

<div>Cursor should be 'default' here. <!-- Selected by 1st selector-->
  <input type="text" value="dummy"/>  <!-- Selected by descendant selector-->
  <a href="#">Link</a>                <!-- Selected by descendant selector-->
  <span class="Action">Action</span>  <!-- Selected by descendant selector-->
</div>
<div class="Popup">Cursor should remain unchanged here. <!-- Not Selected -->
  <input type="text" value="dummy"/>  <!-- Not Selected -->
  <a href="#">Link</a>                <!-- Not Selected -->
  <span class="Action">Action</span>  <!-- Not Selected -->
</div>
<div>Cursor should be 'default' here. <!-- Selected by 1st selector -->
  <input type="text" value="dummy"/>  <!-- Selected by descendant selector-->
  <a href="#">Link</a>                <!-- Selected by descendant selector-->
  <span class="Action">Action</span>  <!-- Selected by descendant selector-->
</div>

Answer №3

To change the cursor behavior, first set it to cursor:default; for the body element. Then, specify a different cursor style by using cursor:pointer; or any other desired pointer.

body {
    cursor:default;
}

popUp:hover {
    cursor:pointer;
}

If you only want to change the cursor on specific elements, simplify the code like this:

popUp:hover {cursor:pointer;}

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

Scaling down a retina image sprite using CSS: A step-by-step guide

Struggling with loading retina images? You're not alone. Many web developers face the challenge of scaling down high-resolution images effectively. Take, for example, two image sprites: one normal and one retina. The issue arises when you need to adju ...

Retrieve specific values from columns for dynamically generated rows using JavaScript

Currently, I am in the process of creating a dynamic table using PHP. In my screenshot, there are two rows with multiple columns. Each column in both rows has the same id. For example, the column for Monday has id="mon" in the first row and also id="mon" i ...

HTML drag-and-drop setDragImage fails to show ghost image on initial drag operation

Currently, I am working on developing a drag and drop menu feature that allows users to drag an image thumbnail from one div to a canvas element. The main issue I am facing is that the source div uses a sprite for displaying its background thumbnail. As a ...

Changing the action of a form dynamically with Javascript and Selenium in a Java environment

I am having trouble updating the action attribute of a form using JavaScript and Selenium in Java. Here is the snippet of HTML from the site: </div> <div class="contionue-shopping-wrapper"> <form class="co-formcontinueshopping" action="htt ...

Issues with IE6 hover state not properly resetting

Tutorial This particular issue has been isolated in the test case provided above. It is easily noticeable when viewed in IE6. Description of the Issue In Internet Explorer 6, when hovering over the link, all child elements that are supposed to be visibl ...

unable to auto-populate drop-down selection based on selected value in AngularJS

Currently, I am implementing a drop-down within an ng-repeat loop in AngularJS. Here is the HTML code snippet: <select class="form-control" ng-model="l.Language" ng-change="languageChange($index, l.Language)" ng-options="L.ID as L.LanguageDDL for L i ...

Is there a way to split the text into distinct pages within a contenteditable area, similar to the functionality in Google Docs

I've been working on developing a specialized browser-based text editor and I've encountered a puzzling question. How can I detect and split long texts into separate pages, similar to how Google Docs handles pagination? I'm aware that Google ...

aligning two elements within a container to have equal heights

I need to position 2 divs inside a container div. Their height should always be the same, regardless of content. To achieve this, I am using absolute positioning with top and bottom set to 0. Currently, the container div #three collapses and hides the cont ...

Only one ID is recognized by jQuery

I have a group of three checkboxes set up like this: <input id="image_tagging" class="1" type="checkbox" value="1" checked="checked" name="data[image_tagging]"> Now, I've created an AJAX function (which is functioning correctly), but it seems ...

Take a breather with a break in a div's outline

I'm looking to add a break on the right border of my div which already has an outline around it. Here's a visual example of how I want it to look like: (how I want it to look like) Here is my HTML code: <div class="carouseldiv w-75 mx-au ...

What is the best way to ensure a bootstrap sidebar collapses without altering the content width?

How can I create a bootstrap sidebar collapse without affecting the content width? To view my code, please visit Codepen $(document).ready(function() { $('#sidebarCollapse').on('click', function() { $('#sidebar').tog ...

What could be causing Line Clamp to fail when using text alignment justify?

How can I get proper ellipsis to show with -webkit-line-clamp when using text-align:justify? It's functioning correctly with text-align:left/right. Does anyone have any suggestions or tricks? div { text-align: justify; width:150px; pad ...

The Boostrap 4 dropdown menu is not fully visible when included in the code

I created a menu using bootstrap and php, which is working perfectly. However, when I include it in another php file, the dropdown items are not visible. I'm unsure if I need to add a div or use something else? This is the code for my menu.php: <b ...

Improved Discord Plugin: Retrieving a dynamic value from the plugin configurations directly in Discord

Hey there, I'm new to discord plugin programming and I'm struggling with retrieving a variable input from the plugin settings. Unfortunately, I don't have any example code to share but if you're familiar with discord plugins, I could re ...

A combination of Webpack CSS modules with Angular templates

I am currently trying to integrate webpack CSS modules into my angular/ionic project. I'm wondering if it's possible for the CSS module class definitions to be correctly appended to an external template instead of an inline template. When I embe ...

I can't figure out why my unslider isn't adapting to the screen size. Check out the fiddle for more details

I recently implemented unslider to create a slideshow that spans 100% of the width on my website. Everything seemed to be working fine until I tried resizing the screen, and the slides remained the same size as they were initially loaded. Here is the code ...

Enhance the appearance of text with Firefox and Chrome support

Add texture to text for Firefox and Chrome browsers Looking for ways to create textured text using CSS or JS! Create textured text that is supported by both Firefox and Chrome Please assist me with this. Thank you! ...

Fundamental CSS Selector

I'm encountering an issue with CSS selectors while using Foundation 5: I am having trouble selecting the desired element, which seems strange to me. The specific problem I am facing is related to changing the background-color of the "Right Button Act ...

What is the best way to ensure balance between columns in Bootstrap 4?

Hello everyone! I am currently working on ensuring that all the columns in my table have equal width, except for the update and delete columns which should remain as they are. I'm utilizing Bootstrap 4.5.2 for this task. Below is the code snippet of ...

Locating error messages in Selenium WebDriver: mastering xpath and css selectors

Test scenario: 1. Visit the [example test URL] : 2. Scroll to the bottom of the page and observe the Facebook comment module. 3. Without signing into Facebook, click 'Comment using' button and select 'Facebook' from the dropdown menu. ...