Table rows not aligning - html

I have two tables in the following fiddle. The rows are not aligned to the header. This is my code for first table
<table class=" table-bordered">
<tr>
<th>Doc Name:</th>
<th>Atom Type:</th>
<th>Cluster Method:</th>
<th>k:</th>
<th>
<small>Stylistic Feature(s):</small>
</th>
</tr>
<tr>
<td style="font-size: 10px;">rowling_and_dickens</td>
<td style="font-size: 10px;">sentence</td>
<td style="font-size: 10px;">kmeans</td>
<td style="font-size: 10px;">2</td>
<td style="font-size: 10px;">hapax_dislegomena;honore_r_measure;hapax_legomena;</td>
</tr>
</table>
The other table follows a similar pattern. I am not adding them here due to paucity of space.
https://jsfiddle.net/fk6fnto4/
My table rows do not get aligned properly no matter what I do. I have looked at many solutions and modified the code accordingly, to no avail. Please help me identify what is wrong here.

To make column content aligned, we need to set its width to be fixed. Currently each cell takes as much space as it needs. It seems that the first column should have the smallest width, and the third one should be widest. Other columns could be same width since they all contain 5 digit numbers. To set different td widths we could use different classes for each, but it could be messy, so we'll use nth-child() selector which looks like this:
td:nth-child(1) {
width: 10%
}
This sets width of all td elements that are the first child of their parents to 10%. What you should do is add this to your style so it looks like this:
<style>
.container {
margin-left: 0;
}
tbody {
display: block;
max-height: 500px;
overflow-y: scroll;
}
thead,
tbody tr {
display: table;
width: 100%;
}
thead {
width: 100%;
}
table {
width: 200px;
}
td:nth-child(1) {
width:10%
}
td:nth-child(3) {
width:30%
}
</style>
What this means is that the first column will take up 10% of tbody width, the third one will take up 30%, and others will use 20% each. However, if those can also contain data of variable length, you should add similar rules for them too.
This way, column content will be aligned to center. To set different alignment use text-align property.

Related

Making table row height not changing, when increasing the whole table height

When I increase the table height, all the rows get resized and the additional height is distributed equally. among them.
Question
Is it possible to make a row (in my example the one with headers) always stay at it's minimum height? As an analogy I see it as specifying flex-grow: 0 on a Flex item.
No fixed height
I don't want to make that row fixed height (e.g. set on it height: <fixed value in px>), just make it's height the natural minimum to render all the contents.
Code
FIDDLE with the example code to work on. Screenshot below.
I want to make the first row in the right table (.Table-Row--NotResizable) to be the same height as the first row in the left table.
HTML
<div class="TableDisplay">
<table class="Table Table--Natural">
<tr>
<th>Artist</th>
<th>Song</th>
</tr>
<tr>
<td>Prince</td>
<td>Kiss</td>
</tr>
<tr>
<td>Bob Dylan</td>
<td>Idiot Wind</td>
</tr>
</table>
<table class="Table Table--Full">
<tr class="Table-Row--NotResizable">
<th>Artist</th>
<th>Song</th>
</tr>
<tr>
<td>Prince</td>
<td>Kiss</td>
</tr>
<tr>
<td>Bob Dylan</td>
<td>Idiot Wind</td>
</tr>
</table>
</div>
CSS
html,
body {
height: 100%;
min-height: 100%;
}
.TableDisplay {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
height: 100%;
}
.Table {
height: 100%;
border-collapse: collapse;
}
.Table td,
.Table th {
border: 1px solid black;
}
.Table--Full {
height: 100%;
}
.Table--Natural {
height: auto;
}
/* Make this row do not participate in height changes */
.Table-Row--NotResizable {
/* ??? */
}
In fact fixed value in px is exactly what you should use:
.Table-Row--NotResizable {
height: 1px;
}
If you set it to 1px then the browser will resize it to exactly the size needed to fit the content. Table content has to fit into table cell, so the height will not be smaller, and as any (non-empty) content will be higher than 1px it will also not be greater than minimum needed.
I've been scratching my head at this for ages but I finally found a solution that works for me which slightly differs from the current answer.
In order to prevent each <Tr> from resizing to match the <Table> height, I set the height of each of my <Tr> to 1px, however, to stop these rows from resizing, I had to add an empty final <Tr> that does not contain any data to the end of my <Table>. It seems this behaviour is because the <Table> element by default needs the <Tr> elements to sum up to the total height of the table, and the empty <Tr> element stretches to fill this height whilst the rows containing data can be sized to their content.

table - td width in percentage with overflow not working

