Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 09:24 PM
Ben Sharvy
Guest
 
Posts: n/a
Default 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.
  #2  
Old July 20th, 2005, 09:24 PM
Nick Theodorakis
Guest
 
Posts: n/a
Default Re: How to Center a Fixed-width Element (not Text) Relative to Window?

On 28 Sep 2003 18:40:57 -0700, bsharvy@mac.com (Ben Sharvy) wrote:
[color=blue]
>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.[/color]


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
nick_theodorakis@hotmail.com
nicholas_theodorakis [at] urmc [dot] rochester [dot] edu
  #3  
Old July 20th, 2005, 09:24 PM
David Dorward
Guest
 
Posts: n/a
Default Re: How to Center a Fixed-width Element (not Text) Relative to Window?

Ben Sharvy wrote:
[color=blue]
> 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.[/color]

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/
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles