search
HomeWeb Front-endHTML Tutorial[Study Notes] line-height_html/css_WEB-ITnose in css

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

Of course, the half-line spacing may also be a negative value (when line-height

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
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
HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 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.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 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 and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),