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!

Recent Features

  • By
    5 More HTML5 APIs You Didn&#8217;t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

Incredible Demos

  • By
    Web Audio API

    The Web Audio API allows developers to load and decode audio on demand using JavaScript.  The more I evaluate awesome games for Firefox OS TVs, the more I get to learn about these APIs that I normally wouldn't touch.  The following is a very basic introduction to the WebAudio API...

  • By
    Degradable SELECT onChange

    Whenever I go to Google Analytics I notice a slight flicker in the dropdown list area. I see a button appear for the shortest amount of time and the poof! Gone. What that tells me is that Google is making their site function...

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!

  13. alexander

    :( not working in IE 11 !

  14. Have done a ton of research but still can’t solve the issue with my stationery. I have two side by side columns, one for text and one displaying a number of corporate logos, which I want to stay at the top of the cell, but if I enter a lot of text the logos gradually start moving down. I’ve tried everything but nothing works. Appreciate any help here. I’ll include the code to anyone willing to help.

    Thanks!

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