473,883 Members | 1,723 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 9272
> 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
23089
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
26891
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
10369
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
3088
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
2301
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
3777
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
2778
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
9938
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11140
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10414
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9572
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...
0
7125
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
5797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4218
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3232
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.