Jump to content

How do I apply a linear gradient to text, that is wrapping?


simona6

Recommended Posts

.class{
-webkit-background-clip: text;
    color: none;
    background: linear-gradient(#333333 45%, #000000 73%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

I want to apply this gradient so it is lighter at the top, ie if it was white at the top and soft grey at the bottom.

The problem is, if I do this to text that is bold and goes to two lines, the top line is one shade and the bottom is the other shade.

Rather than might splitting the two rows into two tags, can this be done on a 'per line' basis?

Link to comment
Share on other sites

So it's impossible to make it the way I explained?
IE if you had a slogan, and it had to be on two rows... you have to them make two rows of H2 tags.. and apply the gradient to each row?

That's very awkward.

Link to comment
Share on other sites

The trick is to get the right ratio between gradient length and font size. With font "arial black" I found
 

grad length px : font_size pt     approx  1.8 : 1
   

 

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title>Example</title>
   <style type='text/css'>
        body {
            background-color: #000;
        }
        .gradhead {
        font-family: "arial black";
        font-size: 30pt;
        background: repeating-linear-gradient(#FFF , #EEE , #006EFC 55px);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        width: 30%;
        float: left;
        padding: 8px;
        }  
   </style>
</head>
<body>
    <div class="gradhead">
        LINE 1
   </div>
    <div class="gradhead">
        LINE 1<br>
        LINE 2
   </div>
    <div class="gradhead">
        LINE 1<br>
        LINE 2<br>
        LINE 3
   </div>
</body>
</html>

image.png.b7b87666d53826b729c2e02264f0fdf9.png

Link to comment
Share on other sites

To achieve a gradient that transitions from lighter at the top to darker at the bottom on a per-line basis, especially when the text spans multiple lines and is bold, you can utilize the background-clip: text property along with a linear gradient background.

.class {
    color: transparent; /* Hides the original text */
    background: linear-gradient(to bottom, #FFFFFF 0%, #CCCCCC 100%);
    -webkit-background-clip: text; /* For Safari */
    background-clip: text;
    -webkit-text-fill-color: transparent; /* Ensures the text is transparent */
    font-weight: bold; /* Optionally, if your text is bold */
}

Color and Gradient Setup:

color: transparent;: This makes the original text color transparent so that it doesn't show.

background: linear-gradient(to bottom, #FFFFFF 0%, #CCCCCC 100%);: Defines a linear gradient that starts with #FFFFFF (white) at the top (0%) and transitions to #CCCCCC (light grey) at the bottom (100%).

Background Clip:

-webkit-background-clip: text;: Ensures that the background gradient is clipped to the shape of the text. This property is prefixed for compatibility with older versions of Safari.

background-clip: text;: Standardized version of background-clip for other browsers.

Text Color:

-webkit-text-fill-color: transparent;: Ensures the text itself remains transparent so that the gradient background shows through the text.

 

Best Regard

Danish hafeez | QA Assistant

ICTInnovations

Link to comment
Share on other sites

@Danishhafeez

Again (with amazing consistency)...

BS_meter.JPG.523e145a2b9806f93490742b6b82ab19.JPG

All you (and your pet AI?) have done is replicate the original problem, demonstrated below

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bovine Excrement</title>
<style type='text/css'>

    .class {
        color: transparent; /* Hides the original text */
        background: linear-gradient(to bottom, #FFFFFF 0%, #CCCCCC 100%);
        -webkit-background-clip: text; /* For Safari */
        background-clip: text;
        -webkit-text-fill-color: transparent; /* Ensures the text is transparent */
        font-weight: bold; /* Optionally, if your text is bold */
    }
    
</style>
</head>
<body>
    <h1 class='class'>
        Line 1<br>
        Line 2<br>
        Line 3
    </h1>
</body>
</html>

Giving

image.png.e5139350e7e3d4c555f80367a0bd9681.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.