{"id":40,"date":"2007-08-15T12:02:13","date_gmt":"2007-08-15T19:02:13","guid":{"rendered":"http:\/\/css-tricks.com\/using-divs-inside-tables\/"},"modified":"2015-07-02T04:55:37","modified_gmt":"2015-07-02T11:55:37","slug":"using-divs-inside-tables","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/using-divs-inside-tables\/","title":{"rendered":"Using Divs Inside Tables"},"content":{"rendered":"
Despite their status as mortal enemies, divs and tables can work together if you need them to. Because of the ability to use vertical centering in tables, it is a somewhat popular technique to use a single-celled table with both horizontal and vertical centering to absolutely center your page content in a browser window (mostly only useful for fixed-size content).<\/p>\n
One important thing to remember when putting a div inside of a table is that the div needs to live inside of a particular table cell,<\/strong> meaning inside<\/strong> of a td or th element which is inside<\/strong> of a tr element.<\/p>\n To see what I mean, take a look at a table like this:<\/p>\n <\/p>\n Now you want to add another row to this table and put a div inside it, you might do this:<\/p>\n But that breaks the layout! Strange but true. See below:<\/p>\n <\/p>\n What you need to do is make sure the div is inside an actual table cell, a td or th element, so do that:<\/p>\n And you should get what you are expecting, a fully style-able div<\/strong> inside a table:<\/p>\n <\/p>\n<table border=1 width=200 cellpadding=10>\r\n <tr>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <\/tr>\r\n <tr>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <\/tr>\r\n<\/table><\/code><\/pre>\n
<table border=1 width=200 cellpadding=10>\r\n <tr>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <\/tr>\r\n <tr>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <td>\r\n <p>I'm text in a cell.<\/p>\r\n <\/td>\r\n <\/tr>\r\n <tr>\r\n <div id=\"my_box\">\r\n <p>I'm text in a div.<\/p>\r\n <\/div>\r\n <\/tr>\r\n<\/table><\/code><\/pre>\n
<tr>\r\n <td colspan=3>\r\n <div id=\"my_box\">\r\n <p>I'm text in a div.<\/p>\r\n <\/div>\r\n <\/td>\r\n<\/tr><\/code><\/pre>\n
element in HTML is used for displaying tabular data. You can think of it as a way to describe and display data that would make sense in spreadsheet software. Essentially: columns and rows. In this article, we\u2019re going to look at how to use them, when to use\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2013\/09\/table-guide.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":181375,"url":"https:\/\/css-tricks.com\/centering-css-complete-guide\/","url_meta":{"origin":40,"position":2},"title":"Centering in CSS: A Complete Guide","date":"September 2, 2014","format":false,"excerpt":"Centering things in CSS is the poster child of CSS complaining. Why does it have to be so hard? They jeer. I think the issue isn't that it's difficult to do, but in that there so many different ways of doing it, depending on the situation, it's hard to know\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2014\/09\/guide-centering.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2767,"url":"https:\/\/css-tricks.com\/vertically-center-multi-lined-text\/","url_meta":{"origin":40,"position":3},"title":"Vertically Center Multi-Lined Text","date":"May 13, 2009","format":false,"excerpt":"If you only have a single word or a single line of text, there is a clever way to vertically center it in a block with CSS. You set the line-height of that text to be equal to the height of the box. Works great, but is a major fail\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/csstricks-uploads\/abehappy.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":8679,"url":"https:\/\/css-tricks.com\/wolf-in-sheeps-clothing\/","url_meta":{"origin":40,"position":4},"title":"Wolf in Sheep’s Clothing","date":"March 10, 2011","format":false,"excerpt":"Roger Johansson has a neat quick-tip style article on centering page content both vertically and horizontally by making the html element display: table; and the body element display: table-cell; with vertical-align: middle;. See the full code to make it happen in the article. I'm down with that. Works in all\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2597,"url":"https:\/\/css-tricks.com\/what-is-vertical-align\/","url_meta":{"origin":40,"position":5},"title":"What is Vertical Align?","date":"April 20, 2009","format":false,"excerpt":"CSS has a property called vertical align. It can be a bit confusing when you first learn about it, so I thought we could help explain what it is for and some use cases. The basic usage is like this: img { vertical-align: middle; } Notice in this usage case,\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/csstricks-uploads\/vert-align-cross-browser.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"featured_media_src_url":null,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":5,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":204489,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/40\/revisions\/204489"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}