Table of Contents
Basic concepts
Line height, line spacing
4 types of boxes
Value
Inherited
Reference
Home Web Front-end HTML Tutorial [Study Notes] line-height_html/css_WEB-ITnose in css

[Study Notes] line-height_html/css_WEB-ITnose in css

Jun 24, 2016 am 11:39 AM

It’s been almost ten days since I wrote an article. One article a week is really hard to say... I originally wanted to continue the previous incident (Part 1) and summarize the types of incidents, but after reading it, it’s still a bit messy to sort it out. , I just kept putting it off and didn't write it... I really can't put it off any longer. Today I will briefly talk about the line-height attribute in CSS that we commonly use but don't necessarily really understand.

Basic concepts

Line height, line spacing

Line height refers to the vertical distance between the baselines of text lines. So what is the baseline? Do you remember that the vertical-align attribute has a baseline value, and this baseline is the baseline. Take a look at this "stealed picture" (selected from the reference article below), I actually modified it~


Note: The second to last picture is the baseline, the bottom That root is the bottom line, not the baseline.

The distance between the two red lines in the picture is line-height (line-height), and the distance between the bottom line of the previous line and the top line of the next line is line spacing, and the distance between the top line and the bottom line of the same line is the size of font-size, half of the line spacing is half line spacing, half line spacing, font-size, line-height The relationship between them can be seen at a glance in the lower right corner of the picture~

半行距 = (line-height - font-size)/2
Copy after login

Of course, the half-line spacing may also be a negative value (when line-height < font-size), then there is will overlap, as shown in the figure below:

4 types of boxes

The four types of boxes to be mentioned are inline box, line box, content area, and containing box ~

  • inline box (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. The height of the inline box is equal to font -size, When line-height is set, the height of the inline box remains unchanged, but the line spacing changes.

  • line box (line box)
    A line box refers to a virtual rectangular box of this line, consisting of in-line boxes in this line. Line boxes are also a concept in browser rendering mode and cannot be displayed. The height of the line box is equal to the maximum value of the height of all inline boxes in this line. When there are multiple lines of content, each line has its own line box.

  • content area (content area)
    The content area is a box surrounding the text and cannot be displayed. Its height depends on font-size and padding. Personally think: The height of the content area = font-size padding-top padding-bottom, which needs to be verified. I also look forward to the answers from my friends~

  • containing box
    The containing box is a box that wraps the above three types of boxes. It’s a bit convoluted~ Look at the picture

Forgive me for my limited drawing skills, but I can still identify it carefully. It can be seen~ ^_^

Value

Generally, the default line-height of the browser is 1.2. You can customize line-height to overwrite this initial value, so how to set line-height? There are the following 5 ways:

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

It seems so simple~ However, line-height is an inheritable attribute, and its inheritance rules are a little complicated...

Inherited

needs to be advanced in advance The explanation is: the size of line-height is closely related to font-size. In addition to specifying the px of line-height, the remaining setting methods are calculated based on font-size .
Let’s talk about it one by one~

  • inherit
    There is actually nothing to say about this. It inherits the value of line-height of the parent element, so the value of the parent element is the same.
    If its descendant element does not set line-height, it will also be this value.

  • length
    Assuming that line-height is set to 20px, then the line height of this line is 20px, which has nothing to do with font-size and will not change with font-size. Corresponding scaling.
    This length value (20px) will be inherited by descendant elements, and all descendant elements will use the same inherited line-height (20px) unless the descendant element sets line-height.

  • Percentage
    Assume that your font-size is 16px and line-height is set to 120%. Then its line height is: 16 * 120% = 19.2px. That is, line-height is calculated based on its own font-size.
    The child element will inherit the line-height of the parent element, so what does it inherit, the percentage (120%)? Or 19.2px?
    The answer is the latter, 19.2px, which is the final value of the parent element line-height after calculation.

  • normal
    When line-height is set to normal, the line height depends on the browser's parsing, usually 1.2.
    Different from the previous one, for an element whose line-height is set to normal, its child elements no longer inherit the calculated final value of its line-height, but are calculated based on the font-size of the child element itself. See the table below~

    element font-size line-height 计算后的lline-height
    body 16px normal 16px * 1.2 = 19.2px
    h1 32px normal 32px * 1.2 = 38.4px

    It can be seen that the sub-elements scale accordingly with the size of their own font-size.

  • Pure numbers
    If you want the flexibility of normal but also want to set a custom value, then use Pure numbers~
    The only difference between pure numeric mode and normal is the size of the value. Pure numeric mode can be set at will, while the value of normal is determined by the browser.

    element font-size line-height 计算后的lline-height
    body 16px 1.5 16px * 1.5 = 24px
    h1 32px 1.5 32px * 1.5 = 48px

    Its descendant elements will inherit this value (such as 1.5), and then calculate their own line-height based on their own font-size.

The summary is as follows:

设置方式 line-height 计算后的line-height 子元素继承的line-height
inherit 父元素的line-height值 不用计算 父元素的line-height值
length 20px 不用计算 20px
% 120% 自身font-size (16px) * 120% = 19.2px 继承父元素计算后的line-height值 19.2px,而不是120%
normal 1.2 自身font-size (16px) * 1.2 = 19.2px 继承1.2,line-height = 自身font-size(32px) * 1.2 = 38.4px
纯数字 1.5 自身font-size (16px) * 1.2 = 19.2px 继承1.5,line-height = 自身font-size(32px) * 1.5 = 48px

So, which one is the best way?
Generally speaking, setting the line height value to pure number is the most recommended way, because it will scale with the corresponding font-size.

This is a summary of line-height, friends are welcome to give feedback~

Reference

MDN line-height
In-depth understanding of css line height Line Height property
CSS line height??line-height

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles