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

I must update the menu to show only one sub-menu at a time, ensuring that multiple menus cannot be displayed simultaneously

When I click on a menu option, it displays correctly, but when I click on another option, both are displayed. The goal is to only display one at a time. $('.sub-menu ul').hide(); $(".sub-menu a").click(function () { $(this).paren ...

What is causing the align content property to not function as expected in this situation?

Is it expected for the flex items of the .center-section-container to be closer together when using this property? They do not seem to be affected at all. I have applied flex wrap so the items are now in different lines but too far away from each other, I ...

The functionality of the jQuery button subscription is completely ineffective

I am attempting to display search results on the same page, but I'm encountering an issue. Here is my code: <form id="myForm"> <input id="filter_id" type="text"/> <input type="submit" id="load_results" value="Search" /> < ...

Hover effects in CSS animations can be hit or miss, with some working smoothly while others may

Below is the HTML content: <div class="wrapper"> <figure align="center"><img src="http://www.felipegrin.com/port/or-site.jpg" alt=""><br><span>RESPONSIVE HTML5 WEBSITE FOR <br> OR LEDS COMPANY</span></fig ...

The CSS does not get applied to the returned element in Next JS

When I create a function to return a DOM element with an associated className, the local style doesn't seem to be applied. For example: export default function thing(){ const elem = function(){ return (<div className="myCss">Hello< ...

Automatically updating CSS file in progress

Is there a way to have CSS changes automatically update on my template without requiring me to manually refresh the page? I've noticed this question being asked multiple times, but none of the solutions provided seem to work for me. I attempted usin ...

Having difficulty implementing Jquery UI effects within an HTML Dialog in Google Apps Script

Looking for a scrolling effect in an HTML Dialog with Google Apps Script. I've already tried this link and placed the code in the editor. However, when highlight buttons are clicked, they work but do not cause scrolling within the HTML dialog. < ...

Enhance user experience with Bootstrap by automatically adding a frame around a card upon clicking

Hello everyone! I am a beginner in the Angular/Web Development world and I am currently working on improving my HTML/CSS skills. While using Bootstrap in my Angular project, I ran into a challenge that I couldn't figure out on my own. I have implement ...

how to change class on vue function result

I need a way to display the content stored in requestData as li elements. Each list item should have an onclick function that, when clicked (selected), adds a specific value from a reference to an array. If clicked again (unselected), it should remove the ...

Frozen Blanket: Modifying Particle Velocity

I need assistance adjusting the particle speed in this snowfall animation script. I'm having trouble locating the specific values that control the "Falling Speed." The current speed of the falling particles is too fast, and here is most of the code sn ...

Ways to customize a directive to show conditionally with no need for container elements

I have created a basic role-based security directive in angularjs. It's my first attempt at making a directive. My goal is to replace the following HTML: <authorize if-granted="GET_POSTS"> Hello WORLD!!!! {{name}} </authorize> with j ...

optimal method for organizing design elements

I have a container with 9 square-shaped divs that I want to arrange in the following layout: It should resemble something like this: _________ ____ ____ | A | B | C | | |____|____| | | D | E | |_________|____|____| | F | G | ...

Adjust the color of the navbar only after a user scrolls, with a slight delay rather than

My code changes the navbar colors when I scroll (200ms). However, I would like the changes to occur when I am slightly above the next section, not immediately. In other words, what adjustments should I make to change the color in the next section and not ...

Lines are showing up on the screen, but their origin within the html or css remains a

While creating my website, I encountered a minor issue. Some elements like div or text element are showing lines when hovered over with the mouse, but they disappear once clicked elsewhere. Surprisingly, there is no border defined for them. I am stuck at ...

Troubleshooting: Unable to Display Collapsing Navbar in Bootstrap 5 When Toggler is Pressed

My attempt at creating a collapsing navbar isn't working as expected, and I'm struggling to identify the issue. I have set up a collapse toggler that should target #navbar1. However, when I shrink the page, the Navbar collapses, the .navbar-togg ...

JavaScript implementation of a carousel slider with responsive design

I have successfully implemented a feature carousel slider using the jquery.featured.carousel.js file along with some CSS. Here is the jsfiddle link to my project: LINK. When running this code on localhost, I noticed that I need to refresh the page every ...

Offset the content from the border within a container that includes a scrollbar

The containing element has some content that is set to have a fixed height and overflow: auto; as shown in the JSFiddle link provided. When scrolling through the text, you will notice that the border comes into contact with the text both above and below. ...

Script for automated functions

I have implemented 4 functions to handle button clicks and div transitions. The goal is to hide certain divs and show one specific div when a button is clicked. Additionally, the background of the button should change upon hovering over it. Now, I am look ...

Siblings selectors within Internet Explorer slow down significantly when using the :hover pseudo-class

Sample page: http://jsfiddle.net/y2pwqch6/6/ This particular example showcases a CSS selector known as the adjacent sibling selector: .l:hover + .r { color: blue } The issue arises when hovering over any element on the page, causing Internet Explore ...

Discovering keywords within HTML code and concealing particular content through jQuery

My HTML code snippet is shown below: <div id='p1'> <p>First Paragraph</p> <p>Abbot and Costello - Africa Screams</p> </div> <div id='p2'> <p>Second Paragraph</p> <p>Abbot and ...