search
HomeWeb Front-endCSS Tutorialwhat is css baseline

In CSS, the baseline "base line" is not the lower edge of Chinese characters, but the lower edge of English letters. The baseline is always consistent with the tallest element in the row, and the baseline changes with the tallest element in the row.

what is css baseline

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

1. Basic concepts

1. Baseline, bottom line, top line, middle line

Note: Baseline (base line) is not The lower edge of the Chinese character text is the lower edge of the English letter "x".

2. Content area

The content area refers to the area wrapped by the bottom line and the top line (inline elements display:inline; can be displayed through the background-color attribute out), you may not necessarily see it in practice, but it does exist. The size of the content area changes based on the value of font-size and the number of words.

3. Line spacing and line height

Line-height: includes the content area and the blank area that is symmetrically expanded based on the content area. We call this row height. Generally speaking, it can also be considered as the distance between the baselines of adjacent text lines.

Line spacing: refers to the distance between the baseline of the previous text line and the top line of the next text line between adjacent texts. Of course, I prefer to think of it as (upper text line height - content area height)/2 (lower text line height - content area height)/2.

4. Inline box

The inline box is a concept in the browser rendering model. It cannot be displayed, but it does exist. Its height It is the height specified by the row height.

5. Line box

Line box (line box) is a similar concept to the inner box of the same line. The line box refers to a virtual rectangle of the current line. Box is also a concept in browser rendering mode. The height of the line box is equal to the largest value of the inline box among all elements in this line (the inline box with the largest line 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)

2. Vertical-align: Set the vertical alignment of the element.

Line-height is the vertical centering (line-height) of a single line of pure text. If the line contains pictures and text, after the browser renders them, readers can find that the text and pictures are not centered along the center line in the vertical direction. , but aligned along the baseline. This is because the default vertical alignment of elements is baseline alignment (vertical-align: baseline).

CSS syntax: vertical-align

 Syntax:

  baseline | sub | super | top | text-top | middle | bottom | text-bottom | | | inherit

    Description:

      Set the vertical alignment of the element content.

 Parameters:

    baseline: Baseline alignment;

    sub: Subscript display;

     super: Superscript display; : Top alignment;

∼ posit text-top: Top alignment of text;

∼ posit middle: Middle alignment; // Not well-studied properties

∼ bottom: Bottom alignment;

##  text -bottom: The bottom alignment of the text;

## #   Percentage and length: CSS2, can be negative.

Initial value: baseline

Inheritance: Not inherited element nodes to correctly handle the vertical-align attribute). Also, this property cannot be inherited.

Detailed explanation of attribute values

In the above section, we introduced the baseline, top line, middle line and bottom line of the text, as well as the content area, inline box and line box. In this section, the vertical Alignment is closely related to these concepts.

1. Baseline alignment (vertical-align: baseline)

Baseline alignment (vertical-align: baseline) makes the baseline of the element the same as the base element (take Baseline alignment with the highest row height as the baseline

what is css baseline

2. Top alignment (vertical-align: top)

Top alignment (vertical-align : top ) is to align the top of the element's inline box with the top of the line box

what is css baseline

3. Text top alignment (vertical-align: text-top)

Text top alignment (vertical-align: text-top) is to frame the element inline The top is aligned with the top line of the text line

what is css baseline

4. Vertical-align : bottom

Vertical-align : bottom ) is the opposite of top alignment (vertical-align : top)

what is css baseline

5. Text bottom alignment (vertical-align : text-bottom)

what is css baseline

6. Vertical-align: middle

Middle alignment (vertical-align: middle) is usually used on pictures to align the vertical center line of the picture with the line of text. Centerline alignment. (There are some deviations in the processing of words. The specific basis has not been researched yet. Students who have done research can contact me~~)

what is css baseline

The definition of the midline is: the midline is located at the baseline. Above, the distance from the baseline is half the height of the lowercase letter Align a quarter of an em above the baseline as the midline.

7. Superscript and subscript

Superscript (vertical-align:super) raises the baseline of the element relative to the baseline of the base element, and subscript (vertical-align:sub) Lowering the baseline of the element and moving it by a certain amount are not specified in the CSS specification and are determined by the browser.

