Home Web Front-end CSS Tutorial How css solves the problem of text compatibility under different browsers

How css solves the problem of text compatibility under different browsers

Apr 03, 2020 am 09:01 AM
css Browser

How css solves the problem of text compatibility under different browsers

Goal:

css enables compatible text alignment under different browsers.

In the front-end layout of the form, we often need to align the prompt text of the text box at both ends, for example:

How css solves the problem of text compatibility under different browsers

Solution process:

1. The first thing that comes to mind is whether the problem can be solved directly with css

css

1

2

3

.test-justify {

    text-align: justify;

}

Copy after login
Copy after login

html

1

2

3

<div class="test-justify">

       测试文本

</div>

Copy after login

How css solves the problem of text compatibility under different browsers

Okay, text -align:justify was completely ineffective, and I was unwilling to accept it, so I tested it with a piece of text. The effect is as follows:

How css solves the problem of text compatibility under different browsers

(Recommended tutorial: CSS tutorial)

It turns out that this attribute is for aligning both ends of the paragraph text, then try text-align-last: justify this attribute

css

1

2

3

.test-justify {

    text-align: justify;

}

Copy after login
Copy after login

How css solves the problem of text compatibility under different browsers

The effect is achieved, but the disadvantage is that it is completely incompatible with IE and Safari browsers.

2. Then think about it, since the above implementation has compatibility issues, can you write separate css classes for 2, 3, 4, etc. texts of such length, because the text box prompt text of the form is also Not a lot.

css

1

2

3

4

5

6

7

8

9

div {

    width: 100px;

}

.w2 {

    letter-spacing: 2em;

}

.w3 {

    letter-spacing: 0.5em;

}

Copy after login

html

1

2

3

<div class="w2">测试</div>

<div class="w3">测试了</div>

<div>测试来了</div>

Copy after login

How css solves the problem of text compatibility under different browsers

This solution seems to be able to solve the problem and should be fine for most scenarios, but Unfortunately, it is not really aligned at both ends, and it still cannot meet the needs in special display cases. We will leave it alone and continue to try.

2. The above is a pure css implementation. Next, let’s see if the combination of css and dom can create a unified solution.

html

1

2

3

4

<div class="test-justify">

    测 试 文 本

    <span></span>

</div>

Copy after login

css

1

2

3

4

5

6

7

.test-justify {

    text-align: justify;

}

span {

    display:inline-block;

    padding-left:100%;

}

Copy after login

How css solves the problem of text compatibility under different browsers

Think about it, it’s a little exciting, and it’s perfectly compatible with IE and Safari. This solution In fact, it is an extension of the first paragraph alignment scheme, using spaces to force word segmentation, and then using span to fake the last line (test-justify will not align the last line).

In order to increase scalability, we have to optimize this solution, because in most cases the text is loaded on the backend.

For example: How to write .net core razor view loading model displayname

1

<label asp-for="Email"></label>

Copy after login

Just add a small piece of js and it should be compatible with all scenarios.
css

1

2

3

4

5

6

7

8

9

10

11

div {

    width: 300px;

    border: 1px solid #000;

}

.test-justify {

    text-align: justify;

}

span {

    display:inline-block;

    padding-left:100%;

}

Copy after login

html

1

2

3

<div class="test-justify">

    测试文本

</div>

Copy after login

js

1

2

3

4

5

6

7

8

var $this = $(".test-justify")

, justifyText = $this.text().trim()

, afterText = "";

for (var i = 0; i < justifyText.length; i++) {

    afterText += justifyText[i] + " ";

}

afterText = afterText.trim() + "<span></span>";

$this.html(afterText).css({ "height": $this.height() / 2 + "px" });

Copy after login

How css solves the problem of text compatibility under different browsers

Okay, this solution should work It supports mainstream browsers, but the disadvantage is that it is re-adjusted through js, so when you refresh it, you will see the process of aligning both ends of the text (flash). The experience is not very good, so make it compatible.

Only IE and Safari do not support text-align-last: justify so call the last solution only considering these two browsers

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

