简体   繁体   English

有没有办法使换行符(\n)的高度小于文本的行高?

[英]Is there a way to make the height of new line characters (\n) be less than the line-height of the text?

I'm building a reader to display text and the text comes with \n\n characters within it, and when rendered in HTML, it makes the empty lines' heights bigger than I want.我正在构建一个阅读器来显示文本,并且文本中带有\n\n字符,当在 HTML 中呈现时,它使空行的高度比我想要的要大。 Is there a way to control the height of the empty line produced in Html, without replacing them with Html tags?有没有办法控制 Html 中产生的空行的高度,而不用 Html 标签替换它们?

在此处查看示例。我想减少那个空间的高度,这是由文本中的 \n\n 字符引入的

Thanks in advance.提前致谢。

You can do it by replacing empty lines with an empty HTML element:您可以通过用空的 HTML 元素替换空行来做到这一点:

 const text = `this is an example of multiline text. spacing between these lines is consistent, but line below will be different even multiple empty lines: will have the same height`; const div = document.getElementById("main"); div.textContent = text; div.innerHTML = div.innerHTML.replace(/([\n]{2,})/g, "<p>$1</p>"); console.log("original text length: ", text.length); console.log("text from html length: ", div.textContent.length); console.log("are both texts equal? ", div.textContent === text);
 div { white-space: pre; line-height: 2em; } p { height: 2em; margin: 0; padding: 0; border: none; line-height: 0; visibility: hidden; white-space: none; }
 <div id="main"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM