473,382 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to Center a Fixed-width Element (not Text) Relative to Window?

For instance, if a style-sheet is defined as

..maintext {width:35em; background-color:white;}
body {background-color:gray;}

And the HTML is:

<div class="maintext">

some text

</div>

How do I get everything in (the white box) div element to
automatically center itself in the (gray) browser window, regardless
of size of window, zoom setting, etc.? I've tried margin and padding
percentages, but nothing works.
Jul 20 '05 #1
2 5241
On 28 Sep 2003 18:40:57 -0700, bs*****@mac.com (Ben Sharvy) wrote:
For instance, if a style-sheet is defined as

.maintext {width:35em; background-color:white;}
body {background-color:gray;}

And the HTML is:

<div class="maintext">

some text

</div>

How do I get everything in (the white box) div element to
automatically center itself in the (gray) browser window, regardless
of size of window, zoom setting, etc.? I've tried margin and padding
percentages, but nothing works.

Add this declaration to .maintext

margin: auto;

This will set allow a defined-width container to have equal left and
right margins, which will center it, at least in browsers that are
somewhat css-compliant.

Nick

--
Nick Theodorakis
ni**************@hotmail.com
nicholas_theodorakis [at] urmc [dot] rochester [dot] edu
Jul 20 '05 #2
Ben Sharvy wrote:
How do I get everything in (the white box) div element to
automatically center itself in the (gray) browser window, regardless
of size of window, zoom setting, etc.? I've tried margin and padding
percentages, but nothing works.


This should be simple, but MSIE has bugs, so it gets a little complicated.
http://dorward.me.uk/www/centre/ has the full story.

--
David Dorward http://dorward.me.uk/
Jul 20 '05 #3

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

Similar topics

0
by: Sonu K Mehrotra | last post by:
mysql server is <172.16.27.39> running on Linux 9.1 I have installed mysql control center (client GUI) on 172.16.27.252 running on Windows 2000 advanced server The machines are connected in...
32
by: Axel Dahmen | last post by:
Hi, I'd like to align a <div> within the center of the visual canvas. The text within the div should be left-aligned. How can I possibly achieve this? So far I can only align text blocks...
32
by: Axel Dahmen | last post by:
Hi, I've posted this question a few months ago but didn't get a truly satisfying answer then. So I'd like to post my question again: How can I center a DIV? The content in the DIV should be...
7
by: Petesman | last post by:
I am trying to make a prompt that will ask the user for some input... If I just use var input = prompt("dafa") everything works fine but the box is put in the top left corner of the window. I need...
10
by: Pierre Jelenc | last post by:
The demo page at http://www.robschwimmer.com/tmp/test.html has a menu at the bottom, which should be centered 50 pixels from both left and right edges. As the dotted green line around the enclosing...
2
by: dave | last post by:
trying to make adjustments to my 'position fixed' nav menu, but can't seem to get it centered, no matter how hard I try: this jumbled-mess can be viewed at: http://hardwares.po.gs/menu_test/ ...
1
by: Henry Reardon | last post by:
I have been having problems with the Development Center for several days now. It seemed to be working fine when I initially installed DB2 (LUW) Version 8 (FP7) and upgraded to FP8, except that I...
9
by: pout | last post by:
What are the purposes of fixed-point? When should it be used? I read: #define Int2Fixed(x) (((long)(short)x) << 16) and the fixed-point in 16.16 format. Does the 16 in the MACRO refer to...
12
by: Vincent Vinet | last post by:
I am trying to center a datagrid horizontally on a form, yet it stays aligned right... the horizontal align property seems to be useless, just as putting the grid inside a centered div or table...
4
by: Harris Kosmidhs | last post by:
Please check http://www.episfaleia.gr/links.php Why doesn't the page centers on screen? After googling I use for #page margin-left:auto and margin-right:auto. thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.