What is that space?

This question already has an answer here:

  • How do I remove the space between inline-block elements? 32 answers

  • The space is in the HTML. There are several possible solutions. From best to worst:

  • Remove the actual space in the HTML (ideally your server could do this for you when the file is served, or at least your input template could be spaced appropriately) http://jsfiddle.net/AWMMT/2/
  • Use float: left instead of display: inline-block , but this has undesirable effects on t he height: http://jsfiddle.net/AWMMT/3/
  • Set the container's font-size to 0 and set an appropriate font-size for the internal elements: http://jsfiddle.net/AWMMT/4/ -- this is pretty simple, but then you can't take advantage of relative font size rules on the internal elements (percentages, em)

  • http://jsfiddle.net/AWMMT/1/

    <div>...</div><div>...</div>
                  ^
                  |--- no whitespace/new line here.
    

    Your spaces were the new lines the browser converted to "spaces" when displaying it.

    Or you could try to hack a bit with CSS:

    http://jsfiddle.net/AWMMT/6/

    body { white-space: -0.125em; }
    body > * { white-space: 0; /* reset to default */ }
    

    There's actually a really simple way to remove whitespace from inline-block that's both easy and semantic. It's called a custom font with zero-width spaces, which allows you to collapse the whitespace (added by the browser for inline elements when they're on separate lines) at the font level using a very tiny font. Once you declare the font, you just change the font-family on the container and back again on the children, and voila. Like this:

    @font-face{ 
        font-family: 'NoSpace';
        src: url('../Fonts/zerowidthspaces.eot');
        src: url('../Fonts/zerowidthspaces.eot?#iefix') format('embedded-opentype'),
             url('../Fonts/zerowidthspaces.woff') format('woff'),
             url('../Fonts/zerowidthspaces.ttf') format('truetype'),
             url('../Fonts/zerowidthspaces.svg#NoSpace') format('svg');
    }
    
    body {
        font-face: 'OpenSans', sans-serif;
    }
    
    .inline-container {
        font-face: 'NoSpace';
    }
    
    .inline-container > * {
        display: inline-block;
        font-face: 'OpenSans', sans-serif;
    }
    

    Suit to taste. Here's a download to the font I just cooked up in font-forge and converted with FontSquirrel webfont generator. Took me all of 5 minutes. The css @font-face declaration is included: zipped zero-width space font. It's in Google Drive so you'll need to click File > Download to save it to your computer. You'll probably need to change the font paths as well if you copy the declaration to your main css file.

    链接地址: http://www.djcxy.com/p/15778.html

    上一篇: 如何写一个:悬停在内联CSS?

    下一篇: 那是什么空间?