what is css baseline

Superscript and subscript will not change the size of the element text.

8. Length values ​​and percentages

Similar to superscripts and subscripts, length values ​​and percentage values ​​can make the baseline of an element higher (positive value) or lower (negative value) relative to the baseline of the base element. ).

The moving size of superscripts and subscripts is determined by the browser, and setting the length value or percentage can accurately control the range of text movement up and down.

Percentage is related to line-height. For example, the following code is displayed as shown in the figure below.

what is css baseline

My test under @FireFox

Test code:

<style type="text/css">
  p {
     vertical-align:baseline;    
     font-size:20px;  
     line-height:60px; 
     background-color:yellow; 
  }
  span {
     background-color: red; 
  }
  u { 
     background-color: blue; 
  }
  del { 
     background-color: pink; 
  }
</style>

//HTML代码
<p> 
    <span>Ajax测试</span> 
    <u>Ajax测试</u> 
    <del>Ajax测试</del> 
    Ajax测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试 
</p>

Default:

what is css baseline

Other instructions:

1. The offsetWidth of SPAN, U, and DEL tags = SUM (character * font-size * correction coefficient) (here, the correction coefficient for Chinese is 1, and the correction coefficient for numbers is 1 0.6, English character correction coefficients vary greatly, for example, ijl is very small, wmk, etc. are relatively large, and the correction coefficients for uppercase English are also not uniform).

2. The offsetHeight of SPAN, U, and DEL tags.

Corollary: The background rendering area of ​​the inline element, that is, the size of the content area, is directly affected by font-size.

 For

block-level elements, the calculated height of the block-level element is calculated by adding up the height of the included line boxes, so the height here is 60px;

  3. Change span.style .lineHeight is set to 15px (changed from 10px to 60px) —-> Found no change

 Inference: The size of the content area is not affected by line-height, which is used to handle the gap between the baselines of adjacent text lines distance.

4. Set span.style.lineHeight to 70px (from 61px to 80px) —->The height of the line box begins to adjust with the setting

 Corollary 1: The height of the line box is within the line The highest inline box height, adjusted through line-height.

The calculated height value of p element is span.style.lineHeight value, which is not controlled by p.style.lineHeight.

Corollary 2: The calculated height value of

without setting the height attribute is the accumulated value of the line box height.

what is css baseline

## 5. Put span.style.verticalAlign= sub; del.style.verticalAlign= super; –>Look at the picture and talk

Inference: added The height of the line box, the upper and lower subscripts move based on the baseline.

6. Confirmation of all alignment methods:

∣ positi a) First confirm the base element in the line, and take the element with the maximum line-height value as the base; )Other text is aligned to the base element, and the effect is achieved based on line-height and vertical-align;

    c) Sub and super are ways to change the baseline, so they will have an impact on the final height of the line box;

    d) Top and bottom are inline box alignments. Top refers to the alignment of the top of the element's inline box with the top of the base inline box;

#     d) Top and text-bottom will also affect the final line box. Height means that the top of the inline box of the element is aligned with the top of the content area of ​​the base element (when line-height=content area height, it is aligned with the top of the base content area. When line-height is less than the height of the content area, the text will continue to rise. The phenomenon of shifting, when line-height is set to 0px, the vertical middle of the content area is exactly aligned with the top of the reference content area.)

what is css baseline

    f) Percentage and length values: based on The baseline is moved, and the percentage is calculated as row height * percentage.

g) Regarding middle, it feels like it is symmetrically expanded based on a baseline, but the rules for generating the baseline are unclear.

Recommended learning:

css video tutorial

The above is the detailed content of what is css baseline. For more information, please follow other related articles on the PHP Chinese website!

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
@keyframes vs CSS Transitions: What is the difference?@keyframes vs CSS Transitions: What is the difference?May 14, 2025 am 12:01 AM

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

Using Pages CMS for Static Site Content ManagementUsing Pages CMS for Static Site Content ManagementMay 13, 2025 am 09:24 AM

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.