Tips for maintaining table elements in place using JavaScript

My goal is to keep the tags within the table fixed when printing. The code I've written functions correctly in View mode, however, when printed using JavaScript, if one of the columns is too large, the rest of the columns get displaced. I want all columns to remain aligned at the top. I attempted to use position: fixed but it caused the table layout to crash.

<script>


    function printDiv() {
        var divToPrint = document.getElementById('table');
        var htmlToPrint = '' +
            '<style type="text/css">' +
            'table th, table td,table tr {' +
            'font-family:B Nazanin;'+
            'border:0.1em solid #000;' +
              'text-align:center;' + 
            
            'font-size:14px;' +
            'padding:0.2em;' +
            '}' +
            '</style>';


        htmlToPrint += divToPrint.outerHTML;
        newWin = window.open("");
        newWin.document.write(htmlToPrint);
        newWin.print();
        newWin.close();
    }



</script>

https://i.sstatic.net/e3FVJ.png

Answer №1

To ensure that the content in each cell is displayed at the top, you should use the CSS property vertical-align: top;.

<style>
            .table-bd{
                border-collapse: collapse;
                border: none;
                width: 200px;
            }
            .table-bd td{
                border: 1px solid #CCCCCC;
                word-wrap: break-word;
                word-break:break-all;
                width: 50px;
                height: 80px;
                vertical-align: top;
            }
        </style>

<table class="table-bd">
            <tr>
                <td>asdfasdfasdf</td>
                <td>asdfasdfasdfasdfasdf</td>
            </tr>
            <tr>
                <td>asdfasdfasdf</td>
                <td>qwerqwerqwerqwerqwer</td>
            </tr>
            <tr>
                <td>erwtewyrettyrety</td>
                <td>tyuirtyiuiytugfhdfgh</td>
            </tr>
        </table>

https://i.sstatic.net/uwUgK.png

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 need help with customizing the progress bar in HTML and CSS

How can I create a progress bar like the one shown below: https://i.sstatic.net/BFv87.png .detail-load { height: 42px; border: 1px solid #A2B2C5; padding: 1px; border-radius: 10px; } .detail-load > .detail-loading { ...

Unable to dynamically append items to Owl Carousel using JavaScript

I am currently working on adding items dynamically to an Owl carousel. This is how I am approaching it: HTML <div id="avatar-carousel" class="owl-carousel lesson-carousel"> <div class="item item-logo"& ...

Error: The function $(...) does not contain a progressbar

I encountered a TypeError saying $(...).progressbar is not a function when loading the Gentelella - Bootstrap Admin Template Page. This error is affecting many jQuery processes. How can I resolve this? Error in my Code: (custom.js) // Progressbar if ($( ...

Is there a way to achieve the same zooming and panning effects on an element using CSS transform instead of jQuery's

If you click on the following link, you will see a jsFiddle that I have created to demonstrate my question: http://jsfiddle.net/VbCcA/3/ In my project, I have developed a function that allows for panning and zooming to a specific element. This function i ...

Error: Headers cannot be set after they have already been sent, resulting in an Unhandled Promise Rejection with rejection id 2

I'm a beginner with Node.js and I've been using express.js. In a section of my code, I'm trying to retrieve data from a form via AJAX and store it in a variable called url. I have access to request.body, but I'm encountering an issue wh ...

Using postMessage with an iframe is causing issues within a React application

I encountered two errors when executing the code below in my React application: try { iframe.src = applicationRoutes.href; iframe.style.width = '0px'; iframe.style.height = '0px'; iframe.style.border = '0px& ...

Is it possible for the font size to adjust based on the heading (h1, h2, h3) I choose when using CSS to specify a particular font size?

What will happen if the following HTML code is used: <h1> This is text</h1> <h2> This is also text</h2> and CSS is applied to change the font size like this: .h1 { font-size: 30px } .h2{ font-size: 30px } Will both texts ...

Issue with Bootstrap 4 navbar functionality on smaller screens

I'm currently in the process of integrating a navbar that transforms the navbar choices into a dropdown menu on smaller screens (as illustrated in the documentation available here). However, I am encountering an issue where the button responsible for ...

SCRAM-SERVER-FIRST-MESSAGE: The client's password is required to be in string format

After researching documentation from various sources on a similar issue, I have not been successful in resolving this specific error within my code. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string') ^ ...

Creating a dynamic carousel with adjustable images

My image carousel is giving me trouble with responsiveness, specifically the height of the images. While the width looks okay, the height is only half of what I need. Can someone offer some ideas on how to make my image height 100% responsive? I've tr ...

The new Date function is malfunctioning on Firefox

Could you please explain why this particular function is not functioning correctly in Firefox (V 34 latest)? It appears to be working successfully on all other browsers, but 'DatePosted' is displaying as Invalid Date. Do you have any insights on ...

The issue of JQuery selector failure within an AngularJS controller

In my current setup, I have viewA along with ControllerA. However, when an image is clicked on viewA, I need to switch over to another ViewB along with its corresponding ControllerB. In ViewB, there are multiple checkboxes which the user can interact wit ...

Importing FontFace elements within the _document.tsx file

I have a collection of FontFace objects stored in a file called fonts/myFonts.js: const fontFaces = [ new FontFace('MyFont', "url('local/location.woff2') format('woff2')", { weight: '400', style: ...

How to Route in Angular 5 and Pass a String as a Parameter in the URL

I am currently working on an Angular project that focuses on geographic system data. The concept is as follows: I have a component with the route: {path: 'home'}. I aim to pass a geojson URL along with this route, making it look like this: {pat ...

The method window.scrollTo() may encounter issues when used with overflow and a height set to 100vh

Suppose I have an HTML structure like this and I need to create a button for scrolling using scrollTo. However, I've come across the information that scrollTo doesn't work well with height: 100vh and overflow: auto. What would be the best way to ...

Retrieve active route information from another component

We are utilizing a component (ka-cockpit-panel) that is not linked to any route and manually inserted into another component like so: .. ... <section class="ka-cockpit-panel cockpit-1 pull-left"> <ka-cockpit-panel></ka- ...

Refresh client web pages with JSON data without using eval

I am currently working as a consultant on a web application that functions as a single page app. The main purpose of the app is to constantly fetch new json data in the background (approximately every minute) and then display it on the screen. Our clients ...

Create dynamic transitions for hidden elements using a special technique

Is it possible to smoothly transition a div element from display:none to display:block? I attempted to first set the display to block and then apply a transition, but it doesn't seem to be working as expected. HTML <input type="text" class="inp"& ...

Utilize React MaterialUI's ExpansionPanelSummary by incorporating a counter to a specific div id

I am in need of a feature that will automatically increment a counter in the div id every time the render function is invoked. The main goal is to ensure that each div has a unique identifier. Below is the current render function implementation - render ...

The attempt to log in using AJAX and PHP was unsuccessful

Why does the output always show a false alert even though the email and password match those in the database? PHP: <?php include 'db.php'; $email = trim($_POST['email']); $password = trim($_POST['password'] ...