Automatic line breaks in html pre tags

Automatic line breaks in html pre tags
At this time, you can use overflow:auto; (when the code exceeds the container boundary, display the scroll box), but this method does not work for all mainstream browsers. Some browsers will directly cut off the excess content.

We all know that the <pre> tag can define preformatted text. A common application is to represent computer source code. Text enclosed in the pre element usually retains spaces and line breaks, but unfortunately, when you write code inside the <pre> tag, if you don't wrap it manually, it will also be preserved for you and will not automatically wrap.

At this time, you can use overflow:auto; (when the code exceeds the container boundary, display the scroll box), but this method does not work for all mainstream browsers. Some browsers will directly cut off the excess content.

pre wrap 解決<pre>標簽里的文本換行(兼容IE, FF和Opera等)

Since there are not many places in this website that use source code, and I didn’t pay much attention to this issue before, not long ago, an enthusiastic netizen reported this problem on QQ, so when I changed the theme this time, I looked for a solution and shared it.

Copy code
The code is as follows:

pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}

This CSS solution allows the text in the pre tag to wrap automatically. I have tested it on all the browsers I have, and all of them support it, including IE6, IE7, IE8, Firefox, Opera, Safari and Chrome.
The border will only go out of bounds when you shrink the window to a width of less than 20 characters. In addition, I saw some posts sharing this CSS technique, saying that it can solve the problem of long word wrapping, but I tried it and it didn't work.

<<:  Tutorial on using Webpack in JavaScript

>>:  Achieve 3D flip effect with pure CSS3 in a few simple steps

Recommend

A brief introduction to VUE uni-app basic components

1. scroll-view When using vertical scrolling, you...

Solution to MySQL server login error ERROR 1820 (HY000)

Fault site: Log in to the MySQL server and get th...

Nginx configuration file detailed explanation and optimization suggestions guide

Table of contents 1. Overview 2. nginx.conf 1) Co...

N ways to achieve two-column layout with CSS

1. What is a two-column layout? There are two typ...

How to deploy Vue project using Docker image + nginx

1. Packaging Vue project Enter the following name...

CSS multi-column layout solution

1. Fixed width + adaptive Expected effect: fixed ...

Installation process of MySQL5.7.22 on Mac

1. Use the installation package to install MySQL ...

How to solve nginx 503 Service Temporarily Unavailable

Recently, after refreshing the website, 503 Servi...

What is ssh port forwarding? What's the use?

Table of contents Preface 1. Local port forwardin...

A brief discussion on JS regular RegExp object

Table of contents 1. RegExp object 2. Grammar 2.1...

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...

Linux general java program startup script code example

Although the frequency of starting the shell is v...

Several ways to set the expiration time of localStorage

Table of contents Problem Description 1. Basic so...