What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A carriage return or line feed in an ASP string does not automatically create a visible line break in an HTML page: the browser normally collapses ordinary whitespace. For readable multiline text, keep the content HTML-encoded and apply white-space: pre-wrap, or encode it first and then convert normalized newlines to trusted <br> elements.

First identify which ASP you are using

  • Classic ASP: usually a .asp page with VBScript between <% ... %> and output such as Response.Write.
  • ASP.NET Web Forms: usually an .aspx page with server controls and runat="server".
  • Razor: typically a .cshtml or .vbhtml page with @ expressions.

The examples below distinguish these technologies; their syntax and encoding helpers are not interchangeable. Classic ASP combines server-side script with HTML returned to the browser, and Response.Write writes text to the HTTP output (Microsoft’s Classic ASP overview; Response.Write reference).

Recommended for ordinary text: preserve newlines with CSS

For comments, form submissions, descriptions, and other text, leave line breaks in the text and tell the browser how to render whitespace. This avoids constructing HTML from the content.

<div class="multiline"><%=Server.HTMLEncode(value)%></div>

<style>
.multiline {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}
</style>

Server.HTMLEncode makes markup characters in the value display as text rather than executable HTML. pre-wrap preserves newlines and runs of spaces while allowing long lines to wrap. The optional overflow-wrap helps with long unbroken strings. CSS controls the rendering; this is not an ASP-specific feature. See the MDN reference for white-space.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Classic ASP: convert newlines to HTML breaks

Use this when the template requires explicit line-break elements. Normalize the line endings first, HTML-encode the content, and only then add trusted <br /> markup:

<%
Function HtmlWithBreaks(ByVal value)
    Dim text

    If IsNull(value) Then
        HtmlWithBreaks = ""
        Exit Function
    End If

    text = CStr(value)
    text = Replace(text, vbCrLf, vbLf)
    text = Replace(text, vbCr, vbLf)
    text = Server.HTMLEncode(text)
    text = Replace(text, vbLf, "<br />")

    HtmlWithBreaks = text
End Function

Response.Write HtmlWithBreaks(rs("description"))
%>

For data known to contain CRLF only, the shorter form is:

<%=Replace(Server.HTMLEncode(rs("description")), vbCrLf, "<br />")%>

That shortcut will not replace LF-only or CR-only endings. Do not insert breaks into untrusted text and then encode the whole result: encoding afterward turns the trusted tags into visible text. Conversely, writing unencoded form or database content can allow injected markup or script-capable HTML. Microsoft advises encoding client-supplied data before writing it to an HTML response (HttpResponse.Write and output encoding).

Choose the rendering method that fits the content

Need Use
Ordinary user-entered prose with newlines HTML-encoded text plus white-space: pre-wrap
Explicit breaks in an existing HTML layout Encode text, normalize line endings, then replace LF with trusted <br />
Preserve line breaks but collapse repeated spaces and tabs white-space: pre-line
Code, logs, or data where spacing matters Encoded text in <pre>, or pre-wrap if long lines should wrap
A static break authored by the developer A literal <br /> in the HTML

white-space: pre preserves whitespace but disables ordinary wrapping, so long lines can overflow. A <pre> element also preserves whitespace and is often styled in a monospace font; it suits code and logs better than normal prose. Regardless of element or CSS, encode untrusted text. The distinctions among these CSS values are documented by MDN.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What CR, LF, and CRLF mean

Form VBScript representation Meaning
CR vbCr or Chr(13) Carriage return
LF vbLf or Chr(10) Line feed
CRLF vbCrLf CR followed by LF; common in Windows text

Imported text can use CRLF, LF alone, or CR alone. Replacing only vbCrLf works only when that exact sequence is present. The robust conversion above maps CRLF and lone CR to LF, then replaces every LF with a break.

vbCrLf is a string sequence, not an instruction to the browser to draw a line break. For example, Response.Write "One" & vbCrLf & "Two" may put a newline in the generated HTML source, but ordinary HTML whitespace rules do not guarantee a visual break. Response.Write "One<br />Two" emits HTML that does. A newline in page source and a rendered line break are different things; Classic ASP documentation shows that Response.Write can emit HTML such as <BR> (Classic ASP page and output examples).

ASP.NET Web Forms and Razor equivalents

Web Forms

For plain text, use an encoding-capable literal and apply the same CSS approach. For example:

<asp:Literal ID="DescriptionLiteral" runat="server" Mode="Encode" />

Place the control in an element with the multiline class, and assign its text value in server-side code. If converting newlines to breaks instead, encode the value before adding only the trusted <br /> tags.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Razor

Razor normally HTML-encodes expressions. That is desirable for text, so a CSS rule such as white-space: pre-wrap is usually the simpler option. If you deliberately turn newlines into HTML breaks, account for Razor’s encoding behavior: ordinary encoded output will display the tags as text, while output treated as markup requires a controlled, safe conversion. Do not mark arbitrary user input as raw HTML. Microsoft explains the distinction between encoded expressions and intentionally emitted markup in its Razor syntax guide; Web Pages also documents Server.HtmlEncode.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot when the break still does not appear

  1. Check the output context. A normal <div> collapses whitespace by default; a textarea, preformatted element, plain-text response, and HTML email have different display rules.
  2. Check whether the string contains actual control characters. The literal text rn is four visible characters, not a CRLF sequence.
  3. Check which newline is present. A replacement for vbCrLf will not match lone LF or CR.
  4. Check the CSS. A rule setting white-space: normal on the element overrides the preservation you expected. Try pre-wrap.
  5. Check encoding order. If you encode after inserting <br />, the browser will display the tag literally.
  6. Check the data path. A database driver, import step, or sanitization routine may have removed the breaks before the page receives the value.
  7. Distinguish source from display. A newline visible in View Source does not prove the browser will render a new line.

To inspect actual CR and LF characters temporarily in Classic ASP:

<%
Dim debugText
debugText = Replace(text, vbCr, "[CR]")
debugText = Replace(debugText, vbLf, "[LF]")
Response.Write Server.HTMLEncode(debugText)
%>

If the markers are absent, the value does not contain those control characters at that point in the code. If the value contains literal backslash sequences and the input format explicitly defines them as escapes, convert them before normal newline handling:

value = Replace(value, "rn", vbLf)
value = Replace(value, "n", vbLf)
value = Replace(value, "r", vbLf)

Only do this for a format that uses literal escape sequences; replacing every literal n can alter legitimate text.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Textarea, blank lines, and other output contexts

A <textarea> preserves multiline editing input. When that value is later rendered into an ordinary <div>, use encoded text with white-space: pre-wrap or the normalized break-conversion function; the textarea’s behavior does not carry over to the div.

Converting consecutive newlines produces consecutive breaks and therefore visible blank lines. Decide whether those lines mean extra spacing, a paragraph boundary, or should be suppressed. If a value ends in CR or LF, conversion can also create an empty final line; remove only the terminal newline if that is not meaningful, rather than trimming all whitespace and potentially deleting indentation.

If stored content already contains <br />, encoding it will display the tags literally. That is normally the safe behavior for untrusted text. Legacy content intentionally stored as HTML needs a deliberate HTML-sanitization policy; newline conversion alone does not make it safe. In a plain-text HTTP response or plain-text email, keep text newlines rather than inserting HTML tags. In an HTML email, use the same encode-then-break or CSS strategy appropriate to the renderer. JavaScript and JSON strings have their own escaping rules and should not be rendered as HTML without context-appropriate encoding.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.