In the CSS, I have
table {
max-width:100%;
background-color:transparent;
border-collapse:collapse;
border-spacing:0
}
In HTML, I have
<table class="table">
<tr>
<td>
<img width="90px" src="logo.png">
</td>
<td>
<div class="page-header">Header
<td>
</tr>
</table>
How do I change the background color of the second <td> without changing the other table's <td> in CSS file?
Mind you, I'm using Bootstrap as my base CSS. And it's kinda complicated when it comes to modifying the code.
Using CSS you can something like
table tr:first-child td:nth-child(2){
background:red;
}
Add a class to your desired <td> element.
In that class, specify for desired background
See here for a working example
Add a class to the td you want to change (for example, use 'headerTd'):
<table class="table">
<tr>
<td><img width="90px" src="logo.png"></td>
<td class="headerTd"><div class="page-header">Header<td>
</tr>
</table>
Then add the following rule to your css:
.headerTd{
background-color:red;
}
You can go for styling only the 2nd simply give the style attribute.
style="color:red"
.secondTD {
background:red;
}
<table class="table">
<tr>
<td><img width="90px" src="logo.png"></td>
<td class="secondTD"><div class="page-header">Header<td>
</tr>
</table>
But if you want without classes, then
table:tr:td {
background:red;
}
<table class="table">
<tr>
<td><img width="90px" src="logo.png"></td>
<td><div class="page-header">Header<td>
</tr>
</table>
in case when every row with first td:
table>tr:td {
background:red;
}
Related
If I have the HTML below I want to find a single CSS class definition that will effect the outermost table but not the sub tables. Without changing the HTML can I get .myClass to do this:
I was playing around with the not selector but couldn't get it to work.
.myClass tr td div :not(table) {
background-color: red;
}
<body>
<div class="myClass">
<table>
<tr>
<td>
<div>My</div>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>Hello</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>World</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</body>
No you cannot do this with pure css without changing your html.
You can do it with jQuery or by simply putting the 'My' inside of a span, because text is not accesible by a selector.
But in all fairness your question was partly answered by #Pangloss. To access the outer table, just use >. Your problem lies in the fact that on the 2nd and 3rd rows, you still have the outer table present. Your question should actually be something like "What's the selector for an element that does not have a specific child type", and then you would find that you cannot target a parent of an element on css yet.
The first cell is the first div inside the first tr, we can target this using the following:
tr:first-child div:first-child{
background:red;
}
Full snippet:
tr:first-child div:first-child {
background: red;
}
<body>
<div class="myClass">
<table>
<tr>
<td>
<div>My</div>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>Hello</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>World</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</body>
My code is this :
<div style="width: 300px;">
<table width="100%" border="2px solid blue">
<tr>
<td style="width:30%">Player</td>
<td style="width:30%">Club</td>
<td style="width:30%">Country</td>
</tr>
<tr>
<td style="width:30%">HazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazard</td>
<td style="width:30%">Chelsea</td>
<td style="width:30%">Belgium</td>
</tr>
<tr>
<td>Ronaldo</td>
<td>Real Madrid</td>
<td>Portugal</td>
</tr>
<tr>
<td>Messi</td>
<td>Barcelona</td>
<td>Argentina</td>
</tr>
</table>
</div>
The result is this :
I tried to add width=30% in the column player, but it's still not working.
it's beacause your text is too large
use this may help you
<style>
table tr td
{
word-break: break-all;
}
</style>
Moob was before me, use word-wrap:break-word; but also use table-layout:fixed
css code
table{
table-layout: fixed;
}
td {
word-wrap:break-word;
}
That's because "HazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazardHazard" is stretching it.
Table cells will stretch to fit their content, and if they can't break, they will keep stretching.
If it was "Hazard hazard..." etc with spaces, it would break as expected. The same would happen if you put a large image in the table cell.
I was wondering if it is possible to have the table embedded inside the td to have the same height as the td.
<table>
<tr>
<td style="background:red">
<table style="background:blue">
<tr>
<td>Hello</td>
</tr>
</table>
</td>
</tr>
</table>
Here is the jsfiddle... https://jsfiddle.net/ekh6z8w0/
As you can see through the fiddle the embedded table is off by a pixel or two. Is it possible to have the blue inside overlap the slight red border?
Remove the padding from td, should work.
td{
padding:0;
}
You can try like this: Demo
CSS:
table>td, table>td>table {
padding:0;
margin:0;
}
table table {
padding:0;
margin:0;
height:100%;
border:1px solid red;
background-color:blue;
color:#fff;
}
HTML:
<table>
<tr>
<td>
<table>
<tr>
<td>Hello</td>
</tr>
</table>
</td>
</tr>
</table>
Hope this is what you want.
I cannot figure out the positioning problem using table in firefox and IE.
The fiddle will show the complete problem properly http://jsfiddle.net/qfDB9/ Even after providing proper height and width the problem persists. The actual reason I created the table is perfectly shown in Chrome whereas IE and Firefox has positioning problem.
CSS:
table tr #number_table {
width:50px;
height:50px;
text-align:center;
font-size:50px;
}
table tr #photo_table {
width:150px;
height:100%;
text-align:center;
}
table tr #description_table {
width:400px;
padding-bottom:2em;
font-size:20px;
}
table tr #band_name {
text-align:center;
height:25px;
}
HTML :
<table border="1" cellpadding="0" cellspacing="5" width="600px" style="color:#000;">
<tr>
<td id="number_table">1</td>
<td rowspan="2" id="photo_table"><img src="http://upload.wikimedia.org/wikipedia/commons/d/df/The_Fabs.JPG" width="140px" height="140px"/></td>
<td rowspan="4" id="description_table">something something <br><br><br><br>something something <br><br><br><br>something something <br><br><br><br>something something <br><br><br><br></td>
</tr>
<tr>
<td rowspan="3"></td>
</tr>
<tr>
<td id="band_name">Something</td>
</tr>
</table>
Is there a way to resolve this.
You need to vertically align the content in the <tr> tags
table tr { vertical-align:top; }
See DEMO
I might also add that you should consider using div's instead of tables
[SOLVED] Check This out. Just Add valign="top" to the <tr> or <td>
DEMO HERE: http://jsfiddle.net/qfDB9/9/
Suggestion, Always try too use the semantic code. like using thead, tbody, as it helps in implementing the CSS too. Also help in streamlining the CSS throughout the site.
Solution You can add this CSS "table tbody td{vertical-align:top;}" so all the content in all <td>s should be align to top. So if you have to give some table heading which are usually align bottom then you can handle by <thead> <th> "vertical-align:bottom.".
Please refer the code here:
HTML
<table border="1" cellpadding="0" cellspacing="5" width="600px" style="color:#000;">
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
</tr>
</thead>
<tbody>
<tr>
<td id="number_table">1</td>
<td rowspan="2" id="photo_table"><img src="http://upload.wikimedia.org/wikipedia/commons/d/df/The_Fabs.JPG" width="140px" height="140px"/></td>
<td rowspan="4" id="description_table">something something <br>
<br>
<br>
<br>
something something <br>
<br>
<br>
<br>
something something <br>
<br>
<br>
<br>
something something <br>
<br>
<br>
<br></td>
</tr>
<tr>
<td rowspan="3"></td>
</tr>
<tr>
<td id="band_name">Something</td>
</tr>
</tbody>
</table>
CSS
table thead th{vertical-align:bottom;}
table tbody td{vertical-align:top;}
table tr #number_table {
width:50px;
height:50px;
text-align:center;
font-size:50px;
}
table tr #photo_table {
width:150px;
height:100%;
text-align:center;
}
table tr #description_table {
width:400px;
padding-bottom:2em;
font-size:20px;
}
table tr #band_name {
text-align:center;
height:25px;
}
You can refer to the fiddle too: http://jsfiddle.net/aasthatuteja/sU3SS/
Let me know if the helps!
Enjoy!!
I got a table with a couple <td>:
<table>
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px;">
<div>
Third
</div>
</td>
</tr>
</table>
What I want to do is to place the "Third" <td> (with the div) to the right side of the table and the "First" and "Second" <td> should stay left.
Style with float:right; didn't work for me...
You need to make your table's width 100%, then control the widths of your first 2 columns, and finally right-align your third column.
http://jsfiddle.net/25Mqa/1/
<table>
<tr>
<td class="first">First</td>
<td class="second">Second</td>
<td class="third">Third</td>
</tr>
</table>
CSS:
table { width:100%; }
.first, .second { width:50px; }
.third { text-align:right; }
The problem is that the width of a <table> is determined by its content, by default. If you want the <table> to span 100% width (of its containing block) like block-level elements do, you can either add table-layout: fixed; and then specify your width - or just give it a width, depending on what you're after, e.g.
table {
width: 100%;
}
http://jsfiddle.net/QEaAd/2/
try add style="text-align:right;"
<table>
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px; text-align:right;">
<div>
Third
</div>
</td>
</tr>
</table>
Only if you have 2 divs one near other:
<div id="fr">div1</div>
<div id="fr">div2</div>
you can float them right:
<style>
#fr{float:right;}
</style>
<table style="width: 100%;">
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px; display: block; float: right;">
<div>
Third
</div>
</td>
</tr>
</table>
http://jsfiddle.net/pbesD/
I believe this does what you want, however from what I understand, floating table elements will cause problems in versions of Internet Explorer <8
I dont know what you are trying to do with tables and divs? But I normally use this for emailers.
I use the align attribute for td's. This helps a lot in making sure your layout looks the way you want. And it works in all browsers :)
FIDDLE
HTML:
<table width="100%">
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px;" align="right">
<div>
Third
</div>
</td>
</tr>
</table>
In Bootstrap 5.2 you can add .text-start and .text-end to your text class to align elements inside a table.