Line-breaks between divs render a space. How to eliminate it from HTML?

◇◆丶佛笑我妖孽 提交于 2019-12-04 23:53:27

One more possible way is to set the font-size of the parent to zero and then set the font-size of child elements to whatever you need. Like:

#mybar { font-size: 0; }
#mybar span { font-size: 14px; }

However, you have to be aware that font-size = 0 is displayed different depending on the browser: http://webdesign.about.com/od/fonts/qt/tipfont0.htm

I tested in Firefox and works as expected. If I remember correctly, the minimum font size can be also set depending on the browsers, so, it may not be consistent.

Matei Mihai

Possible duplicate on Removing whitespace between HTML elements when using line breaks

You can either use float:left css style or just comment the spaces between tags:

<div style="width:100px">
    <div style="width:50%; display: inline-block;">
        div1
    </div><!--
    --><div style="width:50%; display: inline-block;">
        div2
    </div>
</div>

Later edit: I think the css solution is to set font-size:0px; to the container div so:

<div style="width:100px; font-size:0px;">
    <div style="width:50%; display: inline-block; font-size:11px;">
        div1
    </div>
    <div style="width:50%; display: inline-block; font-size:11px;">
        div2
    </div>
</div>

should solve the problem.

Seen on CSS unwanted spacing between anchor-tag elements

I think the only actually good solution is to switch divs with list

<ul style="width:100px">
    <li style="width:50%; display: inline-block;">
        div1
    </li>
    <li style="width:50%; display: inline-block;">
        div2
    </li>
</ul>

typically 'reseting' the list properties will make it look like a div. The benefit is that browsers will not generate space between <li> items.

One way to fix it is to use "display: table-cell" instead of "display: inline-block".

Other way (that I sometimes do), is to remove that space controlling the margin:

#mybar span { display: inline-block; }
#mybar span:not(:first-child) {
    margin-left: -4px; /* Adjust accordingly */
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!