HighDots Forums  

Can I set margins on a centred div and still use max-width?

Cascading Style Sheets Layout/presentation on the WWW (comp.infosystems.www.authoring.stylesheets)


Discuss Can I set margins on a centred div and still use max-width? in the Cascading Style Sheets forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Alan Silver
 
Posts: n/a

Default Can I set margins on a centred div and still use max-width? - 05-18-2006 , 11:24 AM






Suppose I have a div that contains the content of my page. I would like
to have this div centred in the browser, with a (say) 10px margin on
either side. So far, no problem... (WARNING - the HTML and CSS that
follow are bare minimum code, so don't flame me for not setting the
colour etc!!)...

HTML...
<div id="stuff">
stuff goes here
</div>

CSS...
body {
background: white;
}
#stuff {
margin: 0 10px;
background: pink;
}

So far so good. What if I now want to add a max-width to #stuff? If I
do, then when the client area in the browser window gets bigger than the
max width set, the #stuff div ends up on the left side of the window,
with a 10px left margin and a right margin of the client width, minus
the div width minus 10px.

If I change the margin setting for the #stuff div to look like this...

margin: 0 auto;

then when the window is large, the div is nicely centred, but when it is
small, I don't get any margins at all around the div.

So, can I centre the div when the window is big, and still have the 10px
margin when it is small? I can do either one, but I can't work out how
to do both. TIA

--
Alan Silver
(anything added below this line is nothing to do with me)

Reply With Quote
  #2  
Old   
Alan Silver
 
Posts: n/a

Default Re: Can I set margins on a centred div and still use max-width? - 05-18-2006 , 12:20 PM






In article <op.s9q93ze4l8uz2z@zoete_b>, Barbara de Zoete
<trashbin (AT) pretletters (DOT) net> writes
Quote:
Suppose I have a div that contains the content of my page. I would like
to have this div centred in the browser, with a (say) 10px margin on
either side.

What if I now want to add a max-width to #stuff?

You could do something like:
snip

Thanks for the suggestion. I actually found I could do it simply by
adding padding to the body. I never thought of that, but it basically
requires the content to keep a small distance from the edges of the
window, which was exactly what I wanted. I didn't need to muck about
with invisible borders, which was a good job as the real page as a
border set on that div already!!

Thanks again

--
Alan Silver
(anything added below this line is nothing to do with me)


Reply With Quote
  #3  
Old   
Alan Silver
 
Posts: n/a

Default Re: Can I set margins on a centred div and still use max-width? - 05-18-2006 , 03:05 PM



In article <1147979910.123511.168620 (AT) j73g2000cwa (DOT) googlegroups.com>, VK
<schools_ring (AT) yahoo (DOT) com> writes
Quote:
You don't actually need any divs at all for that. You already have two
containers to work with: html and body.
snip

That's completely brilliant!! Thanks a lot for that, I'll have a play
and see if I can simplify the code.

--
Alan Silver
(anything added below this line is nothing to do with me)


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.