Choose the nodes along the route for printing

There is an issue where my page background is visible in a special print view. I want to identify my print view container with a specific class, let's say '.print'. My goal is to find a solution where I can define a CSS rule that targets the '.print' container itself, its parents, and children specifically, leaving everything else unaffected by the rule. This way, all other DOM elements can be styled using a display:none CSS rule.

Here is a sample code snippet:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  width: 100px;
  height: 100px;
  border: 1px solid red;
}

/* Hide all divs except those containing .print */
:not(:has(.print)) {
    display: none;
}

.print, .print * {
    display: unset; //!!!This part is not okay because under print there could be a child with display: block/flex or other value.
}
</style>
</head>
<body>


<div></div> <!-- this should be hidden -->
<div>c <!-- this should be displayed as block -->
    <div>b <!-- this should be displayed as block -->
        <div class="print">p <!-- this should remain untouched by the rules -->
            <div>a</div> <!-- this should remain untouched by the rules -->
        </div>
        <div></div><!-- this should be hidden -->
        <div></div><!-- this should be hidden -->
    </div>
</div>
<div></div><!-- this should be hidden -->

</body>
</html>

I am seeking assistance in merging these two rules without overriding the display property set under '.print'. Can any CSS expert provide guidance on creating such a comprehensive rule or ruleset that works universally to achieve the desired outcome described in the example?

I am trying to target every div element where there is no letter content (c, b, p, a).

If possible, I would like to avoid using JavaScript for this task.

Answer №1

Instead of utilizing the unset rule, you have the option to incorporate multiple selectors into your not:

div {
  width: 100px;
  height: 100px;
  border: 1px solid red;
}

/* Conceal all divs except for those that contain .print */
:not(.print, :has(.print), .print *) {
  display: none;
}
<div>hide</div> <!-- display should be none -->
<div>c <!-- this should be block -->
    <div>b <!-- this should be block -->
        <div class="print">p <!-- this should be UNTOUCHED by the rules -->
            <div>a</div> <!-- this should be UNTOUCHED by the rules -->
        </div>
        <div>hide</div><!-- display should be none -->
        <div>hide</div><!-- display should be none -->
    </div>
</div>
<div>hide</div><!-- display should be none -->

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

IE having issues with border display

I am looking to add borders to specific elements based on certain criteria, such as: Elements with the .xforms-required class that are not span elements OR input elements that are descendants of elements with the .xforms-required class To achieve this, ...

The user interface for selecting a date is not visible

Previously, when I tried this process it worked fine. However, now that I am following the same steps, it is not working as expected. <script src="scripts/jquery-1.6.1.min.js" type="text/javascript"></script> <script src="scripts/jquery.ui. ...

What is the best method for applying a style specifically to controls located on the lower part of the

I am currently working on designing a table in a webpage and need to set styles for the <td> tags. Is it possible to apply different styles to the upper and lower cells? Can I use a <style> that specifically targets the bottom <td> tags? ...

IE is notorious for not playing well with CSS

I am facing an issue with my Google search box code. It works perfectly on Firefox, Chrome, and other browsers except for Internet Explorer. Can anyone help me troubleshoot this? Below is the code snippet: Abulkas Tablet #outerContainer{ width: ...

The combination of Javascript and CSS allows for interactive and visually

I'm currently working on a project where I had to create a simulated weather page using only Javascript. However, I am struggling with the overall layout and have a few questions that I hope someone can help me with: Despite trying various methods ...

When using setInterval, the image remains static on Safari but updates on Chrome

In my project, I am using a mock array to distribute data. One part of the project utilizes this data to display a list of cases, each with assigned images. When a case is hovered over, the images associated with that case are displayed one at a time, with ...

What is the best way to eliminate the border around the input beneath the li element

I am trying to create a language tab input like this: https://i.sstatic.net/N0h2o.png But I want it to look like this: https://i.sstatic.net/QmnSr.png However, I am facing an issue with the border when any language tab is active! Here is my code in En ...

Are you surrounding a cluster of div tags?

I'm really struggling to understand what this particular step in my textbook is asking for. Here's the exact wording: "Beneath the h2 heading, insert a new line containing a div element with the class value table at the same indent level as the ...

What is the method for obtaining the ID of a dynamically generated DropDownList and verifying if its value has been altered?

I successfully created a view with the help of Steven Sanderson's blog where a dynamic number of textboxes and DropDownList are generated. Everything is functioning properly. However, I would like to ensure that the form cannot be submitted until each ...

Adjusting the angular routerLink based on an observable

When working with HTML in Angular, I am looking for a way to use an <a> tag that adjusts its routerlink based on whether or not a user is logged in. Is it possible to achieve this functionality within a single tag? <a *ngIf="!(accountService ...

Ways to eliminate gaps between div elements with CSS

I am currently working on a webpage that consists of one main div housing two inner divs (left_menu and content), along with an additional footer div outside the main container. When viewing the page, there seems to be a noticeable gap between the footer ...

Which tools are best suited for Xamarin development?

Recently, I've been contemplating how to style my app. One common approach is using CSS, but the question that lingers in my mind is whether it's truly compatible and if I should begin utilizing CSS. Is there perhaps a different language that wou ...

tips for passing value to the date field using proctractor

This is the HTML code I am working with: <input id="filter-datepicker" type="daterange" ranges="ranges" class="form-control date-picker" placeholder="Select Date Range" name="sensorDetails.date" ng-model="myDateRange" ranges="ranges" requi ...

The Django URL redirection issue is causing some trouble

I'm having trouble navigating to the next page using buttons like this: <a href="weekly/" class="btn">Weekly Report</a> <a href="daily/" class="btn">Daily Report</a> Whenever I click on the header or another button while on t ...

Safari's flexbox wraps the final column on the top row

When viewed in Safari and some other iOS based browsers, the last column of the first row wraps to the next line. Safari: https://i.sstatic.net/FhYyn.jpg Chrome / Others: https://i.sstatic.net/Vkvr0.jpg Code: .flexthis { display: -webkit-box; d ...

Steps to create interconnected circles using CSS

Currently, I am attempting to create a set of steps using only CSS. Here is what I have achieved so far: https://i.sstatic.net/sCRzr.jpg However, I am facing an issue with removing the excess line where the red mark is located. <ul class="step&q ...

The necessary parameters are not provided for [Route: bill] [URI: bill/{id}]

I'm having trouble locating the error in my code. Here is my controller: public function editBill(Request $req) { $customerInfo=Customer::where('id',$req->id)->first(); $billInfo=Bill::where('id',$req->id)->get( ...

The functionality of the CoreUI breadcrumb navbar became compromised following the inclusion of Bootstrap and Bootstrap-Vue CSS files

I recently incorporated bootstrap and bootstrap-vue into my project using npm. Following that, I included the top two import statements in my App.vue file. However, this seems to disrupt the layout of CoreUI's default horizontal navigation bar. Can an ...

Showing the values of two distinct select boxes in a URL

Here are both select boxes, the goal is to display selected values from both in the URL. This can be achieved by displaying them like this: e.g www.example.com#135+#140 OR www.example.com#135&140 (The order of values doesn't matter as long as bot ...

Creating Tabbed Navigation with CSS - A Step-by-Step Guide

I am looking to create tab contents using CSS. I have managed to create a custom tab style, but I am struggling to style its content with CSS. Can anyone provide guidance on how I can achieve this? CSS: .tabs { list-style: none; margin: 0; pa ...