473,735 Members | 2,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to center vertically ?

FL
Hi Everyone,

I know howto center a block using
margin-left: auto ;
margin-right: auto ;

but I'm trying to center vertically a box, any idea to solve this?
Jul 20 '05 #1
27 9250
> but I'm trying to center vertically a box, any idea to solve this?

can't be done without some kind of client-side scripting language
(JavaScript) - as far as I know. even then, how do you know it'll be
effective? can't you live without it?
Jul 20 '05 #2
FL
e n | c k m a wrote:
but I'm trying to center vertically a box, any idea to solve this?

can't be done without some kind of client-side scripting language
(JavaScript) - as far as I know. even then, how do you know it'll be
effective? can't you live without it?


I can, my boss can't ;o)
The (ugly) way to do this is create 5 frames, 4 empty on each side and
the main that receive the content but I don't like frames...
I can't see any good reason to be able to center a box horizontally and
not vertically
Jul 20 '05 #3
FL <ml**@mangoosta .fr> wrote in
<bv***********@ biggoron.nerim. net>
e n | c k m a wrote:
but I'm trying to center vertically a box, any idea to solve this?

can't be done without some kind of client-side scripting language
(JavaScript) - as far as I know. even then, how do you know it'll be
effective? can't you live without it?


I can, my boss can't ;o)
The (ugly) way to do this is create 5 frames, 4 empty on each side and
the main that receive the content but I don't like frames...
I can't see any good reason to be able to center a box horizontally
and not vertically


If a combination of elements that is ugly, deprecated, invalid, uses tables
for layout but doesn't use frames is any more acceptable - try a table with
a single cell
<td valign="middle" align="center">

That "works" when the table is set to a height of 100% - it *doesn't*
validate, though I've just checked that it does center vertically and
horizontally in IE 5x/6 Opera 7 Mozilla 1.6 and Netscape 7.1

Example at http://informer.postmaster.co.uk/index.html (nothing to do with
me, BTW)

I guess it depends on how much say the boss gets - I'm afraid that sometimes
the choice for me is informed by the question as to who pays, w3c or the
boss. Perhaps you're braver than me in standing up for truth, justice and
coding standards :)

