Set td width in table with table-layout: fixed - html

As far I know the column widths are determined by the first row of the table. My problem is the first td in table has colspan set to '2'. Here is how my table looks like. Important thing is I can not change the HTML structure of table.
<table>
<tbody>
<tr>
<td colspan="2"><strong>Personal Details</strong></td>
</tr>
<tr>
<td>
<ul>
<li>Title: </li>
</ul>
</td>
<td>
test
</td>
</tr>
<tr>
<td colspan="2"><strong>Personal Details</strong></td>
</tr>
<tr>
<td>
<ul>
<li>Title: </li>
</ul>
</td>
<td>
test
</td>
</tr>
<tr>
<td colspan="2"><strong>Personal Details</strong></td>
</tr>
<tr>
<td>
<ul>
<li>Title: </li>
</ul>
</td>
<td>
test
</td>
</tr>
<tr>
<td>
<ul>
<li>Title: </li>
</ul>
</td>
<td>
test
</td>
</tr>
</tbody>
</table>
CSS:
table {
table-layout: fixed;
width: 100%;
word-wrap: break-word;
}
What I need is to set the width of td which contain <ul> element to 30%. So td colspan = '2' becomes some kind of header and the td elements below are split 30/70.

$('strong').parents('tr').addClass('header_td'); // jquery line code...
table {
width: 100%;
word-wrap: break-word;
}
table {
width: 100%;
word-wrap: break-word;
border: 2px solid black;
}
table tr:not(.header_td){
background : red;
color:white;
}
table tr:not(.header_td) td:first-child{
max-width:30%;
width:30%;
background:green;
}
table tr:not(.header_td) td{
max-width:69%; // 69% just only beacause some extra padding by element it self doesn't make chage t your structure.
width:69%;
background:black;
}
.header_td{
background : grey;
}
<table>
<tbody>
<tr>
<td colspan="2"><strong>Personal Details</strong></td>
</tr>
<tr>
<td>
<ul>
<li>Title: </li>
</ul>
</td>
<td>
test
</td>
</tr>
<tr>
<td colspan="2"><strong>Personal Details</strong></td>
</tr>
<tr>
<td>
<ul>
<li>Title: </li>
</ul>
</td>
<td>
test
</td>
</tr>
<tr>
<td colspan="2"><strong>Personal Details</strong></td>
</tr>
<tr>
<td>
<ul>
<li>Title: </li>
</ul>
</td>
<td>
test
</td>
</tr>
<tr>
<td>
<ul>
<li>Title: </li>
</ul>
</td>
<td>
test
</td>
</tr>
</tbody>
</table>
here i updated whole my answer with your new requirement please take a look here on it

Take out table-layout: fixed; and add td:first-child { width: 30% } to your css.
http://jsfiddle.net/louis_feat/gcgk9y7e/

You can achieve this by using some pseudo selectors, it won't work in IE 6, 7 and 8 though:
table {
table-layout: initial;
width: 100%;
word-wrap: break-word;
}
tr:not(:first-child) td{
width: 70%;
}
tr:not(:first-child) td:first-child {
width: 30%;
}

Related

How do I style the first three items of a list

i have a table,like:
<html>
<head></head>
<body>
<table class=" top-10">
<thead>
<tr>
<th>index</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<td> 1 </td>
<td> john </td>
</tr>
<tr>
<td> 2 </td>
<td> mia </td>
</tr>
<tr>
<td> 3 </td>
<td> james </td>
</tr>
<tr>
<td> 4 </td>
<td> creed </td>
</tr>
<tr>
<td> 5 </td>
<td> perty </td>
</tr>
... and so on
</tbody>
</table>
</body>
</html>
This is a list
and I want the top three indexes to have different colors
It's kind of like a hot list
How do I write CSS3 styles to make this table look like this?
yes,The code for the first answer looks pretty neat, but there's one problem we seem to be missing.No matter how many pages there are, the first three have this effect.
How can index=1/2/3 have this effect?
You can use CSS nth-child:
https://css-tricks.com/useful-nth-child-recipies/
Example:
.top-10 tr:nth-child(1) td:first-child {
background-color: red;
}
.top-10 tr:nth-child(2) td:first-child {
background-color: orange;
}
.top-10 tr:nth-child(3) td:first-child {
background-color: yellow;
}

Align two tables withone one misplacing another

