开发者

How can I auto remove the white space?

I am processing various RSS news items.

In those items, so开发者_开发问答me of them always have various white spaces (like tabs, redundant space, etc) in front within <p></p> or <div></div>.

How can I automatically remove those unnecessary white spaces in the beginning of a paragraph, using pure HTML or CSS?


Not possible. HTML nor CSS are not programming languages, as such, they do not contain the tools to preform string manipulation.

You could do it easily with javascript like so:

var str = "This is a      string with           multiple spaces";
str = str.replace(/ {2,}/g, " ");
document.write("<pre>" + str + "</pre>");

Will print

This is a string with multiple spaces

Consider this working example


you can use CSS for doing this:

div
{
  white-space:nowrap;
}

or

p
{
  white-space:nowrap;
}

Sequences of whitespace will collapse into a single whitespace. Text will never wrap to the next line. The text continues on the same line until a
tag is encountered

Hope this helps.


If using jQuery, there's the jQuery.trim() function

JavaScript doesn't provide a trim function but here's an example how to write it yourself.


Unfortunately, you cannot process HTML element with HTML or CSS. You need a JS for this.


I had same issue I solved it using "white space attribute"

example :

p{
    white-space: normal;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