| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
Senior Member
|
What am I missing here.
I've never quite grasped how margins and padding works in css. I'm simply trying to make a nested box (200x200) sit in the exact centre of it's containing box (300x300) I've tried two things: Specifying margins of 50px on each side of the small box. Specifying 50px padding on each side of the containing box. 200+50+50=300 so both these examples should work, but they don't. Could someone cast some light over this? Code:
|
|
|
|
|
|
#3 (permalink) |
|
goober :-)
|
Padding isnt included in the "width" and "height" dimensions... So if you want a 200px square box, centered inside a 300px square box, your math is right, but your implementation is wrong. What you would actually need to do is create your #bigredbox with a height and width of 200px, and a padding of 50px, and then your "#littlebluebox" with just the height and width set... no margins needed. If you're struggling to understand this stuff, then consider doing some reading up on the box model. Also helpful to you would be the firebug plugin for firefox (www.getfirebug.com). The "layout" view for inspected elements gives a quite elegant explanation of how dimensions, margins and padding are applying on the element you are looking at. Hope that helps in some way David Oh, and Proc... Padding can be a very useful tool in a web designer's toolbox... You just have to know how to use it right, and try not to mix in padding and margins on the same element, as that is when box model rendering problems start to crop up. Margins are all well and good, but sometimes padding is a better option. My signature sucks.
|
|
|
|
#5 (permalink) |
|
Happy Easter.
Join Date: Feb 2005
Posts: 6,336
|
Meeeeow. Simple rule of thumb - don't pad elements with a width applied - a concept that took me ages to get used to - but once you fix certain aspects and use the block property for the rest it's easy peasy...helps avoid loads of display issue in IE text-align:center; and margin:0 auto; are your friends |
|
|
|
#6 (permalink) |
|
Senior Member
|
So, if I had a 100x200 containing box and a 100x100 box inside, and I wanted to push the small box to the bottom of the containing box, then adding 100px padding to the top would have the effect of increasing the size of the containing box to 100x300. Even though there is space within the box that the small box can be pushed down into. This goes against my logic tbh, thanks for your help tho. I will have a look at the box model to try and get a bit more understanding. |
|
|
|
#7 (permalink) |
|
Happy Easter.
Join Date: Feb 2005
Posts: 6,336
|
IE5-6 doubles padding on container elements with widths/heights - It's fucking annoying e.g. a container with a width set to 300px and padding 20px will display at 340px. So try to avoid using both - and pad the contents - it's usually possible to do this without adding extra markup. |
|
|
|
#9 (permalink) | |
|
Senior Member
|
Quote:
So how would you, or anyone create a simple box with a smaller box centred inside, like I've attempted to do above? could you quote the css code you would use. |
|
|
|
|
#10 (permalink) | |
|
Senior Member
|
Quote:
That's a golden rule, I think i'm starting to understand padding now. Code:
|
|
|
![]() |