This question already has an answer here:
Is there anything in HTML/CSS that tells the browser to ignore whitespace completely?
So many times when you want to put, say, two images next to each other - you try desperately to keep the HTML readable, but the browser puts a space between them.
So instead of something like this:
<img src="images/minithing.jpg" alt="my mini thing" />
<img src="images/minithing.jpg" alt="my mini thing" />
<img src="images/minithing.jpg" alt="my mini thing" />
<img src="images/minithing.jpg" alt="my mini thing" />
you end up with this
<img src="images/minithing.jpg" alt="my mini thing" /><img src="images/minithing.jpg" alt="my mini thing" /><img src="images/minithing.jpg" alt="my mini thing" /><img src="images/minithing.jpg" alt="my mini thing" />
Which is just so horrible!
OK, I might be lucky in that I only have to worry right now about getting this to work in webkit (specifically the one embedded in QT) so it works in Chrome and Safari too. Seems
display: table-cell
has all the benefits of display:inline-block
, but without the whitespace drawback (think indented td's)If you're using php, this works wonderfully. I found the solution here.
I was originally looking for something to remove text nodes consisting of only
whitespace
after parsing html with something like simplexml, but this is much cheaper.Images are per default inline elements, that’s why you see whitespace between them. If you listen to your example in a screen reader, you immediately know why: without whitespace, you’d hear:
So, use
my mini thing.
(dot plus whitespace at the end) as alt text or push the images with CSS together. Do not just remove the whitespace in the code.Unfortunately, newlines count as space characters.
The best solution I have come up with is to use the whitespace inside the tags themselves, instead of outside:
It's not ideal, either, I know. But at least it's not some bizarre CSS hack that relies on the size a space character is rendered or resorting to relative positioning, or JavaScript :)
This is a simple question and the answer is not so simple.
One can try to avoid the spaces in the source code which is not always achievable in CMS, because there they are automatically inserted by the system. If you want to change this you have to dig deep inte the CMS's core code.
Then you can try to use left floated images. But this is dangerous. At first you don't really have a control over vertical-alignment by definition. And secondly, you run into total chaos if you have so many floated elements, that they stretch over more than one line. And if you have a layout that relies on left floated elements (most of them do so today) you can even break some outer floating styles, if you clear floating after the images. This can be overridden, if you float any surrounding element. Rather not to be recommended.
So the only solution would be a CSS declaration that handles the process of whitespace handling. This is not part of any standard (as CSS 3 is not yet finished).
I prefer the no whitespace in HTML variant. With using drupal as CMS this can be achieved rather easy in your template.php and theming files. Then I choose inline-block.
P.S.: inline-block is rather complicated to get in the different browsers. For FF 2 you need display: -moz-inline-box. The rest and IE8 can have display: inline-block right after. And for lte IE 7 you need display: inline in a following separate declaration (preferrably via conditional comments).
edit
What I use for making a element inline-block
The newest solution for this is using
display: flex
on outside container, you can try this with following example:Codepen →
(And yes, Flexbox is becoming widely supported: http://caniuse.com/#search=flexbox)
HTML:
CSS
Update: Also, if you want your items to wrap (as standard inline-block elements do), don't forget to add
flex-wrap: wrap
to your flexbox container.