Home > front end >  C# operator \n for string in HTML not working
C# operator \n for string in HTML not working

Time:10-13

Website on Django 3.2, Visual Studio 2019. It is necessary to transfer the text of the link description - a text string in the HTML code. There are special HTML codes &#10; &#13; for line feed and carriage return. Their use does not give the desired result. <br> - is working: <a href="link">Text3.<br>Text4.</a>. But why HTML special codes not working?

{% extends "/layout.html" %}<!--♕-->{% block content %}<div class="Jambotron">
<h1>Welcome!</h1>
<p class="lead">Text1!&#10;&#13;Text2.</p>
<a href="link">Text3.&#10;&#13;Text4.</a>
</div>{% endblock %}

Example: special codes HTML did not work out as intended

Example: special codes did not work out as intended

CodePudding user response:

In HTML, multiple whitespace characters are collapsed to a single space by default. This is a great "feature", since it allows you to write your markup with readable indentation etc.

However, you can control this behaviour with the CSS property white-space (https://developer.mozilla.org/en-US/docs/Web/CSS/white-space).

To preserve your special whitespace characters you can use white-space: pre;

p {
  white-space: pre;
}
<p class="lead">Text1!&#10;&#13;Text2.</p>

  • Related