I am working with a table design that should have its height and width full-expandable.
The table works great, but I cannot adjust or fit the column names position over the table columns due the scrollbars width. When the scrollbar is visible (and it should be always visible) the table rows are being displaced and their columns names looks bad.
Can you please give me some thoughts?
Is there any way to change the scrollbar z-index or something that avoids the displacing behavior?
Here is a jsFiddle: See sample here
HTML:
<div id="tablecontainer">
<div id="topbar">
<div class="colname cellwidth1">ABC</div>
<div class="colname cellwidth2">ABC</div>
<div class="colname cellwidth3">ABC</div>
<div class="colname cellwidth4">ABC</div>
</div>
<div class="breakline"></div>
<div id="expandtable">
<div class="divrow">
<div class="divcell cellwidth1"> </div>
<div class="divcell cellwidth2"> </div>
<div class="divcell cellwidth3"> </div>
<div class="divcell cellwidth4"> </div>
</div>
<div class="divrow">
<div class="divcell cellwidth1"> </div>
<div class="divcell cellwidth2"> </div>
<div class="divcell cellwidth3"> </div>
<div class="divcell cellwidth4"> </div>
</div>
</div>
<div class="breakline"></div>
<div id="topbar">
<div class="colname cellwidth1">ABC</div>
<div class="colname cellwidth2">ABC</div>
<div class="colname cellwidth3">ABC</div>
<div class="colname cellwidth4">ABC</div>
</div>
</div>
CSS:
html, body, #expandtable, #tablecontainer
{
height:100%;
margin: 0;
padding: 0;
border: none;
overflow-y: hidden;
}
#tablecontainer
{
width: 100%;
margin: 0 auto;
padding-top: 50px;
max-width: 900px;
}
#expandtable
{
margin: 5px 0 0 0px;
overflow-x: hidden;
overflow-y: scroll;
height: 60%;
border-bottom: 0;
background-color: #eee;
margin: 0 auto;
}
.breakline { clear:both;}
.divcell
{
float:left;
border: 1px solid #999;
box-sizing: border-box;
min-height: 30px;
}
.colname
{
float:left;
border: 1px solid #e5e5e5;
box-sizing: border-box;
}
.cellwidth1 { width:10%; }
.cellwidth2 { width:45%; }
.cellwidth3 { width:35%; }
.cellwidth4 { width:10%; }
The easy fix is to use width: 100% instead. Percentages don't include the width of the scrollbar, so will automatically fit. If you can't do that, or you're setting the width on another element, add overflow-x: hidden or overflow: hidden to the surrounding element to prevent the scrollbar.
For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.
The approach of this article is to create table with 100% width using width property and create vertical scroll inside table body using overflow-y property. Overflow property is used to create scrollbar in table. Use display: block; property to display the block level element.
If you want more control over the scrollbar, you can use a jQuery plugin such as this: http://www.yuiazu.net/perfect-scrollbar/
However, before you even start with that you should read up on the proper use of <table>
Instructions for this can be found here: http://www.w3schools.com/html/html_tables.asp
Well it works in Chrome on Mac (at least it's a start. Scrollbar hovers over content instead of displacing it).
With what you have, it might not be possible to factor this change in with just CSS. You might have to use Javascript to find the width of the columns below the header, then adjust the column header's widths accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With