Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

white-space: pre-line adds unusal space at top

Tags:

html

css

I've a below piece of css declaration which adds line spacing given in the below screenshot.

.colGroup{
    white-space: pre-line;
}

I've tried adding the below attributes but with no luck.

display: inline-block;
vertical-align: top;
height: 4.75em;
line-height: 1.75em;

EDIT (added part of the repeating <td> tag where I'm facing this issue).

<td width="15%">
    <span class="colGroup">
       <div>
            <p id="c2308view">
                t = 24 
                t1 = 27 
                t2 = 27 
                t3 = 36
                </p> 
            </div>
        </span>
        <span class="dateColumn">
            <div>
                <p id="c2312view"><span>31-Dec-2010</span></p>
            </div>
        </span>
</td>

Can someone help?

Screenshot

like image 634
David R Avatar asked Nov 20 '18 13:11

David R


People also ask

What does white space pre wrap do?

If we want to force the browser to display those line breaks and extra white space characters we can use white-space: pre; It's called pre because the behavior is that as if you had wrapped the text in <pre></pre> tags (which by default handle white space and line breaks that way).

How do I get rid of extra padding in CSS?

apply float:left and that will remove the space so the text doesn't have to be on 1 line.

What is the difference between the pre and pre line options?

"pre" preserves the text as it is, regardless of spaces and if the text fits in the page (it will create a scroll bar) "pre-line" leaves only a single white space if more than one have been written and sends the text to a new line if it doesn't fit in the page.


2 Answers

I just recently had to deal with the way various white-space values are handled within css. The documentation on MDN for white-space is very helpful. See this, and the entire page: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space#Values

What does it mean when you use the following css?

white-space: pre-line;

This will preserve line-breaks in your code, while collapsing white space. Line-breaks can be explict <br/> elements or newline characters.

Look again at your code. Notice, while you can't see character, you return three times before getting to the values within the <p> element. There is a hidden newline character being read by the DOM.

<span class="colGroup"> /* newline character 1 */
   <div> /* newline character 2 */
        <p id="c2308view"> /* newline character 3 */
            t = 24 /* newline character 4, which I assume you want to preserve */

If you remove these returns within your code, while the syntax is not all that readable, it will honor the white-space value you are desiring.

See the snippet below:

.colGroup {
    white-space: pre-line;
}
<td width="15%">
    <span class="colGroup"><div><p id="c2308view">t = 24 
t1 = 27 
t2 = 27 
t3 = 36</p></div></span>
    <span class="dateColumn">
        <div>
            <p id="c2312view">
                <span>31-Dec-2010</span>
            </p>
        </div>
    </span>
</td>
like image 101
wlh Avatar answered Oct 17 '22 21:10

wlh


try to add a <span> inside of the <p>, this did the trick for me.

For example with Tailwind.css and Vue.js:

<p class="whitespace-pre-line">
   <span>{{ user.info }}</span>
</p>
like image 30
Peter Avatar answered Oct 17 '22 20:10

Peter