function myBrowser() {

    var userAgent = navigator.userAgent;

  

    //判断浏览器版本 

    var isOpera = userAgent.indexOf("Opera") > -1;

    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera;

    var isEdge = userAgent.toLowerCase().indexOf("edge") > -1 && !isIE;

    var isIE11 = (userAgent.toLowerCase().indexOf("trident") > -1 && userAgent.indexOf("rv") > -1);

  

    if (/[Ff]irefox(\/\d+\.\d+)/.test(userAgent)) {

        return "Firefox";

    } else if (isIE) {

        return "IE";

    } else if (isEdge) {

        return "IE";

    } else if (isIE11) {

        return "IE";

    } else if (/[Cc]hrome\/\d+/.test(userAgent)) {

        return "Chrome";

    } else if (/[Vv]ersion\/\d+\.\d+\.\d+(\.\d)* *[Ss]afari/.test(userAgent)) {

        return "Safari"

    } else {

        return "unknown"

    }

}

  

var browser = myBrowser();

if (browser == "IE" || browser == "Safari") {

    var $this = $(".test-justify")

        , justifyText = $this.text().trim()

        , afterText = "";

    for (var i = 0; i < justifyText.length; i++) {

        afterText += justifyText[i] + " ";

    }

    afterText = afterText.trim() + "<span></span>";

    $this.html(afterText).css({ "height": $this.height() / 2 + "px" })

}

Copy after login

Done!

Recommended video tutorial: css video tutorial

The above is the detailed content of How css solves the problem of text compatibility under different browsers. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

gate exchange official website entrance gate exchange latest official website entrance gate exchange latest official website entrance gate exchange official website entrance gate exchange latest official website entrance gate exchange latest official website entrance Mar 25, 2025 pm 04:12 PM

The latest official website portal of Gate.io can be obtained through official announcement channels, authoritative information websites and official download channels to ensure safe access. To identify authentic websites, you need to verify the domain name, check the SSL certificate, reject unknown links, verify official announcements and be alert to abnormal prompts. To protect the security of your account, you need to enable two-factor verification, set high-strength passwords, keep keys and mnemonics, and regularly check transaction records.

How to solve the problem of third-party interface returning 403 in Node.js environment? How to solve the problem of third-party interface returning 403 in Node.js environment? Mar 31, 2025 pm 11:27 PM

Solve the problem of third-party interface returning 403 in Node.js environment. When we use Node.js to call third-party interfaces, we sometimes encounter an error of 403 from the interface returning 403...

Where to download the genuine Ouyi? Official website download. Global Where to download the genuine Ouyi? Official website download. Global Mar 31, 2025 pm 02:09 PM

Ouyi usually refers to Ouyi OKX. The global way to download Ouyi OKX APP is as follows: 1. Android device: Download the APK file through the official website and install it. 2. iOS device: access the official website through the browser and directly download the APP.

gate.io registration tutorial gate.io registration tutorial Mar 31, 2025 pm 11:09 PM

This article provides a detailed Gate.io registration tutorial, covering every step from accessing the official website to completing registration, including filling in registration information, verifying, reading user agreements, etc. The article also emphasizes security measures after successful registration, such as setting up secondary verification and completing real-name authentication, and gives tips from beginners to help users safely start their digital asset trading journey.

The latest tutorial on downloading and registering on okx exchange The latest tutorial on downloading and registering on okx exchange Mar 27, 2025 pm 04:15 PM

Want to safely enter the digital currency world, start with OKX! This latest 2024 tutorial introduces in detail how to download and register an OKX exchange, and provides real-name authentication and account security settings guide.

okx exchange download and registration tutorial okx exchange download and registration tutorial Mar 27, 2025 pm 04:18 PM

Want to start trading digital currency on OKX Exchange? This newbie tutorial provides you with detailed download and registration steps, and guides you through KYC certification. Download the App from the official channel, set a strong password, and enable two-factor authentication.

How to download kraken exchange How to download kraken exchange Mar 27, 2025 pm 04:21 PM

Want to experience a safe and reliable Kraken exchange? This tutorial details how to download the Kraken App (iOS &amp; Android) and access the web version, and reminds everyone to pay attention to safety. Be sure to download it from the official channel, enable two-factor authentication, and protect account security.

okx Ouyi Exchange web version enter link click to enter okx Ouyi Exchange web version enter link click to enter Mar 31, 2025 pm 06:21 PM

1. Enter the web version of okx Euyi Exchange ☜☜☜☜☜☜ Click to save 2. Click the link of okx Euyi Exchange app ☜☜☜☜ Click to save 3. After entering the official website, the clear interface provides a login and registration portal. Users can choose to log in to an existing account or register a new account according to their own situation. Whether it is viewing real-time market conditions, conducting transactions, or managing assets, the OKX web version provides a simple and smooth operating experience, suitable for beginners and veterans. Visit OKX official website now for easy experience

See all articles