how to stop inline-block whitespace being rendered in the browser

℡╲_俬逩灬. 提交于 2019-12-05 05:53:16

You provided nearly all possible solutions to this big layout question. I just want to point out my preferred solution.

Set font-size to the parent to 0 and resetting it again with REM's.

You'll have no trouble with your code and layout if there is no additional text inside the parent div (not the child divs).

REM's (Relative EM's) are not relative to the font-size of the parent elements (like normal EM's are), but relative to the root element of your document – the html element.

HTML:

<div class="parent">
    <div class="child">column 1</div>
    <div class="child">column 2</div>
    <div class="child">column 3</div>
    <div class="child">column 4</div>
</div>

CSS:

html {
    font-size: 1em;
}

.parent {
    font-size: 0;
}

.child {
    display: inline-block;
    font-size: 16px; /* Add pixel-based font-size to support IE8 and below */
    font-size: 1rem; /* Don't use rem along with the font-shorthand to avoid problems in IE9/10 - see note below */
    width: 25%;
}

No Browser support:

  • IE8 and below: Add pixel-based font-size to make it work.
  • IE9/10: not working with font-shorthand; use font-size instead!
  • (Opera Mini & iOS 3.2)

is there a ... way of preventing HTML whitespace from being rendered in the browser whilst using display:inline-block?

Yes, there are several ways. None of them really meet your criteria of 'hack-free' and 'tidy', but they do work.

  • Reformat ('minify') your code so that it doesn't have any white space between the elements.
    This is probably the most hack-free and cross-browser solution. It isn't necessarily tidy though, and it means you're fixing your layout by adjusting the HTML rather than the CSS, which isn't ideal. But it does work well. If you want to keep your code readable, you could use HTML comments so you can keep the gaps but without them being in the DOM:

       <div>block 1</div><!--
    --><div>block 2</div><!--
    --><div>block 3</div>
    

    Still not ideal, but more readable than a massive single line of code.

  • Set the font-size to zero for the container, and back to full size again for the blocks.
    This works really well. It's a pure CSS solution and easy to do. The down side is that it can be difficult to work with if you've got relative font sizes (ie setting back to 14px is fine, but setting to 1em won't work because 1em of the previous font size of zero is still zero).

  • Set a 1em negative margin to close the gap.
    This also works pretty well, but can be imprecise.

Or is there an alternative to inline-block that can be used that has no unpleasant side effects?

  • There's always float:left. But that's got a whole range of different issues of its own. If you're using inline-block, the odds are good it's because you don't want to use floats.

  • Use position:absolute and do the layout manually.

You can use the float method you described in your question, but you didn't clear your floats, which is why the container collapses.

A good method is to use an ::after pseudo element attache to the container element to "auto-clear" itself:

 div:after {
    content: "";
    display: table;
    clear: both;
}

http://jsfiddle.net/s2rJW/3/

When i saw your "workaround" i was thinking: Why don't you use a <table>?

And then i figured this out:

div {
  background: #ccc;
  display: table;
  width: 100%;
}
div div {
  background: #eee;
  display: table-cell;
  width: 25%
}
<div>
  <div>A column</div>
  <div>A column</div>
  <div>A column</div>
  <div>A column</div>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!