HTML Table Styling - html

I'm encountering a problem when styling an dynamic generated table. The user can choose how many columns there have to be, some of them have got a fixed length. How can I let the other give a percentage of the space left, without having to specify the exact width of the columns every time AND without ending up with different column widths with different data/different browsers?
Example:
<style type="text/css">
table{
width:800px;
border:1px solid #CCCCCC;
/* table-layout: fixed; */
}
table td {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
border:1px solid #EEEEEE;
}
table tbody td.active{
text-align:center;
width:100px; /* fixed */
}
table tbody td.option{
width:100px; /* fixed */
}
table tbody td.nonfixed{
width:auto;
}
</style>
<table>
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Active</td>
<td colspan="2">Options</td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5">+ Add new row<td>
</tr>
</tfoot>
<tbody>
<tr>
<td class="nonfixed">[Name 1]</td>
<td class="nonfixed">[Description 1]</td>
<td class="active">[X]</td>
<td class="option">Edit</td>
<td class="option">Delete</td>
</tr>
<tr>
<td class="nonfixed">[Name 2]</td>
<td class="nonfixed">[Description 2]</td>
<td class="active">[0]</td>
<td class="option">Edit</td>
<td class="option">Delete</td>
</tr>
</tbody>
</table>
In the example both "nonfixed" columns should have the exact same width. This should also work when the user adds a nonfixed column or switches the first column with the last etc.
Who's able to help me out?

I see two possible approaches... either use a script to calculate the flexible-width columns' widths and average them, or use nested tables to split the two flex cols at 50%:
<table>
<tr>
<td class="fixed"></td>
<td class="fixed"></td>
<td class="fixed"></td>
<td class="flex-wrapper">
<table width="100%">
<tr>
<td width="50%"></td>
<td width="50%"></td>
</tr>
</table>
</td>
</tr>
</table>

Related

Margin-right property not working?

I'm trying to create table which looks like below:
I have tried using margin-right property ,and also border spacing ,colspan but not able to achieve it!
Here is my link to codepen:https://codepen.io/saisree/pen/jwwwEZ
<tr >
<td style="padding-top:10px;"colspan="2"class=" fixed text-center">
<span class=" border6 bold pull-left">2210 924-16(267) </span><span class="boxed1"></span></tr>
Any kind of help is highly appreciated.
You need two tds in each tr. In the first 4 rows you apply the attribute colspan="2" to the second td tag, in the rows after that you apply colspan="2" to the first td tag. That's basically all. So actually, there are three TDs in each row, but as each row has a td with a colspan, you only see two in each row.
The width will depend on the contents by default, but you can use width settings for the tds of course. You can not use margins on tds.
table {
border-collapse: collapse;
width: 60%;
margin: 0 auto;
}
td {
border: 1px solid #ddd;
min-width: 35%;
}
td:nth-child(2) {
min-width: 50%;
}
<table>
<tr>
<td>AAA</td>
<td colspan="2" ;>BBB</td>
</tr>
<tr>
<td>AAA</td>
<td colspan="2" ;>BBB</td>
</tr>
<tr>
<td>AAA</td>
<td colspan="2" ;>BBB</td>
</tr>
<tr>
<td>AAA</td>
<td colspan="2" ;>BBB</td>
</tr>
<tr>
<td colspan="2" ;>CCC</td>
<td>DDD</td>
</tr>
<tr>
<td colspan="2" ;>CCC</td>
<td>DDD</td>
</tr>
<tr>
<td colspan="2" ;>CCC</td>
<td>DDD</td>
</tr>
</table>

Wrapping table data without table-layout?

