Home > Web Front-end > HTML Tutorial > How to center-align text in HTML? (code example)

How to center-align text in HTML? (code example)

藏色散人
Release: 2018-08-10 17:38:03
Original
60214 people have browsed it

This article mainly introduces how to write HTML text centering code. Hope it helps friends in need.

The specific example of the html text centering code is as follows:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <title>html文字居中测试</title>
    <meta charset="UTF-8">
    <style type="text/css">
        body{background: #ddd;}
        div{width:300px;height:180px;margin:10px auto;color:#fff;font-size:24px;}
        .box1{background: #71a879;text-align: center;}
        .box2{background: #6a8bbc;line-height: 200px;}
        .box3{background: #dea46b;text-align: center;line-height: 200px;}
    </style>
</head>
<body>
<div  class="box1">html文字水平居中</div>
<div  class="box2">html文字垂直居中</div>
<div  class="box3">html文字水平上下居中</div>
</body>
</html>
Copy after login

The effect of the above code is tested locally as shown below:

How to center-align text in HTML? (code example)

So through this article, do you have a better understanding of the relevant knowledge about HTML text centering? In fact, here we mainly remember these key points. In HTML, the css code to center the text:

1. Flat centering: text-align:center;

text-align attribute specifies the element The horizontal alignment of the text in .

This attribute sets the horizontal alignment of text within block-level elements by specifying the point at which the line box is aligned. The value justify is supported by allowing user agents to adjust the spacing between letters and words in line content; different user agents may get different results.

2. Vertical centering: line-height:height;

The line-height property sets the distance between lines (line height).

Note: Negative values ​​are not allowed.


The above is the detailed content of How to center-align text in HTML? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template