JustWatchTheMarginTop

While being jestfully critical of the use of a table on the Justwatchthesky.com placeholder page I proposed a simple CSS alternative and stumbled upon a curious behavior of the margin-top attribute. Assigning a percentage value does not set the margin-top to a percentage of the element’s height as one might expect. Instead it appears to set the margin-top to a percentage of the elements width! The same behavior occurs when padding-top is used.

Check out this CSS alternative and increase the height of the window—the placeholder does not change position. Now increase the width of the window—the placeholder stays centered (thanks to text-align: center for IE PC and an auto left and right margin for everyone else) but also moves down the page.

Quite an slick effect, especially given my assertion that beauty lies in relative proportions. Surprisingly, it works consistently in Safari, IE Mac, IE PC 5+, and Mozilla-based browsers so I would assume this is the intended and appropriate behavior.

Disclaimer

Perceived snooty standards nose-thumbing aside, I can’t wait to see what Ryan is cooking up and really don’t think anyone should fault him for using a gasp</sarcasm> table in this temporary situation. What was I doing in his source in the first place? Hoping to find comments in his code clueing us in on when to expect the new design. Don’t you do that?

Previous
Skinnier J
Next
Budget Design
Author
Shaun Inman
Posted
May 26th, 2004 at 1:27 pm
Categories
CSS
Web
Comments
021 (Now closed)

021 Comments

001

Quite slick indeed Shaun! And yeah, I was trying to find out when JWTS was going to go live too. I’ve been hounding Ryan on the IM and still no answer….but it’ll be awesome to see what happens on the launch.

Lates!

Author
Justin Goodlett
Posted
May 26th, 2004 9:45 am
002

I frequently use tables in the same application as Ryan is doing here — in fact, I use tables for most of my layout design right now. Someday I’ll grow up and learn CSS layout design.

Your CSS alternative isn’t acheiving the same effect Ryan wanted. That is, to place the image in the exact center of the browser window, no matter the resolution of the user.

Author
Sean
Posted
May 26th, 2004 9:47 am
003

Look again Sean, his source doesn’t place it exactly in the center of the window either. It appears to be placing it much higher than that—which is completely besides the point of this post anyway.

Author
Shaun Inman
Posted
May 26th, 2004 9:53 am
004

Ah, you’re right, Shaun. I hadn’t noticed the original’s position being so high. The point of the post is well received. Definately an interesting observation — and fun to play with, too!

Author
Sean
Posted
May 26th, 2004 10:03 am
005

Makes you wonder what they put in the coffee at the W3C sometimes, doesn’t it!

Just for the record, centered horiz./vert. is easily achievable in CSS with:

div {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100px;
    height: 200px;
    margin: 0;
    margin-left: -50px;
    margin-top: -100px;
    border: none;
    }

But I’m sure we all knew that anyway

Author
Bruce
Posted
May 26th, 2004 10:24 am
006

Bruce, what’s the border: none for?

Should note that the provided CSS causes the centered div to be pushed up and off the page as the window height is reduced. So that technique is not without it’s caveats.

Author
Shaun Inman
Posted
May 26th, 2004 10:43 am
007

Bravo.

You just (without knowing it) invented a way of vertical positioning and alignment (comething on which CSS is deeply crippled).

BRAVO!

Width to 100%, margin-top to 50% and we are as close to perfection as one could be.

Author
julik
Posted
May 26th, 2004 2:06 pm
008

How’s that Julik? What you’ve proposed sets the top left of the element to the center of the containing element. This isn’t the same thing as centering. Unless I’m misunderstanding you.

Author
Shaun Inman
Posted
May 26th, 2004 4:25 pm
009

Shaun, you are correct that the spec intends for percentage values to be based on the width — I can’t say why, but it’s clear in the CSS2 spec:

The percentage is calculated with respect to the width of the generated box’s containing block. This is true for ‘margin-top’ and ‘margin-bottom’, except in the page context, where percentages refer to page box height. (Link)