There is a css method that requires that you know the dimensions of the
centered material. You position the material in a div which has its top left
corner at the middle of the screen - positioned top:50% and left:50% - and
then set negative margins for top and left at half the height and width of
the div. That puts the top left corner half the div's height above the
vertical center and half the div's width to the left of the horizontal
center - and so the center of the div is at the center of the window. Breaks
if the div is bigger than the window - you can't scroll left or up to see
the hidden material :(

http://www.wpdfd.com/editorial/thebox/deadcentre4.html
I'd still give talking the boss out of it a go - even if the site works now,
it's going to be a nightmare to maintain and adapt to new requirements, it
imposes severe limitations as to content... and it's a very bad thing for
which you will have to do penance. Your children will ask, "Why are those
people pointing at you in the street?"and you will have to tell them what
you did when you were young and sold your soul for a mess of pottage.

--
PeterMcC
If you feel that any of the above is incorrect,
inappropriate or offensive in any way,
please ignore it and accept my apologies.

Jul 20 '05 #4
Els


PeterMcC wrote:
FL <ml**@mangoosta .fr> wrote in
<bv***********@ biggoron.nerim. net>
e n | c k m a wrote:
but I'm trying to center vertically a box, any idea to solve this?
can't be done without some kind of client-side scripting language
(JavaScrip t) - as far as I know. even then, how do you know it'll be
effective? can't you live without it?


I can, my boss can't ;o)
The (ugly) way to do this is create 5 frames, 4 empty on each side and
the main that receive the content but I don't like frames...
I can't see any good reason to be able to center a box horizontally
and not vertically

If a combination of elements that is ugly, deprecated, invalid, uses tables
for layout but doesn't use frames is any more acceptable - try a table with
a single cell
<td valign="middle" align="center">

That "works" when the table is set to a height of 100% - it *doesn't*
validate,


it does, if you put the 100% height in the css file

example:
http://www.mediatech.nl/~rachel/Rachel/bp/Etretat7.html
css: http://www.mediatech.nl/~rachel/Rachel/bp.css

--
Els

Mente humana é como pára-quedas; funciona melhor aberta.

Jul 20 '05 #5
Els <el***********@ tiscali.nl.inva lid> wrote in
<40************ **********@drea der2.news.tisca li.nl>
PeterMcC wrote:
FL <ml**@mangoosta .fr> wrote in
<bv***********@ biggoron.nerim. net>
e n | c k m a wrote:

> but I'm trying to center vertically a box, any idea to solve this?
can't be done without some kind of client-side scripting language
(JavaScript) - as far as I know. even then, how do you know it'll
be effective? can't you live without it?

I can, my boss can't ;o)
The (ugly) way to do this is create 5 frames, 4 empty on each side
and the main that receive the content but I don't like frames...
I can't see any good reason to be able to center a box horizontally
and not vertically

If a combination of elements that is ugly, deprecated, invalid, uses
tables for layout but doesn't use frames is any more acceptable -
try a table with a single cell
<td valign="middle" align="center">

That "works" when the table is set to a height of 100% - it *doesn't*
validate,


it does, if you put the 100% height in the css file

example:
http://www.mediatech.nl/~rachel/Rachel/bp/Etretat7.html
css: http://www.mediatech.nl/~rachel/Rachel/bp.css


IIRC 100% is not a valid height for a table - the cited example has
height:580px and the vertical centring trick only works with height:100%
--
PeterMcC
If you feel that any of the above is incorrect,
inappropriate or offensive in any way,
please ignore it and accept my apologies.

Jul 20 '05 #6
Els


PeterMcC wrote:
Els <el***********@ tiscali.nl.inva lid> wrote in
<40************ **********@drea der2.news.tisca li.nl>
PeterMcC wrote:

FL <ml**@mangoosta .fr> wrote in
<bv********* **@biggoron.ner im.net>

e n | c k m a wrote:
>>but I'm trying to center vertically a box, any idea to solve this?
>
>
>can't be done without some kind of client-side scripting language
>(JavaScrip t) - as far as I know. even then, how do you know it'll
>be effective? can't you live without it?

I can, my boss can't ;o)
The (ugly) way to do this is create 5 frames, 4 empty on each side
and the main that receive the content but I don't like frames...
I can't see any good reason to be able to center a box horizontally
and not vertically
If a combination of elements that is ugly, deprecated, invalid, uses
tables for layout but doesn't use frames is any more acceptable -
try a table with a single cell
<td valign="middle" align="center">

That "works" when the table is set to a height of 100% - it *doesn't*
validate,


it does, if you put the 100% height in the css file

example:
http://www.mediatech.nl/~rachel/Rachel/bp/Etretat7.html
css: http://www.mediatech.nl/~rachel/Rachel/bp.css

IIRC 100% is not a valid height for a table - the cited example has
height:580px and the vertical centring trick only works with height:100%


oh...
well, in that case, look at this one:
http://home.tiscali.nl/~elizabeth/testonecelltable.html
I think it can be done with any height, as long as it's
specified. Being a percentage, or px or em.

Works in IE6.0, NS7.1, Moz. 1.4, Firebird 0.6.1 and in Opera
versions 7.01 and 7.23.

Almost forgot: valid html4.01 strict.

Hope this helps the op :-)

--
Els

Mente humana é como pára-quedas; funciona melhor aberta.

Jul 20 '05 #7
Els
Els wrote:
PeterMcC wrote:
Els wrote:
PeterMcC wrote:

FL wrote:

> e n | c k m a wrote:
>
>>> but I'm trying to center vertically a box, any idea to solve this?
>>
>> can't be done without some kind of client-side scripting language
>> (JavaScript) - as far as I know. even then, how do you know it'll
>> be effective? can't you live without it?
>
> I can, my boss can't ;o)
> The (ugly) way to do this is create 5 frames, 4 empty on each side
> and the main that receive the content but I don't like frames...
> I can't see any good reason to be able to center a box horizontally
> and not vertically

If a combination of elements that is ugly, deprecated, invalid, uses
tables for layout but doesn't use frames is any more acceptable -
try a table with a single cell
<td valign="middle" align="center">

That "works" when the table is set to a height of 100% - it *doesn't*
validate,

it does, if you put the 100% height in the css file

example:
http://www.mediatech.nl/~rachel/Rachel/bp/Etretat7.html
css: http://www.mediatech.nl/~rachel/Rachel/bp.css


IIRC 100% is not a valid height for a table - the cited example has
height:580px and the vertical centring trick only works with height:100%


oh...
well, in that case, look at this one:
http://home.tiscali.nl/~elizabeth/testonecelltable.html
I think it can be done with any height, as long as it's specified. Being
a percentage, or px or em.

Works in IE6.0, NS7.1, Moz. 1.4, Firebird 0.6.1 and in Opera versions
7.01 and 7.23.

Almost forgot: valid html4.01 strict.


