473,785 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ 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 2515
"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.u k (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.u k> wrote in message
news:%b******** ************@ne ws-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.u k (remove [no-spam] to reply)

Jul 20 '05 #3
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> 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/rasterTriangleD OM.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.u k> 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.u k (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
2818
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 development process of this, the most popular of the web-based, server-side, glue-languages. That said, most descriptions of what is good about php, fail to do it justice. Although they are generally enthusiastic and sometimes fanatical, no...
226
12706
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 "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
125
14855
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 software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
3
8883
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 { mySocketClass sss; ...
6
2005
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 opinion) is to create a reusable framework that can be applied to similar projects. However I have found that if an abstraction is created first during the development phase, when the implementation is occurring, the functionality or intended behaviour...
6
1823
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 interface to a Smart Phone/Treo solution eventually. I would like to create our apps in the most current development environment but, do I abandon the webservice-->Dataset--> XML -->Http-->XML --> dataset -->gridview methodology? Is there a better...
4
4911
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 Advanced Imaging (JAI), ImageMagick, and GIMP all praised as best in different places.
98
4626
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
1912
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 a Product, I want a different formview, fvp, to show. Is it best to have the two forms on separate pages, productView.aspx
19
4644
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 isolate the problem: class MyClass: def __init__(self,s): self.mystring = s
0
10356
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
10161
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...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
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
8986
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
7506
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
6743
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
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2890
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.