0

<table style="text-align: left;width: 100%; float:left" cellpadding="0" cellspacing="1" border="1">    
        <tr>
            <th style="width:25%">head</th>
            <th style="width:25%">head</th>
            <th style="width:25%">head</th>
            <th style="width:25%">head</th>
        </tr>    
</table>
 <div style="height:100px; overflow-y:scroll; width:100%; float:left;">
<table style="text-align: left;width: 100%; float:left" cellpadding="0" cellspacing="1" border="1">    
    <tbody>
        <tr>
            <td style="width:25%">row</td>
            <td style="width:25%">row</td>
            <td style="width:25%">row</td>
            <td style="width:25%">row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>   
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr> 
    </tbody>
</table>
</div>

In the above code i got 2 html tables

Table 1: For header

Table 2: For Body

For Table 2 i kept a div for scrolling,this is my requirement.

when this div is there alignment issues are coming,if i remove the div then both tables alignment is perfect

Can you please help me in fixing this.

stacker
  • 19
  • 6

2 Answers2

0

You can add 1 more div to table 1 and give a margin-right

<div style="margin-right: 15px;">
<table style="text-align: left;width: 100%; float:left;" cellpadding="0" cellspacing="1" border="1">    
        <tr>
            <th style="width:25%">head</th>
            <th style="width:25%">head</th>
            <th style="width:25%">head</th>
            <th style="width:25%">head</th>
        </tr>    
</table>
</div>
 <div style="height:100px; overflow-y:scroll; width:100%; float:left;">
<table style="text-align: left;width: 100%; float:left" cellpadding="0" cellspacing="1" border="1">    
    <tbody>
        <tr>
            <td style="width:25%">row</td>
            <td style="width:25%">row</td>
            <td style="width:25%">row</td>
            <td style="width:25%">row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>   
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr>
        <tr>
            <td>row</td>
            <td>row</td>
            <td>row</td>
            <td>row</td>
        </tr> 
    </tbody>
</table>
</div>
Sonia
  • 1,909
  • 1
  • 11
  • 13
0

// 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();
    }).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;
}

tbody td:last-child, thead th:last-child {
    border-right: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/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>

Source:

HTML table with 100% width, with vertical scroll inside tbody http://jsfiddle.net/hashem/CrSpu/554/

dcangulo
  • 1,888
  • 1
  • 16
  • 48