Tips for adjusting the table's height and width in a container while also including horizontal and vertical scroll bars in the <tbody> to prevent overflow

Despite adding height and width adjustments in the table tag and setting the overflow properties to 'auto', the table still overflows. Attempting to follow this, it doesn't seem to work with my code. Any assistance in identifying mistakes or missing elements in the code would be greatly appreciated.

<style>
    table {
        max-width: 600px;
        max-height: 400px;
        border-spacing: 0;
        display: block;
    }
    
    tbody {
        overflow-y: auto;
        overflow-x: auto;
    }
    
    th {
        /*display: block;*/
        border: 1px solid #293033;
        position: sticky;
        top: 0;
        text-align: center;
        padding: 8px;
        color: #e9ecef;
        background-color: #1e2324;
        font-weight: 500;
        font-size: 14px;
    }
    
    td {
        background-color: #041230;
        border: 1px solid #293033;
        text-align: center;
        padding: 8px;
        color: #e9ecef;
        min-width: 150px;
        font-size: 13px;
        word-spacing: 2px;
    }
    
    td:nth-child(1) {
        min-width: 50px;
        max-width: 50px;
        font-weight: bold;
        color: #66FCF1;
    }
</style>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="resCSS.css">

<script src="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script>
<script src="https://cdn.fancygrid.com/fancy.min.js"></script>

<div class="container">
    <table class="tablecolor2">
        <!-- class="tablecolor" -->
        <thead>
            <tr>
                <th>#</th>
                <th>Dummy</th>
                <th>dvfdgdg</th>
                <th>fvfdgfd/fdgdg</th>
                <th>Cdumm</th>
                <th>sdfdgds</th>
                <th>Cd</th>
                <th>Ankjfdksf</th>
                <th>jnknk</th>
                <th>fdfdgdfg</th>
            </tr>
        </thead>
        <tbody>
            ... (content continues)
        </tbody>
    </table>
</div>

https://i.sstatic.net/t9XOB.png https://i.sstatic.net/mUExR.png

Answer №1

Referencing this specific answer provided:

  • Avoid overflowing the tbody element, rather utilize a parent container such as DIV
  • Avoid applying the style display: block; directly on a table.

/*QuickReset*/*{margin:0;box-sizing:border-box;}

.tableFixHead          { overflow: auto; }
.tableFixHead thead th { position: sticky; top: 0; }

/* CUSTOM SIZE */
.tableFixHead {
  height: 90vh; /* or whatever */
  min-width: 600px;
}

/* OTHER STYLES */
table { border-spacing: 0; }
th,
td {
  text-align: center;
  padding: 8px;
  color: #e9ecef;
  border: 1px solid #293033;
}
th {
  background-color: #1e2324;
  font-weight: 500;
  font-size: 14px;
}
td {
  min-width: 150px;
  background-color: #041230;
  font-size: 13px;
  word-spacing: 2px;
}
td:first-child {
  min-width: 50px;
  font-weight: bold;
  color: #66FCF1;
}
<div class="container tableFixHead">
  <table class="tablecolor2">
    <thead>
      <tr>
        <th>#</th>
        <th>Dummy</th>
        <th>dvfdgdg</th>
        <th>fvfdgfd/fdgdg</th>
        <th>Cdumm</th>
        <th>sdfdgds</th>
        <th>Cd</th>
        <th>Ankjfdksf</th>
        <th>jnknk</th>
        <th>fdfdgdfg</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>cdfdfsdfs</td>
        <td> ssdfsf dfdsgsdgsdg typesetter in the 15th century who is thought to have scrambledon </td>
        <td>ILorem ipsum, or lipsum as it is sometimes </td>
        <td>known, is </td>
        <td>dummy text used in laying out print, </td>
        <td>N/A</td>
        <td>graphic or web </td>
        <td>N/A</td>
        <td>designs. The passage is attributed to an unknown </td>
      </tr>
      <!-- Additional rows removed for brevity -->
      <tr>
        <td>1</td>
        <td>cdfdfsdfs</td>
        <td> ssdfsf dfdsgsdgsdg typesetter in the 15th century who is thought to have scrambledon </td>
        <td>ILorem ipsum, or lipsum as it is sometimes </td>
        <td>known, is </td>
        <td>dummy text used in laying out print, </td>
        <td>N/A</td>
        <td>graphic or web </td>
        <td>N/A</td>
        <td>designs. The passage is attributed to an unknown </td>
      </tr>
      » Additional rows go here
    </tbody>
  </table>