I need to wrap text within a <td> element, but I can't use the css table-layout property as the output is to html e-mail body and Outlook doesn't support the table-layout property.
Is there some other way to wrap text within a <td> element, or do I need to do the line breaking and measuring manually in code?
Here is an example of what I am trying to acheive:
td {
border: solid black 1pt;
font-family: arial;
font-size: 10pt
}
thead td{
text-align:center;
font-weight:bold;
}
table {
border-collapse: collapse
}
<html>
<body>
<table style="width:35pt;height:24pt;table-layout:fixed">
<thead>
<tr>
<td style="width:35pt;height:12pt">Good</td>
</tr>
</thead>
<tbody>
<tr>
<td style="width:35pt;height:12pt;word-wrap:break-word">Costingly Cost Cost</td>
</tr>
</tbody>
</table>
<div style="height:50pt"></div>
<table style="width:35pt;height:24pt;">
<thead>
<tr>
<td style="width:35pt;height:12pt">Bad</td>
</tr>
</thead>
<tbody>
<tr>
<td style="width:35pt;height:12pt" nowrap>Costingly Cost Cost</td>
</tr>
</tbody>
</table>
</body>
</html>
Use the Microsoft proprietary word-break:break-all;
<td style="word-break:break-all;">
That should fix things.
You can try this:
<tr>
<td nowrap>Never increase, beyond what is necessary, the number of entities required to explain anything</td>
<td>Never increase, beyond what is necessary, the number of entities required to explain anything</td>

Designing complex table layout using html and css

I am trying to design the following table using html and css how do I proceed with it. Thank you in advance.
This solution will save you from having to use nested tables. The trick is that you really have four rows, not three, and make use of colspan and rowspan.
Note that you need to set a height for the td in order to ensure they are even.
table {
width: 100%;
border-collapse: collapse;
}
td {
border: 1px solid black;
height: 30px;
}
<table>
<tr>
<td colspan="2"></td>
<td rowspan="2"></td>
</tr>
<tr>
<td rowspan="2"></td>
<td rowspan="2"></td>
</tr>
<tr>
<td rowspan="2"></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
you can try this one:
HTML
<table>
<tr>
<td>
<table>
<tr>
<td colspan="2">Th</td>
</tr>
<tr>
<td>Th</td>
<td>Th</td>
</tr>
<tr>
<td>Th</td>
<td>Th</td>
</tr>
</table>
</td>
<td>Th</td>
</tr>
</table>
CSS
table
{
width:100%;
height:100px;
text-align:center;
border:2px solid gray;
border-collapse: collapse;
}
td
{
border:2px solid gray;
}
.container
{
width:100%;
}
.container .header
{
width:100%;
height:200px;
background:#5076BB;
}
.container .slider
{
width:100%;
height:500px;
background:#5076BB;
}
DEMO HERE
UPDATED DEMO HERE
Read the tutorial: http://www.w3schools.com/html/html_tables.asp
In particular read the part about the attributes rowspan="" and colspan=""
Example:
<td colspan="2">This table data will span two columns</td>
<td rowspan="2">This table data will span two rows</td>

How can i can keep my divs from resizing when i add/remove text?

I have a table with a lot of records and a search box
<table>
<tr>
<td class="col-3">name1</td>
<td class="col-3">sName1</td>
<td class="col-3">age1</td>
</tr>
<tr>
<td class="col-3">name..</td>
<td class="col-3">sName..</td>
<td class="col-3">age..</td>
</tr>
<tr>
<td class="col-3">nameN</td>
<td class="col-3">sNameN</td>
<td class="col-3">ageN</td>
</tr>
</table>
.col-3{
width:33,33%
}
Some of the records may be displayed as none, so if the search box returns a full "none" column it gets wraped to none as if it didnt have the "witdh:33,33%" atribute.
Anyone knows a way to keep the elements in his original size even if they dont have content?
Thanks in advance.
Try table-layout: fixed along with a fixed width for the table
table {
width: 100%;
table-layout: fixed;
}
.col-3 {
width: 33, 33%;
border: 1px solid grey;
}
<table>
<tr>
<td class="col-3">name1 askdlhaslkdhsaldsad asjd;kadaj as;dj;asjd as ;asjsd;asjd;ja</td>
<td class="col-3">sName1</td>
<td class="col-3">age1</td>
</tr>
<tr>
<td class="col-3">name..</td>
<td class="col-3">sName..</td>
<td class="col-3">age..</td>
</tr>
<tr>
<td class="col-3">nameN</td>
<td class="col-3">sNameN</td>
<td class="col-3">ageN</td>
</tr>
</table>
try this one:
<table style="border:1px solid">
<tr>
<td style="min-width:50px">one</td>
<td style="min-width:100px">two</td>
</tr>
</table>

