tags:

views:

1621

answers:

5

Is there an easy way to distribute inline elements within a parent block container using CSS? Setting margins to auto doesn't work since the margins between inline elements are set to 0 and I don't want to mess around with percentages as the content is dynamic.

In particular, I have several anchor elements (a) within a paragraph (p) that spans 80% of its container and I'm looking for an easy way to distribute them evenly within the paragraph.

EDIT (@cletus): The paragraph will not wrap and the anchors are the only thing in the paragraph. By distribute evenly, I mean that the space between the left (right) edge and first (last) element and the elements themselves is equidistant.

A: 

It's a bit unclear what you mean by "distributing evenly"; Could it be that you want to justify the contents of the paragraph?

p { text-align: justify; }
Brant Bobby
See edit on distributing evenly. Justifying doesn't work since the paragraph contents won't wrap and justification doesn't require the last line of a paragraph to be aligned on the right side.
gvkv
A: 

do your anchors have specific widths set? i think that might be necessary for any margin auto's to work.

+1  A: 

Hmmm, sounds to me like you're creating a menu? You might want to use a list to hold your anchors and style the list accordingly. This is the commonly accepted best practice.

As for even distribution of elements, I was looking yesterday for something similar, I was hoping it would be in the CSS3 spec, but it's not (at least I can't find it) which seems like a major f*ckup if you ask me. Anyway...

Which leaves two options. CSS, and Javascript.

With CSS, use the margin-right property for each element. It's a good idea to create a .last class that sets margin-right to zero which prevents your last element from breaking the layout.

There's a bunch of javascripts out there that will do this for you. I prefer only to use JS when absolutely essential, so I couldn't comment on which one is best.

... there is one last thing you could try, but... you didn't hear this from me ok?

You could use a table. That is the quickest (and dirtiest) way to get what you want.

IMHO, and you probably don't want to hear this, but the design is probably flawed. It's common knowledge that distributing items evenly across a layout with CSS is a pain, so designers should avoid it.

UPDATE: You could also try

.link_container { text-align: center; }
.link_container a { margin-right: 10x; }
.last { margin-right: 0; }

then use something like this

<div class='link-container'>
    <a href='...'>Some line</a>
    <a href='...'>Some line</a>
    <a href='...'>Some line</a>
    <a class='last' href='...'>Some line</a>
</div>

That might get you close.

gargantaun
The margin-right property won't work since I don't know the size of the anchors. Tables are something I'm trying to avoid but I have been very tempted to use them multiple times throughout my design.
gvkv
Also, a design is not flawed because a standard is incomplete or broken. Distributing elements is a common activity in layout and there really is no excuse for CSS to not have a simple mechanism for implementing it.
gvkv
I hear you buddy. I've wasted many an hour screaming at my screen because of the failings of CSS. However, CSS is the standard, my designs are not. Overtime, I've come to know what can and can not be done and I design to CSS's strengths. CSS wont be changing anytime soon, so my designs must.
gargantaun
Shouldn't `margin-right` be '10px' in the last snippet?
Kuroki Kaze
no, it's set to 0 so if if the "last" element reaches the far right of the parent elements container, the white space of the margin won't cause the element to wrap.
gargantaun
+2  A: 

Unfortunately, this is not possible with CSS. However, in the special case where your elements are of equal width, this CSS hack makes it fairly easy.

With equidistant spacing between variable-width elements even specifying widths in percentages for each element's container will not suffice. This would still create a variable width between the elements.

This is probably possible to do with JavaScript on most modern browsers. Here is an example page demonstrating a poorly implemented JavaScript hack and proof that attempting to use text justification to solve this problem will not work reliably.

Trey
Thanks for responding to an old question. Unfortunately, elements are not of equal widths. At any rate, I ended up using JavaScript for my layout issues as necessary.
gvkv
+1  A: 

If CSS3 is acceptable (ie, if how it looks in IE6 is not of primary importance), you can use the display property values of "table" and "table-cell" to use the table display model with any type of element; "inline-block" is also something to consider, which acts like a block without breaking a new line.

Anonymous
Thanks for responding on an old question. Actually, IE6 isn't the problem since I made an explicit decision to not support that browser but IE7 doesn't allow those tricks either and I will need to support that. I ended up using JavaScript.
gvkv