</div>

Answer №2

Is this what you're looking for?

<!DOCTYPE html>
<html>

<head>

  <style>
    table {
      max-width: 600px;
      max-height: 400px;
      border-spacing: 0;
      display: block;
    }
    
    tbody {
      overflow-y: auto;
      overflow-x: auto;
    }
    
    th {
      /*display: block;*/
      border: 1px solid #293033;
      position: sticky;
      top: 0;
      text-align: center;
      padding: 8px;
      color: #e9ecef;
      background-color: #1e2324;
      font-weight: 500;
      font-size: 14px;
    }
    
    td {
      background-color: #041230;
      border: 1px solid #293033;
      text-align: center;
      padding: 8px;
      color: #e9ecef;
      min-width: 150px;
      font-size: 13px;
      word-spacing: 2px;
    }
    
    td:nth-child(1) {
      min-width: 50px;
      max-width: 50px;
      font-weight: bold;
      color: #66FCF1;
    }
    
    .container {
 overflow: scroll;
    }
  </style>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="resCSS.css">

  <script src="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script>
  <script src="https://cdn.fancygrid.com/fancy.min.js"></script>
</head>

<body>

  <div class="container">
    <table class="tablecolor2">
      <!-- class="tablecolor" -->

      <thead>
        <tr>
          <th>#</th>
          <th>Dummy</th>
          <th>dvfdgdg</th>
          <th>fvfdgfd/fdgdg</th>
          <th>Cdumm</th>
          <th>sdfdgds</th>
          <th>Cd</th>
          <th>Ankjfdksf</th>
          <th>jnknk</th>
          <th>fdfdgdfg</th>
        </tr>
        <tbody>
          <tr>
            <td>1</td>
            <td>cdfdfsdfs</td>
            <td> ssdfsf dfdsgsdgsdg typesetter in the 15th century who is thought to have scrambledon </td>
            <td>ILorem ipsum, or lipsum as it is sometimes </td>
            <td>known, is </td>
            <td>dummy text used in laying out print, </td>
            <td>N/A</td>
            <td>graphic or web </td>
            <td>N/A</td>
            <td>designs. The passage is attributed to an unknown </td>
          </tr>
          <!-- Additional rows similar to above -->

        </tbody>
    </table>


  </div>
  <script src="research.js"></script>
  <script src="research2.js"></script>
</body>

</html>

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

Center-aligning images in Bootstrap Carousel

I've implemented the Bootstrap 4 Carousel on my website and it's functioning properly. However, I'm facing an issue where I need to align the slider image in the center of the carousel. Currently, the image is aligned to the left side, caus ...

Showing a collection of cards within a dynamic container set up in a 3x3 grid layout to start

In an attempt to showcase cards within a responsive container utilizing bootstrap and django, my goal is to create a 3x3 grid layout on extra-large screens with scrollable overflow that adjusts based on device width. Initially, I experimented with wrapping ...

Using just a simple HTML code, the d-flex feature in Bootstrap seems to be malfunctioning

Embarking on a new Bootstrap website project has presented me with a challenge that I have not been able to overcome, even with minimal HTML and just one flex item (which happens to be the only element on the site): <!DOCTYPE html> <html lang=" ...

