Table of Contents
Don't trim anything
Trim the upper part of the lift and/or the lower part of the lower part
Trip only above the capital height
Trip the upper case height and below the letter baseline
Trip only above x height
Trip x height above and below the letter baseline
Trip only below the baseline of letters
Where is the hieroglyph?
The final thought
Home Web Front-end CSS Tutorial Two CSS Properties for Trimming Text Box Whitespace

Two CSS Properties for Trimming Text Box Whitespace

Mar 08, 2025 am 11:50 AM

Two CSS Properties for Trimming Text Box Whitespace

The

and text-box-trim properties of the text-box-edgeCSS allow developers to accurately control the amount of blank spaces above the first line of text and below the last line of text in the text box. These blanks will make the vertical height of the text box greater than its content height.

This blank is called leading (line spacing), and it appears above and below all text lines (actually two and a half line spacing) to improve the readability of the text. But we just want it to appear between lines of text, right? We don't want it to appear on the upper and lower edges of the text box because it affects our margins, fills, gaps, and other spacing.

For example, if we implement a 50px margin, but the line spacing increases by 37px, there will be a total of 87px space in total. Then we need to adjust the margin to 13px to make the space 50px in practice.

As a design system guy, I try to keep as much consistency as possible and use as few markers as possible, which allows me to use the adjacent brother selector ( ) to create a common rule like this:

/* 当 <element> 后面跟着 <h1> 时 */
<element> + h1 {
  margin-bottom: 13px; /* 而不是 margin-bottom: 50px; */
}</element>
</h1></element>
Copy after login
Copy after login
Copy after login

This method is still troublesome because you still need to do the calculations (although less computationally). However, using the text-box-trim and text-box-edge properties, 50px defined in CSS visually means 50px:

Disclaimer: text-box-trim and text-box-edge are accessible only through the feature flags of Chrome 128 and Safari 16.4, as well as the Safari technology preview without feature flags. See Caniuse for the latest browser support information.

Start from text-box-trim

is a CSS property that activates text box trimming. It has no other purpose in itself, but it does give us options to start, end, start and end or end: text-box-trim

text-box-trim: trim-start;
text-box-trim: trim-end;
text-box-trim: trim-both;
text-box-trim: none;
Copy after login
Copy after login
Copy after login

Note: In older web browsers, you may need to use an older value instead of a new start/end/both value. In older web browsers, you may need to use trim-start/trim-end/trim-both. Unfortunately, there is no reference for this, so you just need to see what works. top/bottom/both

Now, where do you want to prune?

You may want to know what I mean. Consider that a typographic letter has multiple peaks.

There is x height, which marks the top of the letter "x" and other lowercase characters (not including the ascending or upper extension), the uppercase height, which marks the top of the uppercase characters (again, not including the ascending or upper extension), and the letter baseline, which marks the bottom of most letters (not including the ascending or lower extension). Of course, there are also the lifting height and the lowering height.

You can trim the blank between the x height, capital height, or ascending height and the "up" edge of the text box (which is where the upper scribing starts), and the blank between the letter baseline or lower scribing height and the "lower" edge (if text-underline-position is set to under, then where the underline starts).

Don't trim anything

text-box-edge: leading means that all line spacing is included; simply, do not trim anything. This is the same as text-box-trim: none or omitting text-box-trim and text-box-edge completely. You can also use text-box-trim: trim-start to limit lower edge trimming or use text-box-trim: trim-end to limit upper edge trimming. Yes, there are many ways to not do this at all!

Newer web browsers have deviated from the CSSWG specification work draft by deleting the leading value and replacing it with auto despite the "Don't publish (not yet)" warning (*shrugging*).

Of course, text-box-edge accepts two values ​​(instructions about the upper edge, and then instructions about the lower edge). However, auto must be used separately.

/* 当 <element> 后面跟着 <h1> 时 */
<element> + h1 {
  margin-bottom: 13px; /* 而不是 margin-bottom: 50px; */
}</element>
</h1></element>
Copy after login
Copy after login
Copy after login

I can explain all the scenarios auto that work, but none of them are valid. I think all we want is to be able to set the upper or lower edge to auto and set the other edge to something else, but that's exactly what it doesn't do. This is a question, but we will dig deeper soon.

Trim the upper part of the lift and/or the lower part of the lower part