I've tried to look at a few web pages but none of them seem to be addressing the specific issue I'm having. I'm trying to make a web page and I'm trying to make a table aligned to the left side of the web page with while the rest in center aligned. However the items ligned in the center are pushed over to the right where the left aligned table is. How can I align something in the center without it being interefered with something aligned on the left side of the page?
Here is my code.
`<!doctype html>
<title> Webpage.</title>
<body>
<body background="Data/Back.jpg">
<table style="float: left" bgcolor="#9999FF" border="solid">
<tr>
<th><ul><h1>Directory.</h1></ul></th></tr>
<br>
<tr><td><li><a href="subpages/skills.html">Skills.</li></td></tr>
<tr><td><li><a href="subpages/experience.html">Experience.</li></td>
</tr>
<tr><td><li><a href="subpages/References">References.</li></td></tr>
<tr><td><li><a href="subpages/ContactInfo.html">Contact Info.</li>
</td></tr>
<tr><td><li><a href="subpages/Resume.html">Resume.</li></td></tr>
</table>
<center>
<table bgcolor="CCCCCC" border="3px solid black">
<tr>
<th><h1><b><u>Name/Header.</u></b></h1></th>
</tr>
</table>
<img src="Data/Me.jpg" width="25%">
<table bgcolor="#819FF7" width="50%" border="solid">
<tr>
<th>About me.</th>
</tr>
<tr>
<td>Subject
</td>
</tr>
</center>
<style type="text/css">
h1{
color: #0404B4
}
th{
color: #61380B
}
td{
color: #8A0829
}
</style>
</body>`
Simply remove float: left from the first table. Floating an element means that other elements will attempt to occupy any remaining width when that element is less than 100% of the width of the parent. Alternatively, clear the floats afterwards with clear: both.
Here's an example, cleanly formatted, and removing float: left.
h1 {
color: #0404B4;
}
th {
color: #61380B;
}
td {
color: #8A0829;
}
<body>
<table bgcolor="#9999FF" border="solid">
<tr>
<th>
<div style="margin-left: 2em">
<h1>Directory.</h1>
</div>
</th>
</tr>
<tr>
<td>
<ul>
<li>
Skills.
</li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>
Experience.
</li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>
References.
</li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>
Contact Info.
</li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>
Resume.
</li>
</ul>
</td>
</tr>
</table>
<center>
<table bgcolor="#CCCCCC" border="3px solid black">
<tr>
<th>
<h1><b><u>Name/Header.</u></b></h1>
</th>
</tr>
</table><img src="Data/Me.jpg" width="25%">
<table bgcolor="#819FF7" border="solid" width="50%">
<tr>
<th>About me.</th>
</tr>
<tr>
<td>Subject</td>
</tr>
</table>
</center>
</body>
Hope this helps! :)

How to make a tag with dynamic content full height inside of td tag

I have a table where I load dynamic content, and I want that the content inside the cells is the same height as the cells! Precisely I have "a" elements with some text, inside the td cells. I get a problem when the text in a cell is long and goes at newline, and like you can see in the image the other "a" elements in the table row don't fit the full height of the cell.
See the live Demo
Here is the code
HTML
<table class="table" cellspacing="0">
<thead>
<tr>
<th>Data</th>
<th>Titolo</th>
<th>Sede</th>
<th>Città</th>
</tr>
</thead>
<tbody>
<tr>
<td>12/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>asd
</td>
<td>ada
</td>
<td>asdas
</td>
</tr>
<tr>
<td>04/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>ada
</td>
<td>prova
</td>
<td>asda
</td>
</tr>
<tr>
<td>07/09/2017
</td>
<td><a href="eventi.php?id=12">Evento Lignano <br>
sabbiadoro</a>
</td>
<td>Palazzetto dello Sport
</td>
<td>Perugia
</td>
</tr>
<tr>
<td>09/09/2015
</td>
<td>aaa
</td>
<td>aaa
</td>
<td>aaa
</td>
</tr>
<tr>
<td>09/03/2015
</td>
<td>sfsd
</td>
<td>ada
</td>
<td>dadasd
</td>
</tr>
</tbody>
CSS
table{
width:400px;
}
td a{
background:#ff0;
display: block;
padding:20px;
box-sizing:border-box;
height: 100%;
width: 100%;
}
td{
height:100%;
position: relative;
}
table {
width: 400px;
}
td a {
background: #ff0;
display: block;
padding: 20px;
box-sizing: border-box;
height: 100%;
width: 100%;
}
td {
height: 100%;
position: relative;
}
<table class="table" cellspacing="0">
<thead>
<tr>
<th>Data</th>
<th>Titolo</th>
<th>Sede</th>
<th>Città</th>
</tr>
</thead>
<tbody>
<tr>
<td>12/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>asd
</td>
<td>ada
</td>
<td>asdas
</td>
</tr>
<tr>
<td>04/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>ada
</td>
<td>prova
</td>
<td>asda
</td>
</tr>
<tr>
<td>07/09/2017
</td>
<td><a href="eventi.php?id=12">Evento Lignano <br>
sabbiadoro</a>
</td>
<td>Palazzetto dello Sport
</td>
<td>Perugia
</td>
</tr>
<tr>
<td>09/09/2015
</td>
<td>aaa
</td>
<td>aaa
</td>
<td>aaa
</td>
</tr>
<tr>
<td>09/03/2015
</td>
<td>sfsd
</td>
<td>ada
</td>
<td>dadasd
</td>
</tr>
</tbody>
</table>
Here is a little hack I've used in the past.
You can essentially add a pseudo element to the a element and absolutely position it relative to the closest td element. Since the pseudo elements are essentially children elements of the a element, when you click the pseudo element, you are inadvertently clicking the a element.
Updated Example
td a:after, td a:before {
content: '';
position: absolute;
top: 0; right: 0;
bottom: 0; left: 0;
}
td a:after {
background: #ff0;
z-index: -1;
}
You must define an height of your TD, otherwise the browser doesn't know the 100% of what is to be considered:
td {
height:100px;
position: relative;
}
https://jsfiddle.net/wg6paumw/4/

