473,382 Members | 1,349 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.

Centering

Hi all,

I am trying to get a CSS defined layout to center on the page, have
tried doing this with the <center></center> tags but than everything
is centered, including text etc etc.

What should I put into the stylesheet so that, no matter what
resolution the viewer's screen is set to, the layout is always
displayed centered ?
Erik Schulp
www.sgtclog.nl
eschulp@-houjerotzooimaar-home.nl

-Let's all hope there is intelligent life on other planets,
cuz' here on earth, there is bugger all!- *Monty Python*
Jul 20 '05 #1
7 2565
Erik Schulp wrote:
Hi all,

I am trying to get a CSS defined layout to center on the page, have
tried doing this with the <center></center> tags but than everything
is centered, including text etc etc.

What should I put into the stylesheet so that, no matter what
resolution the viewer's screen is set to, the layout is always
displayed centered ?
Erik Schulp
www.sgtclog.nl
eschulp@-houjerotzooimaar-home.nl

-Let's all hope there is intelligent life on other planets,
cuz' here on earth, there is bugger all!- *Monty Python*

Have you tried wrapping your content in something like:

<div id="wrap">
<div id="wrapit">
...
</div>
</div>

and in CSS:

#wrap {text-align: center}
#wrapit {text-align: left; margin: auto auto; width: 100%}

The wrap is needed to address an IE5 bug - wrapit is used for conforming
browsers wherein the margin: auto (for at least left and right) and the
explicit width help center the content.

HTH.

--Nikolaos

Jul 20 '05 #2
Nikolaos Giannopoulos wrote:
What should I put into the stylesheet so that, no matter what
resolution the viewer's screen is set to, the layout is always
displayed centered ?
<div id="wrap">
<div id="wrapit">
...
</div>
</div>

and in CSS:

#wrap {text-align: center}
#wrapit {text-align: left; margin: auto auto; width: 100%}


margin: auto would suffice
The wrap is needed to address an IE5 bug - wrapit is used for conforming
browsers wherein the margin: auto (for at least left and right) and the
explicit width help center the content.


Note that width set to 100%, the div won't be centered, it will extend
from one side of the window to the other. I'd recommend setting a
width in em units, and set a max-width to avoid horizontal scroll bar,
but max-width doesn't work with MSIE 5.x. :(

#wrap {
text-align: center;
}
#wrapit {
text-align: left;
margin: auto;
width: 55em;
max-width: 95%;
}

You could set the width to auto and specify a margin instead.

#wrap {
text-align: center;
}
#wrapit {
text-align: left;
margin: auto 5% ;
}

or margin: auto 2em or auto 5px or whatever suits you.

--
Brian
follow the directions in my address to email me

Jul 20 '05 #3
Brian wrote:
Nikolaos Giannopoulos wrote:
#wrap {text-align: center}
#wrapit {text-align: left; margin: auto auto; width: 100%}
margin: auto would suffice


Sure. That's one of those things were I had specific values (for
top/bottom) and didn't collapse them after switching to auto. Thanks.

The wrap is needed to address an IE5 bug - wrapit is used for
conforming browsers wherein the margin: auto (for at least left and
right) and the explicit width help center the content.


Note that width set to 100%, the div won't be centered, it will extend
from one side of the window to the other.


In our real page I have a width of 93% - just tried to simplify it for
posting though but that wasn't a good idea - once again thanks for the
correction.

I'd recommend setting a width
in em units, and set a max-width to avoid horizontal scroll bar, but
max-width doesn't work with MSIE 5.x. :(
How would you determine the value to specify the width in em units? I
can see possibly setting the margin in em's but it doesn't seem natural
to me to set the width in em's....

#wrap {
text-align: center;
}
#wrapit {
text-align: left;
margin: auto;
width: 55em;
max-width: 95%;
}

You could set the width to auto and specify a margin instead.

#wrap {
text-align: center;
}
#wrapit {
text-align: left;
margin: auto 5% ;
}

or margin: auto 2em or auto 5px or whatever suits you.


Personally I like explicitly specifying the width when centering as I
*easily* know how much screen space is being used.

What's the benefit of setting the margin in em's though?

Do you really want the margin to be larger based on a larger font size -
this may be a silly question but I just don't see why such a situation
is desireable.

--Nikolaos

Jul 20 '05 #4
Nikolaos Giannopoulos wrote:
Brian wrote:
I'd recommend setting a width in em units, and set a
max-width to avoid horizontal scroll bar, but max-width
doesn't work with MSIE 5.x. :(
How would you determine the value to specify the width in em units?