text If used as the first value, the above-up portion will be trimmed; if used as the second value, the below-down portion will be trimmed; if not declared, it is also the default value. (I think you want it to be auto, but it won't be.)

text-box-trim: trim-start;
text-box-trim: trim-end;
text-box-trim: trim-both;
text-box-trim: none;
Copy after login
Copy after login
Copy after login

It is worth noting that the lift and lower height measurements come from the font itself (or not!), so text can be very picky. For example, for Arial fonts, the height of the rise includes the diacritics, the height of the fall includes the drop, and for Fravances fonts, the height of the fall includes the diacritics, I don't know what the height of the rise includes. So, there is a discussion about renaming text to from-font.

Trip only above the capital height

To trim above the capital height:

text-box-edge: auto; /* Works */
text-box-edge: ex auto; /* Doesn't work */
text-box-edge: auto alphabetic; /* Doesn't work */
Copy after login
Copy after login

Remember, undeclared values ​​default to text, not auto (as shown above). So to choose not to trim the lower edge, you need to use trim-start instead of trim-both:

text-box-edge: ex text; /* Valid */
text-box-edge: ex; /* Computed as `text-box-edge: ex text;` */
text-box-edge: text alphabetic; /* Valid */
text-box-edge: text text; /* Valid */
text-box-edge: text; /* Computed as `text-box-edge: text text;` */
Copy after login
Copy after login

Trip the upper case height and below the letter baseline

To trim the upper case height and below the baseline of the letter:

/* 当 <element> 后面跟着 <h1> 时 */
<element> + h1 {
  margin-bottom: 13px; /* 而不是 margin-bottom: 50px; */
}</element>
</h1></element>
Copy after login
Copy after login
Copy after login

By the way, the Caps Height to Baseline option of Figma's Vertical Trim setting does exactly this. However, its development mode generates CSS code with obsolete attribute names (leading-trim and text-edge) and obsolete values ​​(top and bottom).

Trip only above x height

To trim only above x height:

text-box-trim: trim-start;
text-box-trim: trim-end;
text-box-trim: trim-both;
text-box-trim: none;
Copy after login
Copy after login
Copy after login

Trip x height above and below the letter baseline

To trim the x height above and below the letter baseline:

text-box-edge: auto; /* Works */
text-box-edge: ex auto; /* Doesn't work */
text-box-edge: auto alphabetic; /* Doesn't work */
Copy after login
Copy after login

Trip only below the baseline of letters

To trim only below the letter baseline, the following is invalid (Things go so smoothly, didn't it?):

text-box-edge: ex text; /* Valid */
text-box-edge: ex; /* Computed as `text-box-edge: ex text;` */
text-box-edge: text alphabetic; /* Valid */
text-box-edge: text text; /* Valid */
text-box-edge: text; /* Computed as `text-box-edge: text text;` */
Copy after login
Copy after login

This is because the first value is always a required upper edge value, while the second value is an optional lower edge value. This means that alphabetic is not a valid upper edge value, even if including trim-end means we will not provide one. Apart from the verbose complaints, the correct syntax will let you declare any upper edge value, even if you actually cancel it with trim-end:

text-box-edge: cap; /* Computed as text-box-edge: cap text; */
Copy after login

Where is the hieroglyph?

It's hard to know how they will be pruned before a web browser trims, but you can read everything in the specification. In theory, you need to use the ideographic-ink value for trimming, and use the ideographic value without trimming, both are currently not supported:

text-box-trim: trim-start; /* Not text-box-trim: trim-both; */
text-box-edge: cap; /* Not computed as text-box-edge: cap text; */
Copy after login

text-box, abbreviation attribute

If you don't like the verboseness of text box trimming, there is an abbreviation text-box attribute that makes it less important. All the same rules apply.

text-box-trim: trim-both;
text-box-edge: cap alphabetic;
Copy after login

The final thought

At first glance, text-box-trim and text-box-edge may not seem that interesting, but they do make the spacing elements much simpler.

But is the current proposal the best way to deal with text box trimming? Personally, I don't think so. I think text-box-trim-start and text-box-trim-end would make more sense, text-box-trim is used as abbreviation attribute, and text-box-edge is not used at all, but I am willing to accept some simplified and/or consistent approach. What do you think?

There are some other concerns. For example, should there be an option to include underscores, overscores, hanging punctuation marks, or diacritic marks? I would say yes, especially if you are using text-underline-position: under or particularly thick text-decoration-thickness as they will make the spacing between the elements appear smaller.

The above is the detailed content of Two CSS Properties for Trimming Text Box Whitespace. For more information, please follow other related articles on the PHP Chinese website!

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 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
1244
24
How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou&#039;s conic-gradient() polyfill was the last item:

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn&#039;t have to be the case. Let’s look at how PHP projects can enforce a basic

The Three Types of Code The Three Types of Code Apr 11, 2025 pm 12:02 PM

Every time I start a new project, I organize the code I’m looking at into three types, or categories if you like. And I think these types can be applied to

See all articles