BTW: 100% height for a tabel in CSS isn't invalid. I just
checked with the CSS Validator of W3C.
But it doesn't 'work' with Opera or Gecko, until you give
the body AND html a 100% height as well.

Someone else already made an example of this, to be found at
http://karlcore.com/100pcttable/100pctcsswithdtd.php
--
Els

Mente humana é como pára-quedas; funciona melhor aberta.

Jul 20 '05 #8
DU
FL wrote:
Hi Everyone,

I know howto center a block using
margin-left: auto ;
margin-right: auto ;

but I'm trying to center vertically a box, any idea to solve this?


<style type="text/css" media="all">
#TheBoxToCenter Vertically
{
width:356px;
height:124px;
background-color:yellow;
}

#Container
{
position:absolu te;
top:50%;
left:50%;
margin-top:-62px;
margin-left:-178px;
}
</style>
</head>

<body>
<div id="Container" >
<div id="TheBoxToCen terVertically"> inline or block stuff here</div>
</div>

will center vertically TheBoxToCenterV ertically in any/all CSS2
compliant browsers regardless of current browser viewport dimensions.

No javascript. No table design. No nested tables. No frames. No ugly,
deprecated, obsolete, invalid stuff. No non-sense tricks or complex
hack. No kidding.

DU
Jul 20 '05 #9
DU
DU wrote:
FL wrote:
Hi Everyone,

I know howto center a block using margin-left: auto ;
margin-right: auto ;

but I'm trying to center vertically a box, any idea to solve this?

<style type="text/css" media="all">
#TheBoxToCenter Vertically
{
width:356px;
height:124px;
background-color:yellow;
}

#Container
{
position:absolu te;


It should be
position:fixed;
instead!
top:50%;
left:50%;
margin-top:-62px;
margin-left:-178px;
}
</style>
</head>

<body>
<div id="Container" >
<div id="TheBoxToCen terVertically"> inline or block stuff here</div>
</div>

will center vertically TheBoxToCenterV ertically in any/all CSS2
compliant browsers regardless of current browser viewport dimensions.

No javascript. No table design. No nested tables. No frames. No ugly,
deprecated, obsolete, invalid stuff. No non-sense tricks or complex
hack. No kidding.

DU


Argh... this will in fact center within the document, not within the
viewport. There is a wide and common bug on this. Only Opera 7.x gets
this right. Sorry! It should be position:fixed and not position:absolu te.

DU
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
23079
by: Tony Vasquez | last post by:
Can somoene give me an example of how to center a div, according to it's image size... I am using different images, and so I have to resize it in a functino according to the image's size. So I use a h and w vairable that give me the height and width of the current image, but I can't seem to come up with a simple freaking formula to center the image vertically and horozontally. Thanks guys. fnFullView = function () { var h var w
10
26875
by: john T | last post by:
Is there anyway to vertically center a html table using css in such a way it does not alter the html table. When I tryied it just screws up.
10
10360
by: Unknown User | last post by:
Yet another question about vertical centering: How can I center a page vertically? I have searched on Google and didn't find anything that works at least on IE, Opera and Mozilla: http://milov.nl/code/css/verticalcenter.html Or using a table: http://www.quirksmode.org/css/centering.html Is there a way to do it and that works in Opera, IE and Mozilla? Thanks,
11
3065
by: Serg | last post by:
I had some CSS problems, and they were solved by someone who pointed out to me that I should have <!DOCTYPE ..... at the top of my code, which I now do. And it fixed all the inheritance problems, but it caused my ability to center Tables vertically to disappear. If I remove that top line, all is good. Except for my previous inheritance problems. The thing is my code calls one funtion with the parameter $out_msg which is always in...
1
2292
by: Rabbit | last post by:
Dear All I'm new to use ASP.Net for developement, can someone tell me how can I create a web form with size say 300px x 300px, align vertically center on the page Because so far I can use Div to align it horizontally center on the page, but I still can't find out how to do the "Vertically-center alignment"! 'cause I really want such form/table can be located center in the web page.
4
3773
by: John | last post by:
Is it possible to center a memo field value vertically? I use a memo field for printing address labels and I would like the addresses to show up centered vertically. Thanks, john
2
2770
by: jerrygadd | last post by:
Can some body help me vertically center an image using CSS? The customer only uses IE, and so the code is not debugged for other browsers! The image in question is found on line 1075 : "div.layout_img img" The image size is unknown I have tried to use the "margin:0px auto;" method with no success. I include the code here
0
8783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6045
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3269
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2186
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.