Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical scrollbar displaces table rows HTML

Tags:

html

css

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">&nbsp;</div>
                <div class="divcell cellwidth2">&nbsp;</div>
                <div class="divcell cellwidth3">&nbsp;</div>
                <div class="divcell cellwidth4">&nbsp;</div>
            </div>
            <div class="divrow">
                <div class="divcell cellwidth1">&nbsp;</div>
                <div class="divcell cellwidth2">&nbsp;</div>
                <div class="divcell cellwidth3">&nbsp;</div>
                <div class="divcell cellwidth4">&nbsp;</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%; }
like image 314
Jenny Avatar asked Nov 12 '13 20:11

Jenny


People also ask

How do I stop the scroll bar from moving in CSS?

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.

How do I add a vertical scroll bar in HTML?

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.

How do I make a scroll table in HTML?

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.


2 Answers

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

like image 85
Dan Green-Leipciger Avatar answered Oct 24 '22 14:10

Dan Green-Leipciger


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.

like image 30
vernak2539 Avatar answered Oct 24 '22 14:10

vernak2539