Not sure I get your question. The words are clear enough, but not the
sense. One determines the value according to the need. My personal
site uses a max-width of 55em. I don't want it centered, so width is
auto. Thus, at smaller window sizes, the content just rewraps.
I can see possibly setting the margin in em's
I actually don't set margins in em units. But then, neither do you,
if I am to believe what you write below!
but it doesn't seem natural to me to set the width in em's....
em units are based on the font size, so they are the best unit to set
width for any container that contains all or mostly text.
Personally I like explicitly specifying the width when centering as I
*easily* know how much screen space is being used
Sure, if you use percentages. I don't use percentages for width, I
use em units. But if you use anything other than percentage, then you
run the risk of horizontal scroll bars at narrow window widths.
Setting the max-width to 98% fixes that -- except that MSIE/Win
doesn't recognize max-width. Three solutions: (1) leave it, and know
that MSIE users may have a less-than-ideal situation; (2) use
percentage for width; (3) set the margins to a width instead of auto,
and set the width to auto instead of a value. You can use percentage
or pixels if you like, as you are aware.

margin: auto 30px
What's the benefit of setting the margin in em's though?
None. I was merely providing other options for the op.
Do you really want the margin to be larger based on a larger font size -
this may be a silly question but I just don't see why such a situation
is desireable.


There is some disagreement in ciwas. I don't use it. Or rather, I
have yet to see a need for it. Pixels for margin works fine for me.
We recently had a discussion about this, where other(s) thought em
units made more sense. Percentage would work also, if they are kept
small, but I have never used them.

--
Brian
follow the directions in my address to email me

Jul 20 '05 #5
Nikolaos Giannopoulos wrote:
Brian wrote:
Nikolaos Giannopoulos wrote:

My personal site uses a max-width of 55em.
How did you come up with 55em? Trial and error I imagine - Right?


To some degree, yes. As discussed in a thread in ciwas many months
ago, usability studies suggest that reading very long lines of text is
difficult. The reader can lose her/his place when moving to the next
line of text. So I merely wanted to suggest a presentation that would
not have very long lines of text.
What if you wanted to double the existing margin on both sides
sometime down the road - would that be 50em, 52em, 53em????
Only a right margin is specified on the body element at this time.
But sure, I could change that width if it were desirable. You make
this sound like a terrible thing. I don't regard making changes in a
single css file a huge obstacle. Indeed, it is necessary to change
that file to change the layout. We seem to be talking past each other
here.
If you use percentages for the width then I think the intent is
clearer
The intent is clearer? What is unclear about em units?
and so is maintaining the pages down the road.
If I set a width in percentages, and wanted to change it down the
road, I'd have to make the same sort of edit of the same css file.
I can see possibly setting the margin in em's


I actually don't set margins in em units. But then, neither do
you, if I am to believe what you write below!


I said I can see doing it - I didn't say that I would do it.


Fair enough. But I hope you can understand my confusion. When I say
"I can see..." I mean, it seems sensible, I might do the same in
certain situations.
You suggested the following options in your reply to my post:
or margin: auto 2em or auto 5px or whatever suits you. ^^^


I did indeed write that. Some people like to set margin with em
units. I never have, but I hardly think it is such a terrible idea
that the op should avoid it all costs. I see no usability problems in
the general principle of using em units for margin. YMMV.
em units are based on the font size, so they are the best unit to
set width for any container that contains all or mostly text.


I agree with your comment on what em units are but I disagree with
the rest of your statement - especially the wording "best unit to
set width for any container that contains all or mostly text".

I guess I'll ask the same question - Why would I want the width of
the page to get larger/smaller based on a large font and vice
versa?


You have changed what I said in a subtle way, possibly because my
earlier post made things vague. In the part quote above, I said em
was the best unit for a text container, e.g.,

<ul class="navigation">
<li>home</li>
<li><a href="gallery.html">photo gallery</a></li>
<li><a href="statement.html">artist statement</a></li>
<li><a href="contact.html">contact the artist</a></li>
</ul>

ul.navigation {
width: 10em ;
float: left ;
}

If the body element is a text container, and it usually is, then the
caveats that I mentioned about page width apply.
If the page is liquid then why would I even desire such an effect.
I did suggest setting a width in em units, *with a max-width set as
well*, and I also added the caveat that max-width fails in MSIE/Win.
I then presented another suggesting avoiding width altogether. My
earlier post may have been unclear. To set a width on the body
element, a percentage is likely the safest bet with MSIE/Win.
But if you use anything other than percentage, then you run the
risk of horizontal scroll bars at narrow window widths.
Yes. As I mentioned.
Say goodbye to your nice liquid layout and hello to jello ;-)
I don't know what the jello thing means. But I am curious. An acronym?
Maybe if you post the url to your site
< http://www-unix.oit.umass.edu/~btrembla/ >
that uses the 55em width then things can be discussed more
concretely as we could simply try it out with different font sizes
and see the results.
I think you didn't read what I wrote, even though you quoted it above.
I set *max-width* to 55em on the body element of my site, and *not*
the width. In MSIE/Win, max-width is ignored. No harm done. Look at
what you quoted if you don't believe me! :-p
Setting the max-width to 98% fixes that -- except that MSIE/Win
doesn't recognize max-width.


Unfortunately considering market share this is huge problem isn't
it.


