Entire body encompassing table

I am trying to create an HTML page with a table that spans the entire body and displays scrollbars when needed for both horizontal and vertical scrolling.

Here is my current styling for the table:

table {
  box-shadow: inset 0 0 10px #000;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: auto;
}
<table>
  <tr>
    <td>test</td>
  </tr>
</table>

Fiddle Link

The issue I'm facing is that by default, a table column will adjust its width based on content. I want to maintain this behavior while enabling horizontal scrolling for too many columns and vertical scrolling for too many rows. The box shadow represents the edges of the table, which I want to always be at least 100% of the viewport with scrollbars if it exceeds this size.

However, as shown in the example, the table does not span the entire viewport. Any ideas on what I might have done wrong?

Answer №1

When it comes to applying overflow on a table element, you may face some limitations as it does not work directly. To overcome this issue, the workaround involves wrapping your table inside two separate div elements.

1. The outer div with the class .wrapper is used for handling overflow:auto when the content of the table exceeds the width of the viewport.

2. The inner div with the class .inner sets up the min-width and max-width properties based on the viewport width. Additionally, it applies display:inline-block to ensure that the div can expand its width if the content surpasses the viewport limit.

Embedding Code Snippet

body {
  margin: 0;
}

div.wrapper {
  height: 100vh;
  width: 100vw;
  overflow: auto;
}

div.inner {
  box-shadow: inset 0 0 30px #000;
  font: 13px Verdana;
  display: inline-block;
  min-height: 100vh;
  min-width: 100vw;
  vertical-align: top;
}

table {
  border-collapse: collapse;
}

td,
th {
  border: 1px solid;
  padding: 10px;
}
<div class="wrapper">
  <div class="inner">
    <table>
      ...Content goes here...
    </table>
  </div>
</div>

Answer №2

Include height: 100vh; in the CSS for the <table> tag to make use of viewport height units.

UPDATE: utilize overflow-y

body {
  margin: 0;
  padding: 0;
  background-color: orange;
}

table {
  box-shadow: inset 0 0 10px #000;
  height: 100vh;
  overflow: scroll;
}

td{
    background-color:red; 
    width: auto;
}

.box{
   overflow: scroll;
   height: 100vh;
   background-color: green;
   width: 15vw;
}

.text{ float:right;}
<div class="box">
  <table>
  <tr>
    <th>ID</th>
  </tr>
  <tr>
    <td>#1</td>
  </tr>
  <tr>
    <td>#2</td>
  </tr>
  <tr>
    <td>#3</td>
  </tr>
  <tr>
    <td>#4</td>
  </tr>
  <tr>
    <td>#5</td>
  </tr>
  <tr>
    <td>#6</td>
  </tr>
  <tr>
    <td>#7</td>
  </tr>
  <tr>
    <td>#8</td>
  </tr>
  <tr>
    <td>#9</td>
  </tr>
  <tr>
    <td>#10</td>
  </tr>
</table>
</div>

<span class="text"> example text in background </span>

This information should be beneficial. The red cell background highlights its span, while the orange background represents the body, and the green background belongs to the table's parent tag. Feel free to add more columns as needed.

Answer №3

Here is a code snippet that might be helpful:

table {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  background: grey;
  box-shadow: inset 0 0 10px #000;
  overflow: auto; /* Scroll bar will appear if content exceeds fixed dimensions */
}
<table>
  <tr>
    <td>Scroll bar will appear if the content exceeds the fixed dimensions.</td>
  </tr>
</table>

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

What is the best way to utilize a single component for validating two other components?

I am encountering an issue with my components setup. I have three components in total: GalleryAddComponent, which is used to add a new element, GalleryItemComponent, used to edit an element, and FieldsComponent, the form component utilized by both GalleryA ...

Vertical centering issue with div specifically in Safari

