Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

html - How to evenly space many inline-block elements?

Is it possible to evenly space many elements in a div with changeable width.

Here's not working example. If we use text-align:center; elements will be centered, but margin:0 auto; is not working. I want to accomplish something like justify+center:

|..<elem>..<elem>..<elem>..<elem>..|       // for one container width
|..<elem>..<elem>..<elem>..|               // for smaller container width
|....<elem>....<elem>....|                 // even smaller container

Container will be user resizable. One picture is worth a 1000 words:

enter image description here

Container(red box) width:100%; So user can resize it (browser window, js, whatever).
<--> represent even spaces. In second row <--> are bigger because there is more room. I was able to fake it with:

text-align:center;
word-spacing:3em;    // but any fixed value looses proportion
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I recently read about a very clever technique to do exactly what you're asking.

In short, you just need to use text-align:justify; on the container element to achieve this, in conjunction with an extra invisible block at the end.

This works because inline-block elements are seen as being part of the text content, each being effectively a single word.

Using justify will spread out the words in your text so that they fill the entire width of the element with extra space between the words. For inline-block elements, this means that they are spaced out with even spaces between them.

I mentioned an extra invisible block at the end. This is required because normal text-align:justify won't justify the last line of text. For normal text, that's exactly what you want, but for aligning inline-block boxes, you want them all to be aligned.

The solution is to add an extra invisible but 100% width element to the end of your list of inline-block elements. This will become effectively the last line of text, and thus the justify technique will work for the rest of your blocks.

You can use the :after pseudo-selector to create the invisible element without needing to modify your markup.

Here's an updated version of your jsFiddle to demonstrate: http://jsfiddle.net/ULQwf/298/

And here's the original article that explains it in more detail: http://www.barrelny.com/blog/text-align-justify-and-rwd/

[EDIT] One final update after seeing the image you've added to the question. (I don't have a better answer, but some additional thoughts that might be useful).

Ideally what you need here is a :last-line selector. Then you could text-align:justify the main text and text-align:center the last line. That would do what you want.

Sadly, :last-line isn't a valid selector (:first-line is, but not :last-line), so that's the end of that idea.

A slightly more hopeful thought is text-align-last, which does exist as a feature. This could do exactly what you want:

text-align:justify;
text-align-last:center;

Perfect.

Except that it's non-standard and has very limited browser support.

You can read about here on MDN.

I guess as a last resort it might be an option for you, if you can live with only partial browser support. It would at least get what you want for some of your users. But that's not really a sensible way to go.

My gut feeling though is that this as as close as you're going to get. Tantalisingly close to what you want, but just not quite there. I hope I'm proved wrong, but I'll be surprised. Too bad though, because I it would seem like a perfectly logical thing to want to do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...