可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答1:
Oh, you can really easy accomplish that with a single line of CSS:
#parent_of_imgs { white-space-collapse: discard; }
Disadvantage, you ask? No browser has implemented this extremely useful feature (think of inline blocks in general) yet. :-(
What I did from time to time, although it's ugly as the night is dark, is to use comments:
<p><!-- --><img src="." alt="" /><!-- --><img src="." alt="" /><!-- --><img src="." alt="" /><!-- --><img src="." alt="" /><!-- --></p>
回答2:
you can set the font-size
of the container to 0
and the white-space
disappears!
回答3:
The browsers does ignore whitespace in most cases when it's next to a block element.
The problem with images (in this case) is that they are inline elements, so while you write them on separate lines, they are actually elements on the same line with a space between them (as the line break counts as a space). It would be incorrect for the browser to remove the spaces between the images, writing the image tags with line breaks between them should be handled the same way as writing the image tags on the same line with spaces between them.
You can use CSS to make the images block elements and float them next to each other, that solves a lot of problems with spacing, both the space between the images and the spacing on the text line below images.
回答4:
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:
<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" />
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 :)
回答5:
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:
<!-- Disregard spaces between inline-block elements? --> <div class="box"> <span></span> <span></span> <span></span> </div>
CSS
.box { display: flex; display: -webkit-flex; } span { display: inline-block; width: 30px; height: 30px; background-color: #fcf; border: 2px solid #f9f; }
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.
回答6:
<style> img { display: table-cell } </style> <img src="images/minithing.jpg" alt="my mini thing" />...
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)
回答7:
What was so difficult about this solution?
css
.no-whitespace { line-height: 0; font-size: 0; }
html
<span class="no-whitespace"> <a href=# title='image 1'> <img src='/img/img1.jpg' alt='img1'/> </a> <a href=# title='image 2'> <img src='/img/img2.jpg' alt='img2'/> </a> <a href=# title='image 3'> <img src='/img/img3.jpg' alt='img3'/> </a> </span>
回答8:
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:
my mini thingmy mini thingmy mini thingmy mini thing
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.
回答9:
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.
<?ob_start(function($html) {return preg_replace('/>\s+</','><',$html);});?> <img src='./mlp.png'/> <img src='./dbz.png'/> <img src='./b10af.png'/> <img src='./fma.png'/> <?ob_end_flush;?>
回答10:
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
elem.inline { display: -moz-inline-box; /* FF2 */ display: inline-block; /* gives hasLayout in IE 6+7*/ } /* * html hack for IE 6 */ * html elem.inline { display: inline; /* elements with hasLayout and display inline behave like inline-block */ } /* * + html hack for IE 7 */ * + html elem.inline { display: inline; /* elements with hasLayout and display inline behave like inline-block */ }
回答11:
Try this CSS:
img { display: table-cell; }
回答12:
Minify your HTML!
It is good practice to minify the response before it is rendered to the browser.
So unless you need the space (and you hard coded it using
), you always remove the spaces in the minification process.