I need some tricky layout for my project.In this layout i have container div and a table with 100% width. I want to hide the vertical scroller of div element, but content of this div can be scrollable by mouse wheel. For this i have added style to hide the native scroll bar. It works fine when table width was greater than equal to container width. If the table with less than it container, table was not stretched properly with container div. I have assigned 100% width to table but table render to full div.
.content {
overflow-y: scroll;
padding-right: 17px;
box-sizing: content-box;
border: 1px solid grey;
height: 80px;
width: 100%;
}
.container {
width: 70%;
border: 1px solid grey;
overflow: hidden;
}
td {
border: 1px solid grey;
}
<div class='container'>
<div class='content'>
<table style='width: 100%; border-collapse: collpase; table-layout: fixed;'>
<colgroup>
<col style='width: 50px;'>
<col style='width: 50px;'>
<col style='width: 50px;'>
<col style='width: 50px;'>
<col style='width: 50px;'>
</colgroup>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
</div>
</div>
<table>
The code provided in question has a typo:
<table style='width:100%;border-collapse:
collpase;table-layout: fixed;' >
thereby rendering the <table>
to the default style:
border-collapse: separate;
Demo 1 has two <table>s
:
border-collapse: collapse
.content1 {width:105%;...}
.container1 {width:250px;...}
border-collapse: separate
or default<table style="width:105%;...">
.container2 {width:262px;...}
The widths for .container1
and .container2
were changed to fixed widths in order to be consistent with the fixed widths of the columns (each at width:50px
).
Demo 2 has two <table>s
:
border-collapse: collapse
.contentA {width:calc(100% + 17px;...}
.containerA {width:calc(70% + 17px;...}
border-collapse: separate
or default.contentA {width:calc(100% + 17px;...}
.containerA {width:calc(70% + 17px;...}
These <table>s
are dynamic in width, using calc()
css function it compensates for the fixed width of the scrollbar while maintaining relative width percentages.
Fixed Widths
.content1 {
overflow-y: scroll;
padding-right: 17px;
box-sizing: content-box;
border: 1px solid grey;
height: 80px;
/* Changed */
width: 105%;
}
.content2 {
overflow-y: scroll;
padding-right: 17px;
border: 1px solid grey;
height: 80px;
width: 100%;
}
.container1 {
/* Changed */
width: 250px;
border: 1px solid grey;
overflow: hidden;
}
.container2 {
/* Changed */
width: 262px;
border: 1px solid grey;
overflow: hidden;
}
td {
border: 1px solid grey;
}
<div class='container1'>
<div class='content1'>
<table style='width:100%;border-collapse: collapse;table-layout: fixed;'>
<caption>Table 1</caption>
<colgroup>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
</colgroup>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
</div>
</div>
<hr>
<div class='container2'>
<div class='content2'>
<!--Changed width-->
<table style='width:105%; table-layout: fixed;'>
<caption>Table 2</caption>
<colgroup>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
</colgroup>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
</div>
</div>
Dynamic Widths
.contentA {
overflow-y: scroll;
padding-right: 17px;
box-sizing: content-box;
border: 1px solid grey;
height: 80px;
width: calc(100% + 17px);
}
.containerA {
width: calc(70% + 17px);
border: 1px solid grey;
overflow: hidden;
}
td {
border: 1px solid grey;
}
<div class='containerA'>
<div class='contentA'>
<table style='width:100%;border-collapse: collapse;table-layout: fixed;'>
<caption>Table 3</caption>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
</div>
</div>
<hr>
<div class='containerA'>
<div class='contentA'>
<table style='width:100%;table-layout: fixed;'>
<caption>Table 4</caption>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
</div>
</div>
Remove padding-right: 17px;
from content
If you do not want scroll use hight:100%
and remove overflow-y: scroll;
.content {
box-sizing: content-box;
border: 1px solid grey;
height: 100%;
width: 100%;
}
.container {
width: 70%;
border: 1px solid grey;
overflow: hidden;
}
td {
border: 1px solid grey;
}
<div class='container'>
<div class='content'>
<table style='width:100%;border-collapse: collpase;table-layout: fixed;' >
<colgroup>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
<col style='width:50px;'>
</colgroup>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
</div>
</div>
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