473,396 Members | 2,111 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,396 software developers and data experts.

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

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)
May 18 '06 #1
4 1809
On Thu, 18 May 2006 18:24:25 +0200, Alan Silver
<al*********@nospam.thanx.invalid> wrote:
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:

<html>
<head>
<title>Page Title</title>
<style type="text/css">
html, body {
background:white;
color:black;
margin:0;
border:0;
padding:0; }

body {
padding:2em 3em; }

h1 {
color:maroon; }

div#stuff {
margin:1em auto;
border:solid white;
border-width:0 10px; }
</style>
</head>
<body>
<h1>Page Title</h1>
<div id="#stuff">
Stuff
</div>
</body>
</html>

By giving the div#stuff a border of 10px width that has the same colour as
is the background colour of its parent, it simulates some extra margin.
--
______PretLetters:
| weblog | http://www.pretletters.net/weblog/weblog.html |
| webontwerp | http://www.pretletters.net/html/webontwerp.html |
|zweefvliegen | http://www.pretletters.net/html/vliegen.html |
May 18 '06 #2
In article <op.s9q93ze4l8uz2z@zoete_b>, Barbara de Zoete
<tr******@pretletters.net> writes
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)
May 18 '06 #3
VK

Alan Silver wrote:
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!!)...

<snip>

You don't actually need any divs at all for that. You already have two
containers to work with: html and body. Simply center <body> inside
<html> and you are done. See for example
<http://www.geocities.com/schools_ring/tmp/demo01/index.xml> - the gray
bars from each side is <html> with <body> width:80% centered. IMHO it
is much easier to maintain.

The relevant CSS in the sample is:

html {
margin: 0px 0px;
padding: 0px 0px;
width: 100%;
height:100%;
background-color: #DDDDDD;
}

body {
margin: 0px auto;
padding: 1em 3em;
border-left: 1px solid #000000;
border-right: 1px solid #000000;
border-top: 1px none #F5F5F5;
border-bottom: 1px none #F5F5F5;
width: 80%;
height: 100%;
font: 1em Verdana, Geneva, sans-serif;
color: #000000;
background-color: #F5F5F5}

May 18 '06 #4
In article <11**********************@j73g2000cwa.googlegroups .com>, VK
<sc**********@yahoo.com> writes
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)
May 18 '06 #5

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

Similar topics

9
by: Frances Del Rio | last post by:
when I test my stuff with the validator in HomeSite it tells me the body tag no longer reads margin attributes.. does this mean now margins are to be specified only in CSS? I work for an...
8
by: Tinus | last post by:
Hello all, Because you have been so helpfull the last couple of times, I thought after testing and wasting more than 20 pages (and google-ling for 3 days :-( ). I would ask you again for your...
6
by: martin.eyles | last post by:
I am having a problem with this page:- http://www.bytronic.com/tests/centre.html I want a nice centred paragraph, but get a justified paragraph. I expected the .centre to override the #content...
17
by: Alan Silver | last post by:
Hello, If you look at http://www.kidsinaction.org.uk/Thumbs.html you will see a simplified version of a page that could be used to display thumbnail images of various products. Now, due to...
7
by: steve | last post by:
Hi All I have created rdlc files and when I load them into Report Viewer at run time they appear OK If I click on 'Print layout' button on Report Viewer the view again appears acceptable ...
5
by: Anne DeBlois | last post by:
Hi, We are developing a database application in Visual Basic.NET 2005. The application will print label pages. Using the PrintDocument and GDI+ classes, I noticed a slight change when printing...
7
by: Mark | last post by:
Hi, I am creating application in VB 2005. and when I print report it adds extra 0.45 cm margin on left and top, and the reason for this is physical margins of printer. Is it possible to change...
6
by: Paul Wake | last post by:
Do paragraph bottom margins overrule body margins? (To explain what I mean, http://www.xmission.com/~wake/computerrules.html has 5% margins, including the bottom margin, in IE, but not in Firefox....
4
by: =?Utf-8?B?SGVyYg==?= | last post by:
Using the WebForms.ReportViewer when I export to PDF, the margins of the report are too large. How can I change the margins of my report to make better use of the printed page space?
0
by: william.oram | last post by:
Consider the table cell <td<!--Adding 'height=100 style="vertical-align:middle"' makes it easier to view --> <form> <input name="button" type=button value=Go> </form> </td> This will...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.