This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Stylesheets > August 2006 > Horizontal margin collapsing





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Horizontal margin collapsing
Jeremy

2006-08-09, 7:14 pm

Let's say I have some block elements that self-organize by floating to
the left, so if no more blocks can fit in a row they begin on the next
row (see crummy ASCII art at end of message).

I could define the CSS for one of the floating elements as such:

..floater
{
float: left;
width: 48%;
margin-left: 1%;
margin-right: 1%;
}

The problem here is that the floaters will then have twice as much
margin in between them as they will between the floaters and the sides
of the container. Now, in CSS2 (and 3?), vertical margins can collapse
(so that the margins of two elements are overlapping) but horizontal
margins are unable to do so. Is this correct? And can anyone think of
an elegant way to simulate this - and get equal spacing between the
floaters and the edges, and the floaters and each other?

I could do something like this:

..floater:nth-child(2n) { margin-left: 1%; margin-right: 0.5%; }
..floater:nth-child(2n+1) { margin-left: 0.5%; margin-right: 1%; }

except that (a) that pseudoclass is not (AFAIK) widely supported, and
(b) what if I have more than two elements per row?

That turned out to be far longer-winded than I expected; I hope someone
read all the way through :-)

Thanks,
Jeremy

Crummy ASCII Art Diagram:

________________________________________________________
|Container |
| _______________________ _______________________ |
| | Floater #1 | | Floater #2 | |
| | | | | |
| | | | | |
| | | | | |
| |_______________________| |_______________________| |
| _______________________ _______________________ |
| | Floater #3 | | Floater #4 | |
| | | | | |
| | | | | |
| | | | | |
| |_______________________| |_______________________| |
| |
|________________________________________________________|
Jeremy

2006-08-09, 7:14 pm

Jeremy wrote:
> Let's say I have some block elements that self-organize by floating to
> the left, so if no more blocks can fit in a row they begin on the next
> row (see crummy ASCII art at end of message).
> <snip>


Please disregard this message on the grounds that I'm an idiot.

Solution: floaters have equal margin on both sides. *Container* gets
left and right padding equal to this margin.

Duh.

Jeremy
Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews