Ensure there is a blank space preceding a table only when the table is being physically printed

I am currently in the process of developing a report card system for students in grades K-6. The report card will display specific tables based on the student's grade level. For example, a 5th grader may not have a "Reading Stage" table displayed on their report card, whereas a 1st grader would. I have successfully formatted the styles to conditionally print the appropriate tables, but I am encountering difficulty with spacing between the tables.

My goal is to maintain a consistent amount of space between tables. I have attempted various methods such as adding a blank row at the beginning of each table or including a margin-top of 50pt. However, all my attempts have resulted in excess space being added between tables, even those that are hidden. This has led to an undesirable situation where there can be up to 200 points of unnecessary space between tables.

I am in need of a creative solution that will allow me to add space between tables only if they are going to be printed on the report card.

Answer №1

I'm not quite sure how you are currently concealing your tables. If you are using the HTML5 hidden attribute or display: none, then there should be no issue with top margin affecting your layout.

If for some reason neither of these methods work for you, utilizing CSS negation could provide a solution. For instance, indicating that all tables without a specific class should have a margin-top: 1em.

table:not(.skip) {
  margin-top: 1em;
}

.skip {
  position: relative;
  background-color: yellow;
}

.skip::after {
  position: absolute;
  top: 3px;
  left: 150%;
  content: ' <-- no margin-top';
  white-space: nowrap;
}
<table>
  <tr>
    <td>table</td>
  </tr>
</table>

<table class="skip">
  <tr>
    <td>table</td>
  </tr>
</table>

<table>
  <tr>
    <td>table</td>
  </tr>
</table>

<table class="skip">
  <tr>
    <td>table</td>
  </tr>
</table>

<table>
  <tr>
    <td>table</td>
  </tr>
</table>

Answer №2

Although the previous response addressed your question, have you considered utilizing @media print css? By incorporating conditional print css, you can tailor styles specifically for printing purposes.

// For testing purposes only, you can print without this code. It is solely for stackoverflow testing...
$("#testPrint").on("click", function() {
  window.print();
});
@media print {
  /* custom styles for printing */
  .myTables {
    background: orange !important;
    margin: 100px !important;
    border: 1px solid black !important;
    width: 500px;
  }
}

.myTables {
  background: pink;
  border-collapse: collapse;
  border: 1px dashed black;
  padding: 5px;
  margin: 5px;
  text-align: center;
}
<!-- JavaScript not necessary -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>

<div id="wrapper">
  <table class='myTables'>
    <tr>
      <td>test1</td>
      <td>test2</td>
    </tr>
  </table>

  <table class='myTables'>
    <tr>
      <td>test1</td>
      <td>test2</td>
    </tr>
  </table>

  <table class='myTables'>
    <tr>
      <td>test1</td>
      <td>test2</td>
    </tr>
  </table>
</div>

<button id="testPrint">TEST PRINT</button>

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

"Discover the process of incorporating two HTML files into a single HTML document with the help of

