473,473 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is the best way...


What is the best way to center one, or more DIVs in the browser window?
Can someone give me a quick code snipplette. I need something that will
center the DIV, for all sorts of awkward screen resolutions. Remeber, with
that said, I need it to be centered in the BROWSER window, not the screen.
User must be able to resize his browser window to anything though. Anywho,
if you need more info, let me know, and thanks in advance.

Tony
Jul 20 '05 #1
5 2496
"Tony Vasquez" wrote on 12/11/2003:

What is the best way to center one, or more DIVs in the browser window? Can someone give me a quick code snipplette. I need something that will center the DIV, for all sorts of awkward screen resolutions. Remeber, with that said, I need it to be centered in the BROWSER window, not the screen. User must be able to resize his browser window to anything though. Anywho, if you need more info, let me know, and thanks in advance.


Assuming this is as simple as you describe it, I have three words for
you: Cascading Style Sheets. You can do it two ways:

Add this to the document HEAD:

<STYLE type="text/css">
<!--
.centre {
text-align: center;
}
-->
</STYLE>

Then, for every element that needs to be centred, add the 'class'
attribute like so:

<DIV class="centre">Your content</DIV>

Alternatively, you can do this:

<DIV style="text-align: center">Your content</DIV>

That 'style' has to be copied into every element to be centred, just
like 'class' above.

As you can probably tell, this has nothing to do with JavaScript.
This is just a way to replace the depreciated CENTER element. If
you're referring to centring layers, and other things that aren't part
of normal HTML structure, then you need to explain exactly what you're
doing.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 20 '05 #2
Okay, well... I guess I came off like I don't know much, but here is what I
need. I need a script to VERTICALLY align, and horozontally align my DIV
(which I use for the BODY of my web site). So, I mostly need to know how to
get the same effect as maybe a putting it in a table cell and adding
valign=middle to it. Thing is, this web site is table-less.... no tables.
If you need further info, please let me know.

Tony

"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message
news:%b********************@news-text.cableinet.net...
"Tony Vasquez" wrote on 12/11/2003:

What is the best way to center one, or more DIVs in the browser

window?
Can someone give me a quick code snipplette. I need something that

will
center the DIV, for all sorts of awkward screen resolutions.

Remeber, with
that said, I need it to be centered in the BROWSER window, not the

screen.
User must be able to resize his browser window to anything though.

Anywho,
if you need more info, let me know, and thanks in advance.


Assuming this is as simple as you describe it, I have three words for
you: Cascading Style Sheets. You can do it two ways:

Add this to the document HEAD:

<STYLE type="text/css">
<!--
.centre {
text-align: center;
}
-->
</STYLE>

Then, for every element that needs to be centred, add the 'class'
attribute like so:

<DIV class="centre">Your content</DIV>

Alternatively, you can do this:

<DIV style="text-align: center">Your content</DIV>

That 'style' has to be copied into every element to be centred, just
like 'class' above.

As you can probably tell, this has nothing to do with JavaScript.
This is just a way to replace the depreciated CENTER element. If
you're referring to centring layers, and other things that aren't part
of normal HTML structure, then you need to explain exactly what you're
doing.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)

Jul 20 '05 #3
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> writes:
"Tony Vasquez" wrote on 12/11/2003:
Assuming this is as simple as you describe it, I have three words for
you: Cascading Style Sheets. You can do it two ways:

Add this to the document HEAD:

<STYLE type="text/css">
<!--
.centre {
text-align: center;
}
-->
</STYLE>
In a standard compliant browser, this will *not* center a div, as the
orignal poster wanted. The property "text-align" should only affect
inline child elements of the element it is applied to.

IE has a bug that makes text-align work on block level children as
well. There is some amount of luck in that, IE's prior to version 6
couldn't understand the correct way to center a block level element
(margin:auto).

So, to center a div in the viewport of a standards compliant browser:

<style type="text/css">
.center {
width:200px;
height:200px;
position:fixed;
top:0px;
right:0px;
bottom:0px;
left:0px;
margin:auto;
}
</style>

IE<6 won't understand a word of it, sadly, and IE 6 still doesn't
understand position:fixed. It will center horizontally, though.

You can use tricks with negative margins

<style type="text/css">
.center {
position:fixed;
height:200px;
top:50%;
margin-top:-100px;
width:200px;
left:50%;
margin-left:-100px;
}
</style>

It is dangerous, though. If there is not enough room for the element,
it will be partyl outside the top or left edge of the browser, and
you can't scroll it into view.
As you can probably tell, this has nothing to do with JavaScript.


That would be the worst possible way to center, indeed. Mostly because
it doesn't work at all with Javascript turned off.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
"Lasse Reichstein Nielsen" wrote on 12/11/2003:
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> writes:
"Tony Vasquez" wrote on 12/11/2003:
Assuming this is as simple as you describe it, I have three words for you: Cascading Style Sheets. You can do it two ways:

Add this to the document HEAD:

<STYLE type="text/css">
<!--
.centre {
text-align: center;
}
-->
</STYLE>
In a standard compliant browser, this will *not* center a div, as

the orignal poster wanted. The property "text-align" should only affect
inline child elements of the element it is applied to.

IE has a bug that makes text-align work on block level children as
well. There is some amount of luck in that, IE's prior to version 6
couldn't understand the correct way to center a block level element
(margin:auto).

So, to center a div in the viewport of a standards compliant browser:
<style type="text/css">
.center {
width:200px;
height:200px;
position:fixed;
top:0px;
right:0px;
bottom:0px;
left:0px;
margin:auto;
}
</style>

IE<6 won't understand a word of it, sadly, and IE 6 still doesn't
understand position:fixed. It will center horizontally, though.

You can use tricks with negative margins

<style type="text/css">
.center {
position:fixed;
height:200px;
top:50%;
margin-top:-100px;
width:200px;
left:50%;
margin-left:-100px;
}
</style>

It is dangerous, though. If there is not enough room for the element, it will be partyl outside the top or left edge of the browser, and
you can't scroll it into view.
As you can probably tell, this has nothing to do with JavaScript.
That would be the worst possible way to center, indeed. Mostly

because it doesn't work at all with Javascript turned off.


I apologise for that nonsense then. I guess that's what happens when
you develop with only one, particularly broken browser.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 20 '05 #5
Tony Vasquez wrote:
Okay, well... I guess I came off like I don't know much, but
here is what I need. I need a script to VERTICALLY align, [...]
Feed `CSS vertical align' to (y)our friend(!) Google
[Top post]


and stop doing that:
http://www.netmeister.org/news/learn2quote.html
PointedEars
Jul 20 '05 #6

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

Similar topics

23
by: darwinist | last post by:
What PHP Represents There is no shortage of complaints one could make about php as a language, and although the list does shrink with each release, some of them are inherent to the origins and...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
3
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form {...
6
by: Mark Broadbent | last post by:
this might sound like an obvious question but I have found that usually these two evolve at the same time. One of the biggest reasons for creating the abstraction in the first place (in my...
6
by: jhooper71 | last post by:
It's been recommended to me to use a webservice and XML for the data manipulation layer for web applications in .NET 1.1. I was thinking I could use the web service to extend the database...
4
by: Ron Brennan | last post by:
Good evening, Windows 2000, JDK 1.5. What opinions do people have on what way and tool programmaticly produces the best quality thumbnails from larger images? On the web I've seen Java...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
2
by: kbutterly | last post by:
All, I have a menu which contains Category as the master and Product as the child. When I click on a Category in the menu, I want one formView control, fvpc, to show, and then when I click on...
19
by: jsanshef | last post by:
Hi, after a couple of days of script debugging, I kind of found that some assumptions I was doing about the memory complexity of my classes are not true. I decided to do a simple script to...
0
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,...
0
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...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.