I am facing an issue with centering a nested div containing a UL within another div. Despite trying various methods to achieve vertical alignment, I am unable to get it to work properly. PARENT .splash { background: url(../img/splash.jpg); backgro ...

Chaos in the IE8 drop-down menu situation

I'm having trouble with my code for a menu with drop-downs in ie8. It works fine in all other browsers but there seems to be an issue with the positioning of the main menu and submenus. Any thoughts on what might be causing this? <ul id="nav" clas ...

Unique rephrased text: "Amongst a backdrop devoid of white,

In a div with a colored background, I noticed that adding an input element results in an unsightly border. <div class="input-group"> <input type="search" style="height:30px;"> </div> Is there a way to remove this border? ...

Sending a file through an Ajax POST request to a PHP server

I am attempting to upload the HTML input file to my PHP file using a different method than the traditional synchronous HTML forms. The problem I am encountering is that it seems like I am not correctly POST'ing the input file to my PHP file because t ...

Height adjustment for flexbox children

Can someone assist me with marking up a todo list? I am attempting to set the height of div.main-tasks equal to div.tasks so that the former fills the entire latter. Unfortunately, I am unsure how to achieve this. You can refer to the following image for c ...

Is it possible to view the CSS of a PDF file in print template mode using Chrome?

https://i.stack.imgur.com/XlcWm.png Currently facing an issue with debugging CSS on my PDF template. I am unable to inspect the styles directly in the CSS due to limitations. When using inspect element in Chrome, only the styles for text and elements are ...

"Difficulties with Tribute Page Project on FreeCodeCamp: HTML and

I am currently working on a tribute page for a challenge from FCC and I am facing some issues with adding padding to my .life and .work divs in the HTML and CSS code above. Despite spending a few hours searching for solutions online, I have not been able ...

Utilizing ionic-scroll for seamless movement and scrolling of a canvas element

I have set up a canvas element with a large image and I want to enable dragging using ionic-scroll. Following the provided example: <ion-scroll zooming="true" direction="xy" style="width: 500px; height: 500px"> <div style="width: 5000px; h ...

Master Angular Autocompletion

Looking to add a filter autocomplete feature but unsure of how to implement it. Any assistance would be greatly appreciated! I've come across some examples, but they don't quite fit my needs. Here's the snippet of code: <mat-form-field c ...

Highcharts' labels on the x-axis do not automatically rotate

One issue I am facing is that my custom x-axis labels on the chart do not rotate upon window resize. I would like to have them rotated when the screen size is less than 399px. Check out the code here $(function () { $('#container').highchart ...

When the content in the div exceeds its boundaries, it causes it to overlap with

In my design, I have a top menu with a z-index of 999 to ensure nothing covers it. However, I am facing an issue where a scrolling div is appearing on top of the menu bar even though it shouldn't. Any idea why this is happening? Here is the CSS for t ...

Adding CSS files to a JSP page in a Struts2 application

Hello, I am currently learning Java and working on a Struts2 project. I have added a stylesheet in the following path: /WEB-INF/common/css/style.css and also included Bootstrap in this directory: /WEB-INF/common/css/bootstrap/bootstrap.min.css I ...

Is there a way to align the height of a standard column with the ng-include section to ensure a cohesive page display?

I have been working on my Angular app and have implemented ng-include to dynamically add content to a template within the page. The issue I'm facing is that this specific area ends up being longer than both the html and body elements where it resides. ...

"Encountering issue with SCSS variable not being applied to background in file

$enable-grid-classes: false; $enable-cssgrid:true; $deepBlue: #032f3e; body { --bs-body-bg: orange ; } @import "https://fonts.googleapis.com/css2?family=Space+Grotesk:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="463121 ...

Animation of scrolling in a horizontal layout

Currently, I am utilizing ng-repeat 1.4 to dynamically generate elements within a div that has polymer shadow classes and uses the 'layout horizontal' style, resulting in code similar to: <div class='layout horizontal'> < ...

Form fields will not automatically populate with link parameters; the user must manually input the information

My form fetches data from the database using server-side code when the user inputs the "user_id" field. The User ID field is the primary field on the form, and when the user enters their user ID, all other fields retrieve the corresponding user data from t ...

"Internet Explorer text input detecting a keyboard event triggered by the user typing in a

It appears that the onkeyup event is not triggered in IE8/IE9 (uncertain about 10) when the enter button is pressed in an input box, if a button element is present on the page. <html> <head> <script> function onku(id, e) { var keyC = ...

Tips for achieving a full div image on hover

I'm having trouble with my CSS code for an image hover effect in a div. The image is appearing larger than the div and leaking out of it. How can I make sure the image stays contained within the div? Here's the updated CSS code: img { display ...

Utilizing AngularJS, implement ng-form and ng-repeat to seamlessly handle input validation and submission actions

Angular 1.6.2 I am experimenting with iterating over elements of a collection inputted by the user, checking their validity, and enabling the submit action if validation passes. I have tried using ng-form for this purpose and have come up with two differ ...