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

Detailed explanation of Vue advanced construction properties

Table of contents 1. Directive custom directive 2...

React-Native environment setup and basic introduction

Environment Preparation 1. Environment Constructi...

Detailed explanation of nginx configuration file interpretation

The nginx configuration file is mainly divided in...

Centos7 installation of MySQL8 tutorial

MySQL 8 new features: My personal opinion on MySQ...

Practical example of Vue virtual list

Table of contents Preface design accomplish summa...

MySQL DML language operation example

Additional explanation, foreign keys: Do not use ...

5 basic skills of topic page design (Alibaba UED Shanmu)

This topic is an internal sharing in the second h...

Win10 + Ubuntu 16.04 dual system perfect installation tutorial [detailed]

Be sure to remember to back up your data, it is p...

Docker deploys mysql to achieve remote connection sample code

1.docker search mysql查看mysql版本 2. docker pull mys...

About the correct way to convert time in js when importing excel

Table of contents 1. Basics 2. Problem Descriptio...

MySQL Interview Questions: How to Set Up Hash Indexes

In addition to B-Tree indexes, MySQL also provide...

How to decompress multiple files using the unzip command in Linux

Solution to the problem that there is no unzip co...