Why is this HTML table too wide for its container? - html

I am building a page which will be holding a bunch of "leaderboard" style lists. During the development process everything was going well until I got to this latest section.
For some reason, even though each section is pretty much identical (aside from the actual numbers on the lists) this particular part breaks the layout.
Here is a JSFiddle of how it looked up the point where it broke:
https://jsfiddle.net/b91s32w8/
And here is a JSFiddle of how it looks now after adding in the code that broke it:
https://jsfiddle.net/w6rbx080/
The odd part is - that never happened until the following piece of code was added:
<table class="toplist-data">
<tbody class="toplist-body">
<th class="toplist-left"> No. </th>
<th class="toplist-middle"> Chat Name </th>
<th class="toplist-right"> Profile Visits </th>
<tr class="toplist-row">
<td class="toplist-data-left"> 1. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 300 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 2. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 250 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 3. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 200 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 4. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 170 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 5. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 150 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 6. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 120 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 7. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 110 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 8. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 90 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 9. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 50 </td>
</tr>
<tr class="toplist-row">
<td class="toplist-data-left"> 10. </td>
<td class="toplist-data-middle"> SomeRandomChatter </td>
<td class="toplist-data-right"> 25 </td>
</tr>
</tbody>
</table>
What I find strange about that is that piece of code uses the exact same CSS styling as the corresponding code from each of the other (working) sections. That CSS is as follows:
/* Individual TopList Tables */
table.toplist-data {
font-size: 14px;
width: 100%;
}
table.toplist-data th {
font-weight: bold;
padding-bottom: 5px;
margin-bottom: 5px;
}
.toplist-left, .toplist-data-left {
width: 5%;
text-align: left;
}
.toplist-middle, .toplist-data-middle {
width: 70%;
text-align: left;
padding-left: 20px;
}
.toplist-right, .toplist-data-right {
width: 20%;
text-align: right;
}
So if I can use that twice already without it breaking the layout, why is it breaking it on the third section? I can't see it being anything to do with the actual data within the table because it's pretty much identical.
Perhaps the answer is something so obvious I'm overlooking it, I don't know.

I think it's fixed when I removed display: table from .TLtabs

I actually fixed this myself shortly after posting by removing a "whitespace: nowrap" style that I had on a parent element.
As for the person who edited my question - Perhaps you'd like to explain what was wrong with me saying please and showing a bit of courtesy and politeness? And before you go correcting other people's grammar perhaps you should check your own! Read what you edited the title of my question to and come back to me when you spot your mistake!

Related

HTML Validator shows an error on a seemingly correct line of code

I am creating a table for an university task. Every once in a while we have to put our code into a validator. For some reason the validator keeps showing these errors:
https://i.ibb.co/dLnzTh2/error.png
The errors are about the <th> tag inside the <thead>.
<table style="width: 700px" >
<thead>
<tr>
<th colspan="8">Market Shares of Graphics Adapters in Q4 2013
</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">-</td>
<td rowspan="2">Q4 2013</td>
<td rowspan="2">Q3 2013</td>
<td colspan="4" rowspan="1" style="text-align: center">Quarter over Quarter Changes</td>
<td rowspan="2">2012</td>
</tr>
<tr>
<td colspan="2">Unit Shipments</td>
<td colspan="2">Share</td>
</tr>
<tr>
<td>AMD</td>
<td>18.30%</td>
<td>20.70%</td>
<td colspan="2">-10.40%</td>
<td colspan="2">-2.40%</td>
<td>19.70%</td>
</tr>
</tbody>
</table>
Any ideas what is causing this?
For reference this is how the whole table looks like:
https://i.ibb.co/LkMbWc9/table.png
This always means "a column without any cells" - harder to find with collspans, but possible - in the snippet Unit Shipments and Share are blue - this are doubled td, and it makes an empty column in both - since both are colspaned from the top.
Shortly - whole table is too wide of 2 not needed cells
td, th{ border: 1px solid red} /* this shows every th, td even empty */
td[colspan] { border: 1px solid blue} /* this shows colspans */
<table style="width: 700px" >
<thead>
<tr>
<th colspan="8">Market Shares of Graphics Adapters in Q4 2013
</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">-</td>
<td rowspan="2">Q4 2013</td>
<td rowspan="2">Q3 2013</td>
<td colspan="4" rowspan="1" style="text-align: center">Quarter over Quarter Changes</td>
<td rowspan="2">2012</td>
</tr>
<tr>
<td colspan="2">Unit Shipments</td>
<td colspan="2">Share</td>
</tr>
<tr>
<td>AMD</td>
<td>18.30%</td>
<td>20.70%</td>
<td colspan="2">-10.40%</td>
<td colspan="2">-2.40%</td>
<td>19.70%</td>
</tr>
</tbody>
</table>

