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?
021 Comments
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!
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.
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.
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!
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:
But I’m sure we all knew that anyway
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.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.
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.
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)
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.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.
Damn you, IE PC! What have you done to Ryan!?
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.
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.
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 ;)
wow thats quite spiffy there.. i really like that little trick.. never thought of someting neat like that before..
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.
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.
I think it just came alive..
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”:
A very good book btw.
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.