Treehouse

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!

ydkjs-5.png

Recent Features

  • 9 More Mind-Blowing WebGL&nbsp;Demos

    With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities.  I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...

  • How I Stopped WordPress Comment&nbsp;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

  • Introducing MooTools&nbsp;ScrollSidebar

    How many times are you putting together a HTML navigation block or utility block of elements that you wish could be seen everywhere on a page? I've created a solution that will seamlessly allow you to do so: ScrollSidebar. ScrollSidebar allows you...

  • Retrieve Your Gmail Emails Using PHP and&nbsp;IMAP

    Grabbing emails from your Gmail account using PHP is probably easier than you think. Armed with PHP and its IMAP extension, you can retrieve emails from your Gmail account in no time! Just for fun, I'll be using the MooTools Fx.Accordion plugin...

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

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