wtf-html-css icon indicating copy to clipboard operation
wtf-html-css copied to clipboard

inline-block spacing

Open mdo opened this issue 11 years ago • 12 comments

Mention blank spaces and how to deal with those.

mdo avatar Mar 24 '14 07:03 mdo

Exactly! Wanted to mention that as well.

Yahoo!'s Pure CSS framework uses letter-spacing: -0.31em to collapse the whitespace in their Grid component, but suffice to say this arbitrary value breaks in a variety of situations (e.g. using web fonts). http://purecss.io/grids/

My new favorite technique for dealing with this outlined here: http://scottkellum.com/2013/10/25/the-new-kellum-method.html

Basically use web font with zero-width glyphs on the wrapper to annihilate the whitespace -- the downside being that you need to reset the font-family on the cells themselves.

danburzo avatar Mar 24 '14 09:03 danburzo

You can also specify font-size: 0 in the parent element and redefine it for the children. Seems a bit less hacky than negative letter-spacing to me.

sugarenia avatar Mar 24 '14 10:03 sugarenia

I think (although I haven't tested) font-size: 0 is still broken on Android:
http://codepen.io/stowball/details/LsICH

A suggested workaround is to use a very small value -- e.g. 0.001px http://codepen.io/SelenIT/pen/HIika

danburzo avatar Mar 24 '14 10:03 danburzo

Good to know, Dan, thanks!

sugarenia avatar Mar 24 '14 10:03 sugarenia

Always bugs me that the best inline-block space removal method (which works right back to IE6) is never listed. It involves the use of word-spacing: http://codepen.io/pageaffairs/pen/BfIun

pageaffairs avatar Apr 01 '14 23:04 pageaffairs

Isn't the display: table that is required for the parent a bit problematic though? You may not always want that.

sugarenia avatar Apr 02 '14 05:04 sugarenia

@pageaffairs you'll also want to add a width: 100% to the parent in most cases.

danburzo avatar Apr 02 '14 15:04 danburzo

Actually, the display: table-cell fix is a good workaround, however, elements will never break to another line in case you want a grid-like layout to adapt with viewport with. Obviously as it happens with tables, it will always try to fit its children inside the given row.

So as long as this is not major concern for your layout, it would be indeed a great idea to use it.

PS. I do like the fact you get all elements with the same height and vertical-align still works while using the “table-cell” workaround.

zanona avatar Apr 02 '14 15:04 zanona

Another issue might be worth mention is the lack of control over width and margin combinations while using the “table-cell” method. In fact, I haven’t been able to add margins at all while using this method.

zanona avatar Apr 02 '14 15:04 zanona

@danburzo Normally, a block level parent will have width: 100% by default.

@zanona The example I posted doesn't have a problem with wrapping, though. See here: http://codepen.io/pageaffairs/pen/zclie/ The display: table on the parent just helps to jog webkit-based browsers (and still Chrome, it seems) without creating any other issue, as far as I can see.

This method doesn't require any display: table-cell, so issues with that don't arise. You can place as much margin/padding on the elements you like with display: inline-block: http://codepen.io/pageaffairs/pen/BhnHp/

pageaffairs avatar Apr 02 '14 22:04 pageaffairs

@pageaffairs wow that’s brilliant. Now I got confused on how I did come with the table-cell idea. Thanks for the heads-up.

zanona avatar Apr 03 '14 01:04 zanona

@pageaffairs your current example does have a block level parent, but the display:table property affects how it computes its layout. If that was the case by default, the section in your example would stretch the entire width.

danburzo avatar Apr 03 '14 14:04 danburzo