Filling an automatic-width table cell with a fixed-layout table

This sounds very similar to previous questions, but I've not found something that matches what I'm trying to do here.
My current code (very verbose with everything in line) looks like this:
td { border: 1px solid black }
<table style="table-layout:fixed">
<tr>
<td>
<table style="table-layout:fixed">
<tr>
<td style="background-color:red"></td>
<td style="background-color:limegreen;width:30px"></td>
<td style="background-color:blue"></td>
<tr>
</table>
</td>
<td>
<table style="table-layout:fixed">
<tr>
<td style="background-color:red"></td>
<td style="background-color:limegreen;width:30px"></td>
<td style="background-color:blue"></td>
<tr>
</table>
</td>
<td>
<table style="table-layout:fixed">
<tr>
<td style="background-color:red"></td>
<td style="background-color:limegreen;width:30px"></td>
<td style="background-color:blue"></td>
<tr>
</table>
</td>
<td>
<table style="table-layout:fixed">
<tr>
<td style="background-color:red"></td>
<td style="background-color:limegreen;width:30px"></td>
<td style="background-color:blue"></td>
<tr>
</table>
</td>
</tr>
<tr>
<td style="width:25%;text-align:center">Some text here</td>
<td style="width:25%;text-align:center">More text</td>
<td style="width:25%;text-align:center">Hi</td>
<td style="width:25%;text-align:center">Somewhat longer text</td>
</tr>
</table>
What I'm trying to accomplish should look like this:
That is, the four main columns should all be the same width, which is the width of the largest content of any of the columns.
The green columns should always be 30px, and the red and blue columns should fill the remaining space each side of that middle column, only up to the width available in the auto-sized outer column.
Setting the internal tables to 100% width makes this happen, but of course the outer table then takes up the entire page width.
I'm also aware that using tables for this is probably not a great idea now we can use CSS, but I'd like to get this example working in tables before 'translating' it.
I'm very opposed to using JavaScript to solve this, for the record!
Edit: I also tried putting all 'subcolumns' in one row, and setting the text to colspan three at a time, with the 25% then applied to that. This ended up confusing the engine, and the width ended up about 75% of the page.
<table style="table-layout:fixed" border="1">
<tr style="font-size: 1px;">
<td width="25%" colspan="3"> </td>
<td width="25%" colspan="3"> </td>
<td width="25%" colspan="3"> </td>
<td width="25%" colspan="3"> </td>
</tr>
<tr style="font-size: 6px;">
<td style="background-color:red"> </td>
<td style="background-color:green"><div style="width: 30px;"> </div></td>
<td style="background-color:blue"> </td>
<td style="background-color:red"> </td>
<td style="background-color:green"><div style="width: 30px;"> </div></td>
<td style="background-color:blue"> </td>
<td style="background-color:red"> </td>
<td style="background-color:green"><div style="width: 30px;"> </div></td>
<td style="background-color:blue"> </td>
<td style="background-color:red"> </td>
<td style="background-color:green"><div style="width: 30px;"> </div></td>
<td style="background-color:blue"> </td>
</tr>
<tr>
<td colspan="3" style="text-align:center">Some text here</td>
<td colspan="3" style="text-align:center">More text</td>
<td colspan="3" style="text-align:center">Hi</td>
<td colspan="3" style="text-align:center">Somewhat longer text</td>
</tr>
</table>
this might solve your answer, your previous code html elements are not properly closed look on that also.

