CSS text alignment implementation code

CSS text alignment implementation code

When making forms, we often encounter the situation of aligning two fields, such as name, mobile phone number, and place of birth. In this way, we need to use text-align and text-justify styles.

Just set text-align to justify. The situation of text-justify is complicated, and some people may not be familiar with it. The values ​​of IE are as follows:

  • auto : Allows the browser user agent to determine the justification to use
  • inter-word : Justify text by adding spaces between words. This behavior is the fastest way to align all lines of text. Its justification behavior does not work on the last line of a paragraph.
  • newspaper : Justify text by increasing or decreasing the space between words or letters. Is the most precise format for justifying the Latin alphabet
  • distribute : handles spaces like newspaper
  • distribute-all-lines: Justifies lines in the same way as distribute, but also excludes the last line of two aligned paragraphs. Applicable to ideographic documents
  • inter-ideograph : Provides full justification for ideographic text. He increases or decreases the space between ideograms and words

But it was originally implemented as a private feature of IE, like text-overflow, overflow-x, etc., and was not implemented until very late in FF. In other words, there are strict compatibility issues. In addition, FF and Chrome require manual insertion of spaces or soft line breaks between Chinese characters for this to take effect, and the resistance encountered in Chrome is even greater. p>

plan:

.test1 {
          text-align:justify;
          text-justify:distribute-all-lines;/*ie6-8*/
          text-align-last:justify;/* ie9*/
          -moz-text-align-last:justify;/*ff*/
          -webkit-text-align-last:justify;/*chrome 20+*/
      }
      @media screen and (-webkit-min-device-pixel-ratio:0){/* chrome*/
          .test1:after{
              content:".";
              display: inline-block;
              width:100%;
              overflow:hidden;
              height:0;
          }
      }

Run the code:

<!DOCTYPE HTML>
    <html>
        <head>
            <title>Text Alignment</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <style>
                .box1{
                    background:red;
                    width:30%;
                }
                .test1 {
                    text-align:justify;
                    text-justify:distribute-all-lines;/*ie6-8*/
                    text-align-last:justify;/* ie9*/
                    -moz-text-align-last:justify;/*ff*/
                    -webkit-text-align-last:justify;/*chrome 20+*/
                }
                @media screen and (-webkit-min-device-pixel-ratio:0){/* chrome*/
                    .test1:after{
                        content:".";
                        display: inline-block;
                        width:100%;
                        overflow:hidden;
                        height:0;
                    }
                }
            </style>
        </head>
        <body>
            <div class="box1">
                <div class="test1">Name</div>
                <div class="test1">Name Name</div>
                <div class="test1">Name</div>
                <div class="test1">Location</div>
                <div class="test1">Workplace</div>
            </div>
        </body>
    </html>

Summarize

The above is the implementation code of CSS text alignment introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<:  Disable IE Image Toolbar

>>:  How to install OpenSuse on virtualbox

Recommend

Use nginx to dynamically convert image sizes to generate thumbnails

The Nginx ngx_http_image_filter_module module (ng...

Summary of common Linux distribution mirror source configuration

I have been researching Linux recently and tried ...

Install Apache2.4+PHP7.0+MySQL5.7.16 on macOS Sierra

Although Mac systems come with PHP and Apache, so...

Detailed explanation of mysql transaction management operations

This article describes the MySQL transaction mana...

SVG button example code based on CSS animation

The specific code is as follows: <a href="...

Summary of 7 types of logs in MySQL

There are the following log files in MySQL: 1: re...

HTML&CSS&JS compatibility tree (IE, Firefox, Chrome)

What is a tree in web design? Simply put, clicking...

JavaScript to achieve click image flip effect

I was recently working on a project about face co...

Comparative Analysis of UI Applications of Image Social Networking Sites (Figure)

In our life, work and study, social networks have ...

How to use @media in mobile adaptive styles

General mobile phone style: @media all and (orien...