Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove collapsed whitespace between HTML elements?

I have a UL with LIs that will be displayed horizontally using display:inline-block. However, these collapse all whitespace (including newlines and tabs) to a single space between the elements, when I require they be perfectly flush for measurement purposes; I don't want my measurements thrown off by varying sizes of spaces among fonts.

Is there any way to remove whitespace from between these? I need this done without having control of the HTML; this will be for a small proof-of-concept framework that shows it can be made without abusing float

SSCCE: http://jsfiddle.net/nSsTP/

<ul>
    <li>Semantic Cell 1</li>
    <li>Semantic Cell 2</li>
    <li>Semantic Cell 3</li>
    <li>Pretty Cell 4</li><li>Pretty Cell 5</li><li>Pretty Cell 6</li>
</ul>
<style>
    ul>li{
        background: gray;
        display: inline-block;
    }
</style>
like image 852
Ky. Avatar asked Oct 21 '25 13:10

Ky.


2 Answers

You can use one more style tag for

ul    
    {      
    font-size: 0;
}

OR

ul>li
    {
         display: inline-block;    
        background: gray;
        float:left;
}

Hope this will really help.

like image 142
Kalpit S. Avatar answered Oct 24 '25 03:10

Kalpit S.


inline-block leaves white-space between elements.

To remove this space, write elements on same line.

Change

<ul>
    <li>Semantic Cell 1</li>
    <li>Semantic Cell 2</li>
    <li>Semantic Cell 3</li>
    <li>Pretty Cell 4</li><li>Pretty Cell 5</li><li>Pretty Cell 6</li>
</ul>

to

<ul>
    <li>Semantic Cell 1</li><li>Semantic Cell 2</li><li>Semantic Cell 3</li><li>Pretty Cell 4</li><li>Pretty Cell 5</li><li>Pretty Cell 6</li>
</ul>

Demo here.

OR:

Pure css solution:

ul{font-size:0;}
li{font-size:1rem;}

Demo here.

like image 45
codingrose Avatar answered Oct 24 '25 04:10

codingrose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!