Re: How to center vertically ?
Lauri Raittila wrote:[color=blue]
> In article e n | c k m a wrote:
>[color=green][color=darkred]
>>>but I'm trying to center vertically a box, any idea to solve this?[/color]
>>
>>can't be done without some kind of client-side scripting language
>>(JavaScript ) - as far as I know.[/color]
>
>
> That is totally untrue. CSS has many ways to do that.
> http://www.student.oulu.fi/~laurirai/www/css/middle/
>
> The problem is that IE has so little support for things. Min-height is
> pretty much crusial when using different unit for centered element and
> paret. There is workarounds for it though.
>[/color]
Your page is interesting. Your last section is wrong though. When one
abs. pos. a child element of the body, the positioning element becomes
the root element (the initial containing block is the containing block
of such abs. pos. element; that's clearly, word for word, explained in
section 10.1 of CSS2
). Now, the root element is not the browser viewport; it's the document
per se. So, Opera 7.x renders your case correctly: it is MSIE 6, Mozilla
and others which renders this wrong. I can document this with quotes
from W3C CSS2 and W3C CSS2.1. W3C also consistently repeats that the
positioning context for fixed elements is the viewport; that is NOT the
case for absolute positioning. Same with bugzilla:
"Bug 105286: viewport used as containing block for absolutely positioned
elements instead of root (percentage height, %, bottom)"
The addendum works for Mozilla 1.6, Opera 7.50 with or without non-abs.
positioned elements before and/or after the Container div. It works even
if the positioned Container div becomes wider/longer than the viewport.
DU
-------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head profile="http://www.ietf.org/rfc/rfc2731.txt">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta name="DC.subjec t" content="Center vertically a box">
<meta name="DC.date.c reated" content="2004-02-02T09:54:03+11: 00"
scheme="W3CDTF" >
<meta name="DC.date.m odified" content="2004-02-02T09:54:03+11: 00"
scheme="W3CDTF" >
<title>Center vertically a box in the viewport</title>
<style type="text/css" media="screen,t v,projection">
#TheBoxToCenter Vertically
{
width:356px;
height:124px;
background-color:yellow;
}
#Container
{
position:fixed;
/* no hack for MSIE for now */
top:50%;
left:50%;
margin-top:-62px;
margin-left:-178px;
}
</style>
</head>
<body>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<div id="Container" >
<div id="TheBoxToCen terVertically"> inline or block stuff here</div>
</div>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
</body></html>
Lauri Raittila wrote:[color=blue]
> In article e n | c k m a wrote:
>[color=green][color=darkred]
>>>but I'm trying to center vertically a box, any idea to solve this?[/color]
>>
>>can't be done without some kind of client-side scripting language
>>(JavaScript ) - as far as I know.[/color]
>
>
> That is totally untrue. CSS has many ways to do that.
> http://www.student.oulu.fi/~laurirai/www/css/middle/
>
> The problem is that IE has so little support for things. Min-height is
> pretty much crusial when using different unit for centered element and
> paret. There is workarounds for it though.
>[/color]
Your page is interesting. Your last section is wrong though. When one
abs. pos. a child element of the body, the positioning element becomes
the root element (the initial containing block is the containing block
of such abs. pos. element; that's clearly, word for word, explained in
section 10.1 of CSS2
). Now, the root element is not the browser viewport; it's the document
per se. So, Opera 7.x renders your case correctly: it is MSIE 6, Mozilla
and others which renders this wrong. I can document this with quotes
from W3C CSS2 and W3C CSS2.1. W3C also consistently repeats that the
positioning context for fixed elements is the viewport; that is NOT the
case for absolute positioning. Same with bugzilla:
"Bug 105286: viewport used as containing block for absolutely positioned
elements instead of root (percentage height, %, bottom)"
The addendum works for Mozilla 1.6, Opera 7.50 with or without non-abs.
positioned elements before and/or after the Container div. It works even
if the positioned Container div becomes wider/longer than the viewport.
DU
-------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head profile="http://www.ietf.org/rfc/rfc2731.txt">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta name="DC.subjec t" content="Center vertically a box">
<meta name="DC.date.c reated" content="2004-02-02T09:54:03+11: 00"
scheme="W3CDTF" >
<meta name="DC.date.m odified" content="2004-02-02T09:54:03+11: 00"
scheme="W3CDTF" >
<title>Center vertically a box in the viewport</title>
<style type="text/css" media="screen,t v,projection">
#TheBoxToCenter Vertically
{
width:356px;
height:124px;
background-color:yellow;
}
#Container
{
position:fixed;
/* no hack for MSIE for now */
top:50%;
left:50%;
margin-top:-62px;
margin-left:-178px;
}
</style>
</head>
<body>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<div id="Container" >
<div id="TheBoxToCen terVertically"> inline or block stuff here</div>
</div>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
<p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p><p>filler</p>
</body></html>
Comment