I am trying to build a table that contains a td which has a width set in percentage and when overflown a horizontal scrollbar.
Unfortunately I don't manage to make this happen.
http://jsfiddle.net/ne45s2wf/1/
HTML
<div class="container">
<table>
<tbody>
<tr>
<td>cell 1
</td>
<td>cell 2
</td>
<td class="too-long">cell 3 loooooooooooooooooooooooooooooooooooooooooooong
</td>
</tr>
</tbody>
</table>
</div>
CSS
.container {
position: relative;
max-width: 500px;
background-color: red;
}
table {
width: 100%;
}
td.too-long {
background-color: darkgreen;
display: inline-block;
width: 20%;
overflow-x: scroll;
}
First thing I wonder is what is the td-width in percentage relative to? And is it possible to set it to be relative to the table?
I would set a maximum width in percentage for the td with overflow hidden. While this works for the td, the parent containers do not align their width to the td child when its width is set with percentage. The parents width is as if the child did not have any width set. Furthermore the table now is not "responsive" any more.
I would take a look at bootstrap. I am not sure exactly what you mean but it seems like you are having trouble with your tables overflowing. Bootstrap has responsive tables which will scroll in the way you specify at small sizes. Take a look at this:
http://getbootstrap.com/css/#tables-responsive

Resizable div with overflow

This question may look stupid for some. My apologies, I am not that savy with CSS.
Now, I have a table with 2 columns (needs to be equal). The table width is put to 100% and needs to be resized with the page.
The header, it's ok. It only contains 2 cells with 2-3 words, so there are no problems.
Now, each of the following cells contain quite some big text and I need to be displayed as it is (no wrapping).
What I did:
<style>
.contentDiv
{
display: inline-block;
overflow-x:auto;
overflow-y:hidden;
white-space: nowrap;
white-space: nowrap;
}
</style>
<table style="width:100%">
<th>
<tr><td with="50%">head 1</td><td width="50%">head 2</td></tr>
</th>
<tbody>
<tr>
<td><div class="contentDiv">big-text-here</div></td>
<td><div class="contentDiv">big-text-here</div></td>
</tr>
</tbody>
</table>
The bottom line is I want the table to have the full width, the inner content-cell to have half of the table width and the text (no matter how big it is) not to alter the layout (scrollbars should appear if the content is bigger than the required surface).
Thanks!
I kinda didn't know what you mean, but I am sure you mean this http://jsfiddle.net/mDXb5/ also you had some problems with your code above that you should edit. e.g:
</table> tag is missing instead you use </tr> which is wrong
This should eliminate any wrapping.
td { position: relative; }
.contentDiv
{
display: inline-block;
overflow-x:auto;
overflow-y:hidden;
white-space: nowrap;
white-space: nowrap;
position: absolute;
}

Change width of TABLE based on its TD cells

I am looking for a way to specify a table's width by specifying widths of its TDs.
In the following scenario (try it live on jsfiddle) you can see that I have specified width of each TD as 100px and I expected to get a 300px table (and a horizontal scrollbar for div) but in practice browsers give them a width of 63px (that's table's width divided by 3)
Is there any way to make TDs determine the width of table and not other way round? So far I have tried different values of table-layout, display, overflow for TD and TABLE without any success.
The html:
<div>
200px
<table>
<tr>
<td>100px</td>
<td>100px</td>
<td>100px</td>
</tr>
</table>
</div>
and a minimal CSS:
div {
width: 200px;
height: 300px;
border: solid 1px red;
overflow-x: scroll;
}
td {
width:100px;
border: solid 1px #000;
}
table {
border-collapse: collapse;
}
A simple solution is make the td's content be 100px wide.
<div>
200px
<table>
<tr>
<td><div class="content">100px</div></td>
<td><div class="content">100px</div></td>
<td><div class="content">100px</div></td>
</tr>
</table>
</div>
.content {
width: 100px;
}
Simplest solution appears to be setting min-width instead of width for TDs.
If you're dynamically generating the table, you could just dynamically set the width of the table while you're at it. Just calculate the desired width, and add style="width:300px;" (or whatever) to the <table> tag.
Not that the other options people have posted here aren't also perfectly valid, of course.

How to create a table with 100% width but having a dynamic column layout?

I want to create a table that is fully contained within its parent element, but having column widths that are resolved based on their content. If the required length of the table is longer than the content box of the parent element, then a horizontal scrollbar shall appear underneath the table. I tried fiddling with the table-layout and overflow properties, but without success.
HTML code:
<div>
<table>
<tr>
<td>fixed_length_text</td>
<td>variable_length_text</td>
<td>image</td>
<td>double_float_double_float</td>
</tr>
<tr>
<td>fixed_length_text</td>
<td>variable_length_text_variable_length_text</td>
<td>image</td>
<td>double_float_double_float</td>
</tr>
</table>
</div>
CSS code:
div {
padding: 10px;
background: grey;
width: 400px;
}
table {
border-collapse: separate;
border-spacing: 2px;
background: white;
}
tr {
background: green;
}
This is what I have tried on jsFiddle. Is there anyway to combine the best of both worlds?
Try overflow-x:auto;. This applies to just the horizontal axis of the element.
if i understand you right than:
http://jsfiddle.net/nfg34/1/