I successfully created both a bar chart and a pie chart using d3.js individually. Now, I am trying to load these charts into another HTML file using jQuery. $(document).ready(function() { console.log("ready!"); $("#piediv").load("file:///usr/local ...

Building Effective Web Designs with Bootstrap and Semantic Grid System

There are several ways to effectively organize and maintain the Bootstrap Grid. For example: <div id='header'> <div class='row'> <div class='col-md-6 header-left'> or <div id='header&ap ...

Guide on making a flowchart using CSS

I'm working on creating a CSS flow chart diagram and I've included an image below for reference. Might there be a straightforward method to achieve this? I've been scouring the web for examples, but I'm unsure of what terminology to use ...

Discover the art of customizing child elements using vanilla extract!

I recently started using vanilla extract to add styles to a NextJS application. Is there a way to style child elements within the parent element without having to create another class? My React component has a structure like this: <ul className={style ...

What is the correct method to define the width as a percentage while also maintaining a minimum width in pixels?

My header/navigation bar div features a repeat-x background image. Within this, I have a horizontal navigation that needs to be centered over the background with a specified min-width and normal width. I want the navigation to be 80% of the full width, wi ...

What is the best way to incorporate both images and text in PHP code?

I'm currently working on creating a large image call-to-action (CTA) for my new WordPress website. I've set up a new field group with the type "group" in ACF, and added some functions in PHPStorm. However, none of my images, text, or links are ap ...

Converting CSS code into JavaScript

I am currently working with the following code: .mr15 > * margin-right: 15px &:last-child margin-right: 0 I need help translating this code to Javascript. Should I use JQuery or pure Javascript for this scenario? Thank you. ...

HTMLMediaElement does not have the setSinkId method

I am currently in the process of developing a WebRTC application using Angular, with the goal of managing audio output through the setSinkId() method within HTMLMediaElement. However, when attempting to use this method, I am encountering an error message s ...

Tips for waiting on image loading in canvas

My challenge involves interacting with the image loaded on a canvas. However, I am uncertain about how to handle waiting for the image to load before starting interactions with it in canvas tests. Using driver.sleep() is not a reliable solution. Here is ...

Struggles encountered while developing a JavaScript calendar

Hey guys, I'm encountering an issue with Uncaught TypeError: document.getElementById(...) is null. I believe the problem lies in the way types are being parsed for document.getElementById(dayOfWeek).appendChild(dayPTag);. This code is meant to display ...

Do not transfer any files during the migration process from NGINX to Apache

I have a specific URL structure: http://www.example.com/folder/index.php?dir=dir1 I want to make it accessible from: http://www.example.com/folder/dir1 To achieve this, I set up rules in my .htaccess file located in the 'folder' directory: O ...

Is there a way to convert various elements sharing the same class into a list of array items?

Essentially, I am dealing with multiple elements sharing the same class name. My goal is to retrieve an array of integers from an API and then iterate through each element with this class name, replacing them with elements from the array sequentially. For ...

The width of table columns is altered upon the second rendering in JQuery DataTables

Is there a way to prevent JQuery DataTables cells from extending width after adding hyperlink to cell? I am facing an issue where I have two tables on the same page. The first table column has a link which populates the second table. It works fine when th ...

failure in data transmission causing upload issues

Is there a way for me to prevent the character > from causing issues? I have a combo box with an option listed below. I attempted to replace > with &gt;, but it still isn't uploading correctly. <select name="Category3" id="Category3"> ...

What could be causing my page's wrapper to shrink upon logging in?

After logging out, the background of my page wrapper expands to cover 100% of the page. However, once you log back in, it shrinks and adjusts to the width of the member bar... TO TEST USER ACCOUNT BELOW: USERNAME: TEST PASSWORD: TEST123 HTML FOR LOGGED ...

Is it possible to hide the <dd> elements within a <dl> using knockout's custom data binding upon initialization?

I have implemented a <dl> where the <dd> can be expanded/collapsed by clicking on the corresponding <dt> using knockout's data binding. The inspiration for my solution came from a tutorial on creating custom bindings. Currently, I h ...

Tips for updating the class of a button upon clicking it

I have a dilemma with my two buttons - one is green and the other has no background. I want them to change appearance when clicked, from green to one with a dashed border-bottom style. Below is the HTML code for these buttons: <div class="btns"> ...

Is it advisable for a component to handle the states of its sub-components within the ngrx/store framework?

I am currently grappling with the best strategy for managing state in my application. Specifically, whether it makes sense for the parent component to handle the state for two subcomponents. For instance: <div> <subcomponent-one> *ngIf=&qu ...

Stop video and audio playback in an android webview

Is there a way to pause audio and video in an Android WebView without disrupting the page rendering? I have tried various methods but haven't found one that successfully pauses both the sound and the video without affecting the WebView. webView.onPau ...

Finding the nearest span value upon clicking through a specific class

Whenever a user clicks on a hyperlink, my goal is to locate the nearest span element with a specific class and retrieve the text within that class. As of now, it is only returning blank text: Here is the provided HTML code snippet: <div class="plan re ...