HTML space between td of a table

How is that possible that this work:
<TABLE>
<TR>
<TD onclick="play('cell1')" id="cell1">-</TD>
<TD onclick="play('cell2')" id="cell2">-</TD>
<TD onclick="play('cell3')" id="cell3">-</TD>
</TR>
<TR>
<TD onclick="play('cell4')" id="cell4">-</TD>
<TD onclick="play('cell5')" id="cell5">-</TD>
<TD onclick="play('cell6')" id="cell6">-</TD>
</TR>
<TR>
<TD onclick="play('cell7')" id="cell7">-</TD>
<TD onclick="play('cell8')" id="cell8">-</TD>
<TD onclick="play('cell9')" id="cell9">-</td>
</TR>
but if I put spaces between "-" it doesn't. I knew that it doesn't matter in HTML the position of elements(I mean, in this case). Why?
CSS solution:
If I get it right, you want to put - between two spaces, so you will simply need to simulate this using padding: 0px 5px; with your td elements, this is a snippet DEMO:
table td {
padding: 0px 5px;
}
<TABLE>
<TR>
<TD onclick="play('cell1')" id="cell1">-</TD>
<TD onclick="play('cell2')" id="cell2">-</TD>
<TD onclick="play('cell3')" id="cell3">-</TD>
</TR>
<TR>
<TD onclick="play('cell4')" id="cell4">-</TD>
<TD onclick="play('cell5')" id="cell5">-</TD>
<TD onclick="play('cell6')" id="cell6">-</TD>
</TR>
<TR>
<TD onclick="play('cell7')" id="cell7">-</TD>
<TD onclick="play('cell8')" id="cell8">-</TD>
<TD onclick="play('cell9')" id="cell9">-</td>
</TR>
</TABLE>
This will show - as " - " inside the td elements.
HTML solution:
If you want to use HTML only without CSS, the solution will be to use cellpadding=5 with your table, this is a working snippet:
<TABLE CELLPADDING=10>
<TR>
<TD onclick="play('cell1')" id="cell1">-</TD>
<TD onclick="play('cell2')" id="cell2">-</TD>
<TD onclick="play('cell3')" id="cell3">-</TD>
</TR>
<TR>
<TD onclick="play('cell4')" id="cell4">-</TD>
<TD onclick="play('cell5')" id="cell5">-</TD>
<TD onclick="play('cell6')" id="cell6">-</TD>
</TR>
<TR>
<TD onclick="play('cell7')" id="cell7">-</TD>
<TD onclick="play('cell8')" id="cell8">-</TD>
<TD onclick="play('cell9')" id="cell9">-</td>
</TR>
</TABLE>
But this will make spaces between tr elements too, in other words it will make padding-top and padding-bottom too for your td elements.
Conclusion:
So your requirements will be better achieved using paddingin CSS, now it's up to you to choose the right solution.

How make a row with different columns in html