The percentage is calculated with respect to the width of the generated box’s containing block, even for ‘padding-top’ and ‘padding-bottom’.(Link)

Author
Patrick
Posted
May 27th, 2004 9:13 am
010

Thanks for digging that up Patrick (nice job with the <blockquote>s too). I wonder what the reasoning was behind percentage margins being relative to width even when describing a vertical space.

Author
Shaun Inman
Posted
May 27th, 2004 9:50 am
011

Hmm. It’s now May 27th, and I’m still watching the “margintop” and not the “sky.” Ryan must be cookin’ up something really special.

Author
Sean
Posted
May 27th, 2004 1:37 pm
012

Damn you, IE PC! What have you done to Ryan!?

Author
Shaun Inman
Posted
May 28th, 2004 4:01 am
013

In theory, you could use a similar 50% margin subtracting 50% of the height via negative padding or positioning to vertically center the image. I haven’t tried, but I’d imagine it would work.

There was a nifty CSS-based site designed by a female guru whose name I can’t remember, who used a percentage-based margin on the container of her site - I noticed this a few months ago and thought it was a neat trick, although it was unusual having the whole site shift around all the time.

Author
Scott Schiller
Posted
May 28th, 2004 11:55 am
014

I’m sorry if I’m late with this or stating the obvious, but has everyone seen this example…

Dead Centre

That page helped me figure out CSS centering and still does to this day. In this case, he could have just changed the positioning of the “horizon DIV” so that it was slightly higher than center.

Regardless, the margin-top percentage effect is very strange. I still can’t figure out what the reasoning is behind it. Please enlighten me.

Author
Matt Puchlerz
Posted
May 28th, 2004 12:19 pm
015

Regarding Matt’s comments:

I think the above link uses the same sort of technique - 50% to get the horizontal alignment, then a negative offset of half of the width and height (70px height in this case.)

A limitation of that method is its inflexibility; font scaling for example, does not cause the box to blow out in size and stay centered as the offset (35px) is fixed. I’m probably repeating someone else’s thoughts in saying, tt would be nice if a relative number like “-50%” would work ;)

Author
Scott Schiller
Posted
May 28th, 2004 1:07 pm
016

wow thats quite spiffy there.. i really like that little trick.. never thought of someting neat like that before..

Author
Adrian Kostrubiak
Posted
May 28th, 2004 6:21 pm
017

Scott: instead of then defining the height and/or width in pixels, you could supply it in em’s. That way it would scale with the fontsize.

Author
AkaXakA
Posted
May 30th, 2004 7:29 am
018

Akaxaka:

Good call. You’d think since I do that on my site as-is, this would have come to mind here. Apparently not! I really need to sit down and put together a test page to try out a few different methods.

Author
Scott Schiller
Posted
May 31st, 2004 12:43 am
019

I think it just came alive..

Author
Michael Simmons
Posted
May 31st, 2004 9:23 am
020

There’s an explanation for the speciel behavoir when using percentages with top and bottom margins. I quote from 2nd edition (2004) of Eric A. Meyers “Cascading Style Sheets - The Definitive Reference”:

Seem strange? Consider that most elements in the normal flow are (as we are assuming) as tall as necessary to contain their descendant elements, including margins. If an elements top and bottom margins were a percentage of the parent, an infinite loop could result where the parent’s height was increased to accomodate the top and bottom margins, which would then have to increase to match the new height, and so on.

A very good book btw.

Author
Stig
Posted
Jun 1st, 2004 1:04 pm
021

Thanks for sharing your find Stig. I’ve no intent of shooting the messenger here but it seems like there would be a very simple programmatic solution to the infinitely recursive percentage problem—just subtract the existing margin from the height before tabulating the percentage. I do something similar when positioning the footer on this site.

That explanation also falls apart when you consider that margins extend outside of the containing element if no borders are present—another behavior I’m not too fond of.

I’m not buying it.

Author
Shaun Inman
Posted
Jun 1st, 2004 1:20 pm