Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 9th, 2006, 08:05 PM
Jeremy
Guest
 
Posts: n/a
Default Horizontal margin collapsing

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 | |
| | | | | |
| | | | | |
| | | | | |
| |_______________________| |_______________________| |
| |
|_________________________________________________ _______|
  #2  
Old August 9th, 2006, 08:25 PM
Jeremy
Guest
 
Posts: n/a
Default Re: Horizontal margin collapsing

Jeremy wrote:
Quote:
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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles