Page is a not externally linkable
SuzyUK - 1:53 pm on May 26, 2007 (gmt 0)
Probably the best known: Dead Centre The idea is to use Relative/Absolute Positioning & percentages to their fullest.
Having been inspired while reading about a new (to me) method about vertical align & internet explorer [webmasterworld.com]. I've decided to try and summarise the better known ways to achieve this along with the pros and cons of each. If after reading this you can add more methods for discussion, that would be fabulous!
This method was written about a long time ago by WPDFD [wpdfd.com] . This works very well on the surface IF you know the dimensions of both the containing element and its content, in fact it works better now that it did when it was written because Opera fixed themselves!
Example #1:
|
for: This is nice and easy when everything is measured just so. You need to know the containers height as well as the elements height.
against: The text that says it's centered is not really! try amending the font size or line-height. The (blue) #content div is the one that's centered and it will overflow when more content is added or font-size is increased.
Vertical Centering in CSS
based on method by: jakpsatweb
this one is gaining traction, but IE7's better compatibility has confused jakpsatweb's sample.
The thought behind this is quite clever however the use of hacks (for forward compatibility) is it's downfall, as it stands on the sample page, the [id] attribute selector that is used is now read by IE and its specificity (on the #middle position static rule) is overruling the necessary position absolute for IE7.
The method still works OK as long as you think about it as 2 x different methods for 2 x different browsers. and split the CSS some other way than that forward compatible hack.
It uses table properties for those browsers that understand them and a positioning method for IE.
example: #2
|
This works well when you don't know the exact height of the content that will be generated. BUT is only perfect in those browsers that use the table properties because as soon as the content exceeds the height of the containing (#outer) element IE will not stretch but instead overflows the container.
The browsers that use the table properties will expand the container 'table' so this method works nicely for auto-generated content as it will treat the height like min-height.
In IE if the content generated is too tall for the outer container then the content will overflow the container, both top and bottom, overlapping content above and below it, you can compensate for that by adding
overflow: hidden to #outer for IE. This does restrict this method to you to having to know at least one approximate height that's involved.
Vertical Alignment for IE in CSS
based on method by: Bruno Fassino
ref: this thread [webmasterworld.com]
This method combines the table properties one above with an inline trick for IE which involves adding an non-semantic HTML element, though it uses less elements in its structure.
example: #3
|
I hadn't seen this method before so it perked my nosiness. However it's only good as it stands for centering A single inline element in IE like an image or such like, and that inline element cannot span more than one horizontal line either or it breaks below the 'container' in which case it would be easier to use the line-height method where the line height of the span would be equal to the height of the container , and miss out the <i> trick altogether [** update: see note in post 3 below]
Using Line-height to Vertically Center a Single inline element
example: #4
|
Can the inline-block method work for unknown sized blocks?
It seems so!
Like I said by interest was perked at the inline-block method (#3) because I know it's possible to get IE to honour inline-block on block elements with a bit of hackery, so I thought I'd have a go at mixing the various methods to see if you could truly get a vertical center on any object any size and overflow/stretch properly if content was too long.
It seems it does work, but I need you lot to test Safari IE/mac etc..
Initial thinking:
example: #5
|
The above seems to work for anything, will center minimum content and stretch to fit if bigger, it uses padding to simulate the centering effect should the content stretch. IE7 grows by virtue that the height has been removed from
#outer for it, then because you then can't get the <i> to inherit from auto, you put the height equivalent as min-height onto the <i>. Notes:
#inner is not really an inline-block because it's not been given the second part of the hack, but it needs layout so that's all inline-block is doing for it.
Not sure what the IE/Mac CSS should be for this but if anyone wants to test the following (it will not have width but should degrade nicely) and let us know..
|
General Note:
#outer {display: table;} and #middle {display: table-cell;} along with an extra div was added to most of the above examples because Opera doesn't work/center properly unless the whole table construct is present. Next step is to see if it will work without the extra <i> element...to follow, my brains tired!
Thoughts of improvements, and any other methods with samples are appreciated
Suzy
edit reason: added numbers to examples for reference
[edited by: SuzyUK at 6:00 am (utc) on May 27, 2007]