I did point that out in my post, with an alternative solution.
Not to mention that it seems like this additional setting is a
kludge to fix a problem when there need not be a problem to fix (if
one sticks to percentages) - or so it seems IMO.
Max-width is not a kludge. It is a standard part of css.
Three solutions: (1) leave it, and know that MSIE users may have
a less-than-ideal situation;


what solution is (1)?????


<sigh> One can design a site that is functional and accessible in
less-conforming browsers, and that has added presentational features
in more conforming browsers. The whole point of css is to add
visual/aural/... usability and appeal for those browsers that
understand it. Lynx ignores my stylesheet, graphical browsers
(mostly) do not. I make my stylesheet for visitors who use graphical
browsers like MSIE, which gets some of css. I put some things in that
MSIE/Win users will not get, but that enhance the site further for
Mozilla and Opera users.

div#content {
width: 95% ;
max-width: 55em ;
}

/* the following rule is ignored by MSIE/Win */
body>div#content {
width: 55em ;
max-width: 98% ;
}

This is only a hypothetical case. Without content, I cannot explain
why someone would want it. It depends on the context.
What's the benefit of setting the margin in em's though?


None. I was merely providing other options for the op.


Once again we agree but IMO I think that there are additional
disadvantages as mentioned above by myself and yourself.


It depends on the need and the wishes of the author. I see no
usability problems with margin set in em units. If you can think of
one, then point it out.
But you do use it but you may not realize it - Whether you set the
width in em's / margin auto (as I believe is your preference)
My preference is not to center pages. Frankly, I don't get the
obsession with centering everything. Again, that's me.
OR width auto and margin in em's I think you are still going to hit
a problem except the effect is reversed i.e. in the former case the
width of the content area will grow with a larger font and in the
latter case it will shrink with a larger font.


Yes. Mind you, in the latter case, there is no threat of the dreaded
horizontal scroll bars.

--
Brian
follow the directions in my address to email me

Jul 20 '05 #6
Brian wrote:
Nikolaos Giannopoulos wrote:
My personal site uses a max-width of 55em.

I think you didn't read what I wrote, even though you quoted it above.


Ahhhhh!!!! I see. Your right ;-)

I set *max-width* to 55em on the body element of my site, and *not*
the width.


Funny how mis-reading the prefix "max-" above in your comment (twice)
can make such a huge difference. My apologies.

--Nikolaos

Jul 20 '05 #7
Nikolaos Giannopoulos wrote:
Brian wrote:
Nikolaos Giannopoulos wrote:

I set *max-width* to 55em on the body element of my site, and *not*
the width.
Funny how mis-reading the prefix "max-" above in your comment (twice)
can make such a huge difference.


That would have been daft, it's true.
My apologies.


No foul. :)

--
Brian
follow the directions in my address to email me

Jul 20 '05 #8

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

Similar topics

11
by: Jeff Thies | last post by:
I have a series of blocks that are float left that I need centered on the page. <div class="center" align="center"> <div style="width: 100 px;float: left">thumbnail 1</div> <div style="width:...
6
by: Fran¨ois de Dardel | last post by:
http://mapage.noos.fr/dardelf3/tintin/page3bits.html Can I center the series of thumbnails horizontally in the pages _and_ keep the "elastic arrangement" where the number of thumbnails adapts to...
3
by: Jonah Bishop | last post by:
I have a puzzling problem with centering text, and I'm hoping that someone here can help me out. First of all, let me state that I am using XHTML 1.0 Strict and CSS for all layout purposes (no...
2
by: Ryan W Sims | last post by:
I'm having trouble with centering in IE... http://www.ryanwsims.com/koh/ The image should center over the text. It does in Firebird, but not in IE for some reason. If you look at ...
9
by: Pierre Jelenc | last post by:
Is there a way to center (horizontally) a UL list of unknown width? I can put it in a DIV that's centered with "margin-left: auto; margin-right: auto;" but I then have to specify a width; otherwise...
15
by: red | last post by:
How do I center two side by side divs ? I've been writing css pages for a while but there's one thing tha still eludes me. I can center a div with margin auto. I can place two divs side by side...
6
by: Axel Siebenwirth | last post by:
Hi, as described at http://www.quirksmode.org/css/centering.html, I try to do to centering with my site. I did exactly as told on that page but it only seems to center horizontally. My site...
3
by: John Pote | last post by:
1. Horizontal centering a <divin browser window. The current trend seems to be to place page content in a fixed width area in the middle of the browser window. How is this achieved? If I use a...
5
by: Markus Ernst | last post by:
Hello This is a test example: http://www.markusernst.ch/anthracite/ http://www.markusernst.ch/anthracite/living_divani.html After googling and experimenting for several hours, I ended up...
1
by: =?Utf-8?B?ZnJhbmt5?= | last post by:
Hello, I've created a table that has two rows that are span across three columns. The third row has three columns, each with an image. The last row is also span accross three columns. The span...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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.