Html - css // Table (<td><tr>) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Html - css // Table (<td><tr>)

if I've : <table> <td> <tr>Some text</tr> </td> </table> with in css : table, td, tr { width: 100%; } tr { height: 100px; } why table and td take 100% of the page and tr not ? it's like this : [[[ ] ]]

4th Sep 2017, 6:38 PM
NoxFly
NoxFly - avatar
10 Answers
+ 7
It should've been: <table> <tr><td>Row 1 Col 1</td> <td>Row 1 Col 2</td> </tr> <tr><td>Row 2 Col 1</td> <td>Row 2 Col 2</td></tr> </table>
4th Sep 2017, 6:59 PM
Ipang
+ 6
@Ipang, not align horizontally, but vertically
5th Sep 2017, 4:44 AM
NoxFly
NoxFly - avatar
+ 5
oh ok thanks :¥
4th Sep 2017, 7:10 PM
NoxFly
NoxFly - avatar
+ 5
You're welcome, glad to help.
4th Sep 2017, 7:14 PM
Ipang
+ 4
And how can I align text in middle of the td ? when I use 'vertical-align: middle;' it does nothing... https://code.sololearn.com/WepwVm1hLjdO/?ref=app
4th Sep 2017, 7:20 PM
NoxFly
NoxFly - avatar
+ 4
@Shinbi, add this to CSS for td to center align text. text-align:center; (Edit) @visph suggestion correct, comment on display:block; sorry, bit sleepy..
4th Sep 2017, 8:21 PM
Ipang
+ 4
thanks ;)
5th Sep 2017, 5:21 AM
NoxFly
NoxFly - avatar
+ 3
It could be render differently according to browsers rules to correct your code, because you make mistake: <td> (table data == cell) element need to be child of <tr> (table row), and <tr> need to be child of <table> ^^
4th Sep 2017, 6:50 PM
visph
visph - avatar
+ 3
@Shinbi, oh, sorry, I was half asleep typing :D. Then wrap the text in span like: <td><span>Text content</span></td> And add to CSS like: td span { position:relative; top:45%; } And in CSS for the td, remove display:block; Add vertical-align:middle; Please be advised that this will center vertically a single line of text, if text content is longer it will expand downwards. Meaning if the text is long you'll need to recalculate the top. There's also <td valign="middle"></td>, it's a lot easier to use, but I guess it's deprecated by HTML 5. Good luck mate..
5th Sep 2017, 5:17 AM
Ipang
+ 2
Don't set: display: block; ... to your <td> It will broke the table behaviour ^^ And add 'vertical-align:middle;' instead...
4th Sep 2017, 7:29 PM
visph
visph - avatar