简体   繁体   中英

HTML table with 100% width, with vertical scroll inside tbody without td wdth

I have tried the solution posted by Hashem Qolami in post HTML table with 100% width, with vertical scroll inside tbody , but am having issues.

试图解决的问题

Let me fill you in, in case you require additional details.

I've tried multiple posts. Everywhere I have found only fixed width for td in tbody . But I require a dynamic width, according to the data in tbody .

It is working in sites when there are different sections of js, css. But not working when i combine everything to a single html. My Html image: HTML Image

 // Change the selector if needed var $table = $('table.scroll'), $bodyCells = $table.find('tbody tr:first').children(), colWidth; // Adjust the width of thead cells when window resizes $(window).resize(function() { // Get the tbody columns width array colWidth = $bodyCells.map(function() { return $(this).width(); alert("hii"); }).get(); // Set the width of thead columns $table.find('thead tr').children().each(function(i, v) { $(v).width(colWidth[i]); }); }).resize(); // Trigger resize handler 
 table.scroll { /* width: 100%; */ /* Optional */ /* border-collapse: collapse; */ border-spacing: 0; border: 2px solid black; } table.scroll tbody, table.scroll thead { display: block; } thead tr th { height: 30px; line-height: 30px; /* text-align: left; */ } table.scroll tbody { height: 100px; overflow-y: auto; overflow-x: hidden; } tbody { border-top: 2px solid black; } tbody td, thead th { /* width: 20%; */ /* Optional */ border-right: 1px solid black; /* white-space: nowrap; */ } tbody td:last-child, thead th:last-child { border-right: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <table class="scroll"> <thead> <tr> <th>Head 1</th> <th>Head 2</th> <th>Head 3</th> <th>Head 4</th> <th>Head 5</th> </tr> </thead> <tbody> <tr> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> </tr> <tr> <td>Content 1</td> <td>Lorem ipsum dolor sit amet.</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> </tr> <tr> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> </tr> <tr> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> </tr> <tr> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> </tr> <tr> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> </tr> <tr> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> </tr> </tbody> </table> 

Uncomment your table width

table.scroll {
  width: 100%;
  /* Optional */
  /* border-collapse: collapse; */
  border-spacing: 0;
  border: 2px solid black;
 }

Please try this

Cells are responding accordingly when resized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM