Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Basic layout Q - Margins

Featured Replies

Hi,

 

I'm starting a page with a #container div with static background image and 100% width, then within that I have a #main div at 960 px width and centered (using 0 auto margins). When I try to add a top margin to the #main div of 60px it puts a 60px white margin at the top of the whole page (so the background image of the container div starts 50px below the top of the screen).

 

I just want the main div to have a top margin, not the container.

 

I can put a header div in with the same background as the main static bg image but it is not aligned and i'm sure using margins would be a cleaner way to do it.

 

Any ideas? Thanks

It sounds like margin collapse (Google it for an explanation) but basically you may achieve the result using padding-top: 60px; in the #main style instead of margin-top. A background for #main (if you have one) will show in the padding.

 

If that doesn't do what you want, study margin collapse tutorials like http://www.howtocreate.co.uk/tutorials/css/margincollapsing

 

Margin collapse may seem the wrong wording when you are getting an extra margin, but what may be happening is that the margin-top for the child #main div is extending outside its box (collapsing the box) and applying itself to the parent #container

Hey mate

 

I encountered an issue like this before.

 

Try adding padding-top: 1px; to the containing div. This usually fixes the issue but without seeing your code it's hard to be 100% sure of a solution.

 

Jebediah :)

 

 

Hi,

 

I'm starting a page with a #container div with static background image and 100% width, then within that I have a #main div at 960 px width and centered (using 0 auto margins). When I try to add a top margin to the #main div of 60px it puts a 60px white margin at the top of the whole page (so the background image of the container div starts 50px below the top of the screen).

 

I just want the main div to have a top margin, not the container.

 

I can put a header div in with the same background as the main static bg image but it is not aligned and i'm sure using margins would be a cleaner way to do it.

 

Any ideas? Thanks

  • 2 weeks later...

Not sure if you've already sorted this or not. But you need a bit of revision on the css box model.

 

Essentially all elements are contained in a box. Adding a margin adds space outside of this box, so if you have a background color in the box, it won't be shown in the margin. Adding some padding adds space inside the box, so the size of the box becomes whatever size you set it, plus the padding to give a total size.

 

Hope this helps

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.