![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||||
| |||||
|
|
Does anyone grok browser variations for collapsing vertical margins, and which is correct behaviour? Embed <div> inside <div>. Place padding on the outer <div>, margin on the inner <div>. Should vertical margins between inner <div>s collapse? |
|
Should such a vertical margin before the _first_ <div> collapse? IE does, FF doesn't. |
|
Demo example: http://codesmiths.com/dingbat/lj/use...tical_margins/ |
|
I need to do a "gridded" layout (in graphic design terms) which means one or two design elements within an overall coloured block, and maintaining a gutter between them, and inside the overall dimensions of the block. Yes, it's all paper-based guff, but I'm trying to translate corporate marketing-speak into web design 8-( What I really need is some bulletproof CSS that I can then let a whole bunch of HTML coders loose on. So I need stability, simplicity and cross-browser consistency. Am I best shooting for margins (on the inner) here, or padding (on the outer)? |
|
Relevant spec: CSS 2.1 specification: 8.3.1 Collapsing margins http://www.w3.org/TR/CSS21/box.html#collapsing-margins Demo example: http://codesmiths.com/dingbat/lj/use...tical_margins/ Thanks for any comments. Apologies for not doing all my own research, but I'm really out of time tonight. 8-( |
#3
| |||
| |||
|
|
You've only got one inner <div> so I don't really understand the question. |
|
I suppose padding may be less likely to exercise browser bugs as margin collapsing is tricky to get right. |
|
However Firefox is not perfect. Change that 3em top margin on the cleared <p> to 1000px and you should see some margin, but not in Firefox (or Opera or Konqueror surprisingly, making me wonder if I have interpreted the specification wrong. But it seems quite clear). |
#4
| ||||||||
| ||||||||
|
|
On 23 Jan, 18:16, Ben C <spams... (AT) spam (DOT) eggs> wrote: You've only got one inner <div> so I don't really understand the question. My example has one inner <div>. Live code may have one, and will certainly also have two inner <div>s (floated alongside each other). Probably it may also a number of divs, not floated and appearing vertically in normal sequence. |
|
What I want to achieve here (ideally) is simple bulletproof CSS that can be used with simple HTML to produce the necessary. I'd like to avoid any need to mark the inner content as class="first-element" or similar. |
|
If _all_ margins (H&V) collapsed, in this context then I'd get the behaviour I need automatically, for almost no effort. 8-( |
|
As it is, it looks like I need to (for browser compatibility) always set the top margin to 0, then work from that point. The requirements of consistency first, then getting the design right. I think FF's behaviour of not-collapsing the first is the correct one, even if I'm not sure quite which rule(s) are triggering this. Is it because its parent is padded? |
|
Or because it's floated? |
|
Not what I _want_ here anyway, in this particular case, but so it goes. I suppose padding may be less likely to exercise browser bugs as margin collapsing is tricky to get right. The trouble with that is that I don't think I can use it to space elements apart when I'm looking at multiple siblings (unlike the single element example). |
|
However Firefox is not perfect. Change that 3em top margin on the cleared <p> to 1000px and you should see some margin, but not in Firefox (or Opera or Konqueror surprisingly, making me wonder if I have interpreted the specification wrong. But it seems quite clear). Agreed. FF's behaviour is clearly "make the border barely clear of the previous", no matter what the margins. I don't really know what IE does here. It appears to be doing much the same (i.e. it's not calculating anything inbetween), but it bases things on the outside of the box, not the outside of the border. |
|
I haven't studied the spec carefully enough to judge which is better. |
#5
| |||
| |||
|
|
On 2008-01-23, Andy Dingley <dingbat (AT) codesmiths (DOT) com> wrote: Does anyone grok browser variations for collapsing vertical margins, and which is correct behaviour? Demo example: http://codesmiths.com/dingbat/lj/use...tical_margins/ As for the non-float after a float, what you're seeing there is not margin-collapsing, but a consequence of the fact that "clear" generates enough clearance to get the top _border_ of the cleared box underneath the floats. (See 9.5.2 CSS 2.1). As 9.5.2 explains the browser is supposed to do margin-collapsing first, then move the box even further down if necessary to achieve clearance. In this case your 3em margin is probably not enough to get the <p> below the floats, so it effectively disappears. However Firefox is not perfect. Change that 3em top margin on the cleared <p> to 1000px and you should see some margin, but not in Firefox (or Opera or Konqueror surprisingly, making me wonder if I have interpreted the specification wrong. But it seems quite clear). |
#6
| |||
| |||
|
|
What I really need is some bulletproof CSS that I can then let a whole bunch of HTML coders loose on. So I need stability, simplicity and cross-browser consistency. Am I best shooting for margins (on the inner) here, or padding (on the outer)? Demo example: http://codesmiths.com/dingbat/lj/use...tical_margins/ |
#7
| |||
| |||
|
|
When attempting to position "content", don't use margin, but use padding instead. |
![]() |
| Thread Tools | |
| Display Modes | |
| |