The Magical magic of Line-height

Source: Internet
Author: User

Reprinted from: http://www.w3cplus.com/css/fun-line-height.html

The Line-hight property in CSS is used to control the spacing between lines of text. It generally does not have a set of units (such as: line-height:1.4; ) So this is calculated as a percentage of the font size. This is a very important property for the printed version. The lines are too low to squeeze together and the lines are too high to be far apart, both of which will reduce readability. But you probably already know that.

In this article, we will focus on some design. If you know (or can calculate) the line-height exact value, you can do something wonderful!

Set different colors for each line of text

Unfortunately not here ::nth-line() , our direct use <span> is unreliable, because there are many different things that cause text to be disrupted in different places.

Although non-standard, there is one use of the background of an element as the background of the text.

Css

.text {  -webkit-background-clip: text; -webkit-text-fill-color: transparent;}

And here's another trick you can use linear-gradient() to keep the colors together and not fade into another color. It's just going to end suddenly and start the other one. Let's say we know line-height Yes 22px , we can make a ladder color like this.

Css

.text {  background-image: linear-gradient(    to bottom,    #9588DD,    #9588DD 22px,    #DD88C8 22px,    #DD88C8 44px,    #D3DD88 44px, #D3DD88 66px, #88B0DD 66px, #88B0DD);}

Combined with two different techniques

In browsers that do not support text background clipping, such as Firefox, you get a solid background color block behind the text. Maybe it's cool and you'll like it. But maybe you just want to get that text color. In this case, you can use @support go alone to support it.

Also, when you need to use line-height everywhere, it might be better to set him up as a variable. I'm going to use scss here, and maybe one day you can use variables in CSS as well. So after the change, he was still being rendered, and it looked as if it kept working.

SCSS

$Lh:1.4em;Body{Font-size: 1em; Line-height:$LH;}@supports (-webkit-background-clip:text) {P{-webkit-background-clip:Text -webkit-text-fill-color:Transparent background-ima Ge: Linear- gradient (to bottom, #9588DD, # 9588DD $lh, #DD88C8 $lh, #DD88C8 $lh *2, #D3DD88 $lh *2, #D3DD88 $lh *3, #88B0DD $lh * 3, #88B0DD);  }}         /span>                

It is easiest to use this method at the top of the element. Here is an example of the emphasis on the first few lines being changed.

SCSS

. text{-webkit-background-clip:Text -webkit-text-fill-color:Transparent Background-image:linear-Gradient (to bottom, RGBA,0.8),RGBA (White,0.8) $LH,RGBA (White,0.6) $LH,rgba (white, 0.6) $LH *< Span class= "number" >2, rgba (white, 0.4) $LH *2, rgba (white, 0.4) $LH *3, rgba (white,  0.2) $lh *3, rgba (white, Span class= "number" >0.2)); }         /span>                

This becomes more difficult if we try to position the last few lines on any number of text. In this case, we need to get the first color band to start down from the top of the gradient. Fortunately, we can calc() do that.

Css

. text{-webkit-background-clip:Text -webkit-text-fill-color:Transparent Background-image:linear-Gradient (to bottom, RGBA,0.8),RGBA (White,0.8)Calc100%-66PX),RGBA (White,0.6)Calc100%-66PX),RGBA (White,0.6)Calc100%-44PX),RGBA (White,0.4)Calc100%-44PX),RGBA (White,0.4)calc (100%- 22px), rgba (white, 0.2) calc ( 100%-22px), rgba (white, 0.2));   Background-position: bottom Center;            /span>                

There are other ways to do this, such as covering a pseudo-element gradient ( pointer-events: none; so it's not annoying).

Line lines between text

Using a similar solid-color-stops technique that we used above, we can create a 1px line that line-height repeats on the known. The simplest approach is to use it repeating-linear-gradient() to make sure that other elements are working properly (for example, padding based on this line-height ).

SCSS

. Parent{Padding:$LH *2; Background: #082838; Background-image:repeating-linear-Gradient (to bottom, RGBA,0)0,rgba (white, 0) $LH/1em*16px-< Span class= "number" >1, rgba (white, 0.1) $LH/1em*16px- 1, rgba (white, 0.1) $LH/1em* 16px); }         /span>                

In order to get the 1px line used, we need to know what line-height the pixel value is, and then subtract it with this value 1px . Our goal is to repeat the line-height gradient at exactly the height so that the last pixel of the space is a line. Because we are not the body size of the font 1em , but 16px the. And the line-height settings are in em units. So we can 1em divide the cells to be deleted, then copy 16px them and subtract them when needed 1 .

The position of each line of the picture

If you know exactly line-height , the other thing you can do is use the background-size vertical axis to match it. Then you repeat him vertically, and he will have a queue of pictures in each line.

SCSS

.text  background-image: url(image.svg);  $lh $lh;  background-repeat: repeat-y;  padding-left: $lh*2;}

This article according to @chris Coyier's "Fun with line-height!" translation, the entire translation with our own understanding and thought, if the translation is not good or the wrong place also please peer friends pointing. If you want to reprint this translation, please specify the English source: https://css-tricks.com/fun-line-height/.

The Magical magic of Line-height

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.