Adding a half circle connector in HTML can be accomplished by using SVG (Scal

My task is to replicate the construction shown in this image: https://i.sstatic.net/kaRMO.png I have written the entire code but I am unsure how to include a half circle next to the full circle and connect it with a line connector. Here is my code: .ps- ...

Crafting a template for mixing up images and quotes without any specific connection - here's how!

I came across some interesting templates that can generate random quotes and others that create random pictures. My goal is to develop a template that generates both a random quote and a random picture, similar to this example, without any relation betwee ...

Switching between pages, displaying new content within a div without changing the URL address

I have experience working with the Ajax/jQuery .load() or php include() functions to load additional content into a div on a web page. Please check out this example I've created: my website featuring page transitions The setup for this page involves ...

Leveraging the power of the jQuery load function alongside HTML forms in web

Currently in the process of creating a fresh website and considering implementing jQuery's load() function to bring content from various html pages into my main page. load() is functioning smoothly. Below is my current code: Main.html page where cont ...

What is the best way to dynamically add getJSON's data to a div whenever the loadmore button is clicked?

When a page loads, my getJSON function displays its output in a div called myDiv. Now, I am looking to add a button at the bottom of the page. When the user clicks this button, I want to trigger another call to getJSON. Each time the button is clicked, I ...

Looking to retrieve a specific data element within Vue.js?

Here's a thought-provoking query for you. Imagine I have some data stored in an element in the form of an array like this: data: { product: socks, variants: [ { variantId: 2234, variantColor: 'Green', varian ...

Declaration of Character Encoding in CSS3

Do I need to include a character encoding declaration in CSS3? When I add the @charset "utf-8"; declaration, Visual Studio's CSS3 validation displays a warning. Can anyone offer some guidance on this issue? ...

Discovering Characters in String using jQuery and Implementing a Personalized Class

Update: To achieve the desired typography, I will need to implement some creative jquery / css techniques. The first step is to create a class called .underlined which will allow me to have complete control over the underline by setting a background imag ...

Different ways to position two header tags on top of each other instead of next to each other

I am attempting to adjust the placement of my header tags so that the second header sits directly below the first one, rather than side by side. Currently, I am using h1 and h2 tags for my headers. I have experimented with placing both headers inside an h ...

Text area capacity

Is there a limit to the maximum capacity of a textarea for accepting text? The HTML page functions correctly when the text is limited to around 130-140 words. However, if the text exceeds this limit, it causes the page to hang without any response. The tex ...

Error encountered: Failure to construct URL for endpoint 'Bookdetails'. Have you overlooked supplying values for the parameter 'book_id'?

I have been troubleshooting this issue and reviewed similar threads, yet despite verifying all options, the problem persists. My code includes: application.py @app.route("/Booksearch/<int:book_id>", methods=["GET", "POST"]) def Bookdetails(book_id ...

How come my database is not receiving the data from the first name field?

I have encountered an issue with my comment form where the first name field data is not getting sent to the database along with the comments. I have already checked that the 'first_name' column exists in the database, but still facing this proble ...

div not recognizing the minimum height specified in percentage

I'm encountering an issue where one of my divs, content_wrap, is not adhering to the CSS property min-height:100% HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ...

Can Angular toggle visibility of static HTML content?

My website needs to display different services with well-formatted HTML code already in place. I want to show only one service's description at a time, with the option to switch between them by clicking on different service categories. I feel like I ...

What is the best way to ensure that my text always aligns perfectly to the end of the line?

I've been struggling to create a column layout where the text perfectly aligns with the end of the line, but so far, I haven't had any success. Despite my efforts to find a solution online, I have come up empty-handed. What I'm aiming for i ...

Creating dynamic CSS classes with SCSS/SASS

Recently, I encountered a scenario where I needed to automate the creation of a series of CSS classes by utilizing a mixin. This led me to ponder if there is a dynamic approach to achieve this. Essentially, the classes I have are structured as follows: . ...

Tablet-friendly dropdown menu

Seeking advice on implementing @media queries for tablets with a width of 991px. Currently, the CSS file is optimized for mobile devices but needs adjustments for tablet responsiveness in the dropdown menu. I attempted the following CSS code: @media (max ...