Increasing last TR height till parent td height

I have a scenario where one table had some tr and other table have less tr's than first one table.Both tables are under td's. In this Scenerio how to increase only last tr height of html table in % mode not in px mode
HTML CODE:
<table border="1" style="width:100%">
<tr>
<td style="width:50%">
<table border="1" style="width:100%">
<tr>
<td>
A
</td>
</tr>
<tr>
<td>
B
</td>
</tr>
<tr>
<td>
C
</td>
</tr>
</table>
</td>
<td style="vertical-align:top">
<table border="1" style="width:100%;">
<tr>
<td>
1
</td>
</tr>
<tr>
<td>
2
</td>
</tr>
</table>
</td>
</tr>
jsfiddle Demo
Try like this: Demo This will expand only last row of right side content
CSS:
table {
height: 100%;
width: 100%;
}
td {
}
.table {
display: table;
height:100%;
}
.cell {
vertical-align: top;
display: table-cell;
height:100%;
}
.container {
height: 100%;
-moz-box-sizing: border-box;
}
HTML:
<table border="1" style="width:100%;" class="table">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td class="cell">
<div class="container">Increse this row height till parents td height</div>
</td>
</tr>
</table>
EDIT: If you want right column should be equally expanded, you can use like this: Demo
CSS:
table {
height: 100%;
width: 100%;
}
Hope this is what you want!!
use single table and rowspan
<table border="1" style="width:100%">
<tr><td>A</td><td>1</td></tr>
<tr><td>B</td><td>2</td></tr>
<tr><td>C</td><td rowspan="3">3</td></tr>
<tr><td>D</td></tr>
<tr><td>E</td></tr>
</table>

Center a Table inside a TD

I have a very simple problem: I need to center a table inside a TD element. If I were using HTML 4 I'd do it like this:
​<table style="border:solid;width: 100%">
<tr>
<td align="center">
<table style="border:solid; width:50%">
<tr>
<td >I must be in the center</td>
</tr>
</table>
</td>
</tr>
</table>​
But I'm trying not to use deprecated attributes and do it the CSS way. I already tried this:
<td style="text-align:center">
And this:
<td style="margin: 0 auto">
And the tables keeps in the left-side of the cell. Any suggestions?
You had the right idea with margin:auto 0; just take off the 0.
Working example: http://jsfiddle.net/cxnR8/
<table style="border:solid;width: 100%">
<tr>
<td>
<table style="margin:auto;border:solid; width:50%">
<tr>
<td >I must be in the center</td>
</tr>
</table>
</td>
</tr>
</table>​
But, more importantly, do you really need to use tables and in-line styling?
Center the table using the deprecated align="" attribute.
<table>
<tr>
<td>
<table align="center">
<tr>
<td>in the middle</td>
</tr>
</table>
</td>
</tr>
</table>​
Your seccond suggestion is correct. See this working example.
HTML:
<table class="outer">
<tr>
<td>
<table class="inner">
<tr>
<td>in the middle</td>
</tr>
</table>
</td>
</tr>
</table>​
CSS:
.outer
{
width: 100%;
border: solid 1px red;
}
.inner
{
width: 25%;
margin: auto;
border: solid 1px blue;
}
​