How to set line height in css

青灯夜游
Release: 2023-01-04 09:34:18
Original
10364 people have browsed it

In CSS, you can use the line-height attribute to set the line height. You only need to add the "line-height: line height value;" style to the specified element, for example "line-height: 10px;" . The line-height attribute can set the distance between lines (line height), and negative values ​​are not allowed.

How to set line height in css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

The so-called line height refers to the vertical distance between the baselines of text lines. To understand this sentence, you must first understand a few basic knowledge:

Top line, middle line, baseline, bottom line

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <style type="text/css" >
            span
            {
                padding:0px;
                line-height:1.5;
            }
        </style>
    </head>
    <body>
        <div class="test">
            <div style="background-color:#ccc;">
                <span style="font-size:3em;background-color:#999;">中文English</span>
                <span style="font-size:3em;background-color:#999;">English中文</span>
            </div>
        </div>
    </body>
<html>
Copy after login

How to set line height in css

The four lines from top to bottom are the top line, the middle line, the baseline, and the bottom line. They are very similar to the four lines and three spaces when learning English letters. We know that the vertical-align attribute includes top, middle, baseline, and bottom. , which is related to these four lines.

Remember especially that the baseline is not the bottom line, the bottom line is the bottom line.

Line height, line spacing and half-line spacing

Line height refers to the vertical distance between the baselines of the context line , that is, the vertical distance between the two red lines in the figure.

Line spacing refers to the vertical distance from the bottom line of one row to the top line of the next row, that is, the vertical distance between the pink line of the first row and the green line of the second row.

Half line spacing is half of the line spacing, that is, the vertical distance of area 3/2, the sum of the distances of areas 1, 2, 3, and 4 is the line height, and area 1, 2, 4 The sum of the distances is the font size, so half line spacing can also be calculated like this: (line height - font size)/2

How to set line height in css

Content area, inline box , Line box

Content area: The area wrapped by the bottom line and the top line, that is, the dark gray background area in the picture below.

How to set line height in css

Inline box, each inline element will generate an inline box. The inline box is a concept in the browser rendering model and cannot be displayed. , when there are no other factors (padding, etc.), the inline box is equal to the content area, and when the line height is set, the height of the inline box remains unchanged, and the half line spacing [(line height-font size)/2] increases/decreases to the content respectively. The upper and lower sides of the area (dark blue area)

Line box (line box), the line box refers to a virtual rectangular box of this line, is a concept in the browser rendering mode , and is not actually displayed. The height of the line box is equal to the largest value of the inline box among all elements in this row (the inline box with the largest row height value is used as the benchmark, and other inline boxes are aligned to the benchmark using their own alignment methods, and the height of the line box is finally calculated). When there are multiple lines content, each line will have its own line box.

<div   style="max-width:90%">
                <span style="font-size:1em;background-color:#666;">中文English</span>
                <span style="font-size:3em;background-color:#999;">中文English</span>
                <span style="font-size:3em;background-color:#999;">English中文</span>
                <span style="font-size:1em;background-color:#666;">English中文</span>
            </div>
Copy after login

How to set line height in css

(Learning video sharing: css video tutorial

line-height

Now that the basic concepts are clear, we can talk about the line-height attribute, the protagonist of this article.

Definition: The line-height attribute sets the distance between lines (line height), and negative values ​​cannot be used. This property affects the layout of the line box. When applied to a block-level element, it defines the minimum distance between baselines in that element rather than the maximum distance. The difference between the calculated values ​​of line-height and font-size (line spacing) is divided into two halves and added to the top and bottom of a line of text content respectively. The smallest box that can contain this content is a line box.

Possible values

说明
normal默认,设置合理的行间距。
number设置数字,此数字会与当前的字体尺寸相乘来设置行间距。相当于倍数
length设置固定的行间距。
%基于当前字体尺寸的百分比行间距。
inherit规定应该从父元素继承 line-height 属性的值。

示例:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<style type="text/css">
			p.small {
				line-height: 90%
			}
			
			p.big {
				line-height: 200%
			}
		</style>
	</head>

	<body>

		<p>
			这是拥有标准行高的段落。 在大多数浏览器中默认行高大约是 110% 到 120%。 这是拥有标准行高的段落。 这是拥有标准行高的段落。 这是拥有标准行高的段落。 这是拥有标准行高的段落。 这是拥有标准行高的段落。
		</p>

		<p class="small">
			这个段落拥有更小的行高。 这个段落拥有更小的行高。 这个段落拥有更小的行高。 这个段落拥有更小的行高。 这个段落拥有更小的行高。 这个段落拥有更小的行高。 这个段落拥有更小的行高。
		</p>

		<p class="big">
			这个段落拥有更大的行高。 这个段落拥有更大的行高。 这个段落拥有更大的行高。 这个段落拥有更大的行高。 这个段落拥有更大的行高。 这个段落拥有更大的行高。 这个段落拥有更大的行高。
		</p>

	</body>

</html>
Copy after login

效果图:

How to set line height in css

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of How to set line height in css. 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