O'Reilly

Table Cell and Position Absolute

By on  

If you follow me on Twitter, you saw me rage about trying to make position: absolute work within a TD element or display: table-cell element.  Chrome?  Check.  Internet Explorer?  Check.  Firefox?  Ugh, FML.  I tinkered in the console...and cussed.  I did some researched...and I cussed more.  I found the 13 YEAR OLD ticket in Bugzilla...and threw Molotov cocktails into the street.  Luckily my whining on Twitter solicited an excellent solution from Andrew McGivery (@andrewmcgivery).  Check out the wonky solution that make this work!

The HTML

Here's the sample HTML we'll be using for this exercise:

<table >
	<tr>
		<td>
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...</p>
		</td>
		<td>
			<div style="position:relative; overflow: auto; height: 100%;">
				<p>Lorem ipsum dolor sit amet.</p>
				<div style="position:absolute; bottom:0; right:0; background: yellow;">
					This will be positioned at 5,5 relative to the cell
				</div>
			</div>
		</td>
	</tr>
</table>

Alternatively you could use DIV elements with display: table-cell:

<div class="table">
	<div class="cell">
		<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
	</div>
	<div class="cell">
		<div style="position:relative; overflow: auto; height: 100%;">
				<p>Lorem ipsum dolor sit amet.</p>
				<div style="position:absolute; bottom:0; right:0; background: yellow;">
					This will be positioned at 5,5 relative to the cell
				</div>
			</div>
	</div>
</div>

Either of these examples will work.

The CSS

It turns out that pinning height: 100% on the way up the stack cures our absolute positioning blues:

table, tr { height: 100%; }

Here's the craziness with a DIV structure:

.table { display: table; }
.cell { display: table-cell; }
.table, .cell { height: 100%; }

Incredibly odd adding the 100% height to the container, yes?  Well, unfortunately that's what we'll need to do until the bug in Firefox is fixed.

As much as this type of bug annoys the hell out of us, these type of bugs separate the experts and novices.  Well done to Andrew for finding the solution to this heinous problem.  Keep this post handy as you may need it if that Firefox bug lasts another 13 years!

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

Discussion

  1. A handy tip, but I’m curious as to why you would need to absolutely position within a table cell?

  2. MaxArt

    I’ve known that bug for *ages*… technologically speaking. It’s a shame for the Mozilla guys, if you ask me.
    The workaround is just a trick and doesn’t work in all the cases.
    I resolved to not use tables, or not use absolutely positioned elements inside it.

  3. SelenIT

    The Bugzilla ticket mentioned in the article is about relative positioning the table cell itself. Having this not fixed for 13 years could be excused because CSS2.1 spec says that ‘The effect of ‘position:relative’ on … table-cell, and table-caption elements is undefined.’ The problem here is that table cells with `position: relative` are not containing blocks for their absolutely positioned contents. It has its own Bugzilla ticket (https://bugzilla.mozilla.org/show_bug.cgi?id=63895), which also is 13 years old, but has no excuse in the spec. Please help make its priority higher with your votes!

  4. Hey, David have you checked it in ie is not working am using windows 8

  5. Nandan Kr Sahoo

    Hi,
    Thanks for created of Table Cell and Position Absolute.
    but it is not working IE Browser. can you please give me solution for IE browser.

    Thanks
    Nandan

  6. Blake

    I was just dealing with this issue last week :) There are 3 boxes laid out side by side that need to be the same height and have a button inside each box that is positioned absolutely to the bottom left.
    Here’s an example of just 1 box: http://codepen.io/rblakejohnson/pen/IfDyb

    And I’ve commented out the height:100% CSS and it seems to be working in Firefox!? But I remember messing with different ways of doing this last week and it not working in Firefox.
    Anyway, I wondered if you could shed some light on this and the extra div you have in your demo that has inline styles. It doesn’t seem to match up with the code examples you have in this article that only make it seem like your don’t need an extra div. Is the extra div necessary?

  7. Hello David,

    I’ve been ranting on this bug for quite some time as well :). I’ve tried to think inside the (flex)box for this one and created a flextable (for the lack of a better name :p). I now use flexbox, with css tables as fallback, only when the browser doesn’t support the new syntax (2013 = “display: flex”).

    This doesn’t require any additional html at all, which was one of my requirements as well.

    My code sample: http://codepen.io/kevinsmets/pen/AmDzh

    More info (forum post on css-tricks) here: http://css-tricks.com/forums/topic/flextable-flexbox-with-css-tables-fallback/

  8. mem

    I believe it doesn’t work when using images that need to shrink or grow, using max-width:

    cf. http://jsfiddle.net/u7knh/25/

  9. Carlos

    Great been looking for a solution for a while, this is the only one that worked for me. Thanks for sharing

  10. mast

    This solution doesn’t work and there’s actually a much simpler way:

    http://stackoverflow.com/questions/25223757/vertically-align-div-with-absolute-position/25228012#25228012

  11. Man, this was a great help. I found I had to add height="100%" to the table tag as well but this helped me solve a really complex layout. Thanks!

  12. Bill

    Hi David,

    Don’t cry, but your demo doesn’t work at all in Internet Explorer 11 (or any other version). Moo-hoo-hahaha!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Prevent Chrome from Translating a Page

    A while back I shared my favorite Google Chrome extension:  Google Art Project.  I've enjoyed seeing beautiful art when I open a new tab -- it's brought genuine happiness to my day, however small that happiness may be.  About a week ago, however, the art presented had...

  • Create Any Type Of Website With These Multi-Purpose Themes

    We have selected what we believe are the very best multipurpose WordPress themes on the market today. Our list contains a number of best sellers, several newcomers that are proving to be highly popular, and a few themes that are ideal for creating the types of...

  • An Introduction to Static Site Generators

    Static site generators seem to have been becoming more and more popular recently, but they’re not one of those ephemeral novelty things that grow in popularity as quickly as they fall into oblivion shortly after. For over a decade, many different projects — 394 of...

  • Automated Tests for Visual Responsive Layouts

    Today it's all about testing. In 2015, many developers knows about TDD and I personally think that testing is one of the key for quality products. But what about testing in a Front-end environment? How do you guys write your tests for a responsive page or...

  • Getting Dicey With Flexbox

    What if you could build complex CSS layouts in minutes? Flexbox is a new CSS layout spec that makes it easy to construct dynamic layouts. With flexbox, vertical centering, same-height columns, reordering, and direction agnosticism are a piece of cake. There's a popular myth floating around that...