Aligning both rows and columns in a nested table

I am trying to process an XML file via XSLT to produce an HTML report. The report contains a table with 3 columns: Description, Date and Note. One row of this table may contain one description, but multiple dates and notes. The dates and notes for a single description form pairs, but sometimes either a date or a note is missing. I could have the following problems:
A note does not have a corresponding date, so the date-note pair representation is skewed (See Date 1.2 and Note 1.2 in the example). A desired representation would have an empty line followed by Date 1.2 on a different line.
A note spans multiple lines, but its corresponding date does not, so the following notes are not aligned with their dates.
Here is an example. I used <br/> elements for demonstration purposes.
<table>
<tr>
<td class="firstcolumn">Description</td>
<td>Date</td>
<td class="lastcolumn">Note</td>
</tr>
<tr>
<td class="firstcolumn">Description 1. Could span multiple lines.</td>
<td valign="top" align="right">Date 1.2</td>
<td valign="top" align="right" class="lastcolumn">Note without date 1.1<br/> Note 1.2</td>
</tr>
<tr>
<td class="firstcolumn">Description 2.</td>
<td valign="top" align="right">Date 2.1<br/> Date 2.2</td>
<td valign="top" align="right" class="lastcolumn">Some really long note<br/>spanning multiple lines 2.1<br/> Note 2.2</td>
</tr>
</table>
Here is a link to a fiddle: JSFiddle.
I tried to solve this with nested tables: for each row of the outer table, I created a nested table with 2 columns, each row of which contained a date-note pair (or an empty cell and a note). The dates and notes now align. However, the nested table column widths do not align across the rows of the outer table. I tried to solve this by setting all nested table widths using the styles
table { width:100%;table-layout:fixed;} /* for each nested table */
td {width:90px;} /* for the columns of nested tables */
The columns now align. However, sometimes the notes are cut off and not visible (using the overflow style I can make them appear outside the table border, but it looks ugly). Ideally, I would want to have column widths that adjust to the notes' lengths.
I think I made it!
What I did is:
I broke the 2nd and 3rd rows in two
to the first one I gave rowspan="2"
I created two classes: bt and bb
table {
border-collapse: collapse;
width: 75%;
}
td {
border-collapse: collapse;
border: 1px solid black;
padding: 0px;
margin: 0px;
font-family: Calibri;
}
td.firstcolumn {
width: 60%;
}
td.lastcolumn {
width: 25%;
}
.bt {
border-top: 0;
}
.bb {
border-bottom: 0;
}
<table>
<tr>
<td class="firstcolumn">Description</td>
<td>Date</td>
<td class="lastcolumn">Note</td>
</tr>
<tr>
<td rowspan="2" class="firstcolumn">Description 1. Could span multiple lines.</td>
<td class="bb" valign="top" align="right"></td>
<td class="bb" valign="top" align="right">Note without date 1.1</td>
</tr>
<tr>
<td class="bt" valign="top" align="right">Date 1.2</td>
<td class="bt" valign="top" align="right" class="lastcolumn"> Note 1.2</td>
</tr>
<tr>
<td rowspan="2" class="firstcolumn">Description 2.</td>
<td class="bb" valign="top" align="right">Date 2.1</td>
<td class="bb" valign="top" align="right" class="lastcolumn">Some really long note<br/>spanning multiple lines 2.1</td>
</tr>
<tr>
<td class="bt" valign="top" align="right"> Date 2.2</td>
<td class="bt" valign="top" align="right" class="lastcolumn">Note 2.2</td>
</tr>
</table>
Here's a fiddle
Assuming that you need to adjust width automatically based on contents try changing in example provided here
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_td_width
<!DOCTYPE html>
<html>
<head>
<style>
table,th,td
{
border:1px solid black;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td width="70%">January</td>
<td width="30%">$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
<p>The width attribute is not supported in HTML5. Use CSS instead.</p>
</body>
</html>