My question is that how can I make a row with different columns quantity ?
For example I want to have 2 columns in last row in this picture (the portion of each cell must be 50%).
Another question that I have is that how can I make text starts from first line in a cell (center cell , in this picture) ?
My code is :
table {
border: 1px solid black;
border-collapse: collapse;
border-spacing: 5px;
}
th,
td {
padding: 10px;
}
<table style="width:100%" border="1px" cellpadding="5" cellspacing="5" bordercolor="green" bgcolor="yellow" height="500px">
<caption>web design homework</caption>
<tr>
<th colspan="3">My Website</th>
</tr>
<tr bgcolor="#77E022" height="20px" align="left">
<td colspan="3">
home products contact us
</td>
</tr>
<tr>
<td width="25%">last post</td>
<td rowspan="2" width="50%">hello my name is mohammad ghorbani and i am studying computer enginerring in arak</td>
<td>our friends</td>
</tr>
<tr>
<td>our statics</td>
<td>24</td>
</tr>
<tr>
<td>our social pages</td>
<td>about us</td>
</tr>
</table>
There's two primary answer categories to your question.
First, the direct answer. Think of your page as a grid. You want enough squares on the grid to be divisible by both 3 and 2. Say, 6. Then use colspan to set each column to the number of grid columns that would be needed -- so, colspan=2 for 3 columns, and colspan=3 for 2 columns.
<table border=1>
<tr>
<td colspan=6>My Website</td>
</tr>
<tr>
<td colspan=6>home, products, contact us</td>
</tr>
<tr>
<td colspan=2 style="width:33%">last post</td>
<td colspan=2 rowspan=2 style="width:33%">hello my name</td>
<td colspan=2 style="width:33%">our friends</td>
</tr>
<tr>
<td colspan=2 style="width:33%">our statics</td>
<td colspan=2 style="width:33%">24</td>
</tr>
<tr>
<td colspan=3 style="width:50%">our social pages</td>
<td colspan=3 style="width:50%">about us</td>
</tr>
</table>
The other answer category is that you should avoid using tables for your layout structure. There's a LOT of Google results for this one, and it's very opinion based, so I'll just say that generally tables should be used for data, css for layouts, and using tables for layouts may be quicker but it's less flexible.
Try this, its working well, hope it will resolve your issue.
Add this class
.column{display:inline-block; width:49%;}
<table style="width:100%" border="1px" cellpadding="5" cellspacing="5" bordercolor="green" bgcolor="yellow" height="500px">
<caption>web design homework</caption>
<tr>
<th colspan="3">My Website</th>
</tr>
<tr bgcolor="#77E022"
height="20px" align="left" >
<td colspan="3" >
home products contact us
</td>
</tr>
<tr>
<td width="25%"> last post </td>
<td valign="top" rowspan="2" width="50%"> hello my name is mohammad ghorbani and i am studying computer enginerring in arak </td>
<td> our friends </td>
</tr>
<tr>
<td> our statics </td>
<td> 24 </td>
</tr>
<tr>
<td colspan="3" valign="top">
<div class="column"> our social pages</div>
<div class="column"> about us </div>
</td>
</tr>
</table>

Space on html table

This is an easy question, but I cannot seem to solve it. My html table can be seen at the following:
http://jsfiddle.net/Rochefort/kvUKG/
And also included here:
<table style="background:#fff;width:300px;margin-left:14px;" class="form">
<tbody>
<tr style="">
<td class="bosluk"></td>
<td class="alis_baslik"><strong>ALIŞ</strong></td>
<td class="satis_baslik"><strong>SATIŞ</strong></td>
</tr>
<tr style="border-bottom:1px solid #e4e4e4;">
<td class="ikonlar"><strong>$ </strong></td>
<td class="kurlar">DOLAR</td>
<td class="alis">2.2804</td>
<td class="satis">2.2914</td>
</tr>
<tr style="border-bottom:1px solid #e4e4e4;">
<td class="ikonlar"><strong>$ </strong></td>
<td class="kurlar"><strong>DOLAR</strong></td>
<td class="alis">2.2804</td>
<td class="satis">2.2914</td>
</tr>
<tr style="border-bottom:1px solid #e4e4e4;">
<td class="ikonlar"><strong>$ </strong></td>
<td class="kurlar"><strong>DOLAR</strong></td>
<td class="alis">2.2804</td>
<td class="satis">2.2914</td>
</tr>
<tr style="border-bottom:1px solid #e4e4e4;">
<td class="ikonlar"><strong>$ </strong></td>
<td class="kurlar"><strong>DOLAR</strong></td>
<td class="alis">2.2804</td>
<td class="satis">2.2914</td>
</tr>
</tbody>
</table>
I implemented CSS but the DOLLAR item has too much space. How can I remove this extra space?
You can use text-align: right with padding-right instead of padding-left.
Example: http://jsfiddle.net/BfD2v/
.ikonlar {
padding-right:5px;
font-family: Arial;
font-size:12px;
font-weigth: bold;
color: #000;
text-align: right;
}
If you want to make the whole column narrower, you can set the width od the column with this:
.bosluk, .ikonlar {
width: 10px;
}
You should also probably use <th> tags for the headers. The columns would align themselves under the <td> tags then. Like:
<table border="1">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>