skip to Main Content

I’ve been learning the basics of front-end web dev and started doing "The Odin Project"

I saw an example of a website that I saw on Git-Hub that used HTML and CSS only.

I tried taking some inspiration and making my own version.

I added some text and wanted to see if it’s possible to add a gradient to text using CSS.

A lot of examples that use CSS use the same type of code which is using
-webkit-background-clip: text background-clip: text; color: transparent;

It works fine until I imported this custom text: https://www.dafont.com/the-sunset.font
I made a <h2> tag and it seems like it cut the edge off the right side.

Image Without The Gradient Code

Image With The Gradient Code

As you can see in the image with the code, the last character seems to be cut off

It works better with other fonts but I want to use that font.

Is there some other way I can make gradient text without issues like that?

Even if it’s in Javascript, which I’m still yet to learn anything about or maybe something like
sass? I don’t mind waiting until I eventually have to learn it, I’ve been told coding is a life long process.

My other question will be about the code itself other than the gradient text, is what I’m writing in good practice? Better way to do something than I already am doing? which there probably is since I’ve been learning for only a couple of weeks.

I appreciate any answers, thank you!

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="mine.css">
</head>
<body>
    




<div class="container">

  <div class="block1">
    <h2 class="block2">SunSet</h2>
  </div>

<div class="block3"></div>
<div class="block4"></div>
</div>



</body>
</html>

CSS

@import url("myfont.css");


 
body {

    margin: 0;
    padding: 0;
}




.container .block1 {
    background: url(../css/train.png) no-repeat center center fixed;
    background-size: cover;
    height: 1000px;
    position: relative;
}



.container  .block2 {

    padding: 0px;
    top: 20px;
    left: 50px;
    position: absolute;
    font-family: Bingo;
    font-size: 150px;
    background: linear-gradient(45deg, #ff7e5f, #feb47b, #ff7e5f); 
    -webkit-background-clip: text; 
    background-clip: text;
    color: transparent; 
}

Font CSS

@font-face {
    font-family:'Bingo'; /* Choose a name for your font */
    src: url('../css/Bont/The-Sunset.ttf') format('truetype'); /* Adjust the path and format based on your font file */
    /* Add additional src entries for other font formats if needed (e.g., woff, woff2) */
  }

Ignore the font names like "Bingo" and folder name "Bont" I just name stuff really weird.

I’ve tried removing everything besides that one div I’m calling in css to place the image because maybe there was another div cutting the end of the text but it doesn’t seem to be the case.

Changing fonts which seems to fox it, but I’m not sure if there is a problem with how I think but I refuse to use another font just to fix the problem because I want to use that font I should be able to use that font…right?

2

Answers


  1. Chosen as BEST ANSWER

    I appreciate the answer given by @herrstrietzel, I thought I checked all the of threads on this issue but I was wrong, I will paste below the solution that I have been using thus far and it has been working well. It's all to do with the way the text is presented.

    Solution that I use


  2. If you select the text you see it’s cropped
    based on the actual glyph/character bounding boxes.

    The lowercase t is exceeding the bounding box (defined by its metrics: left and right side bearings).

    So you can add a width:100% property to extend the masks to the full width.

    @font-face {
        font-family: 'The Sunset';
        src: url('data:font/woff2;charset=utf-8;base64,d09GMgABAAAAAAc4ABEAAAAADTAAAAbeAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGhYbgSgcKgZgAFwIAgmCcxEICoh0h1oLFgABNgIkAygEIAWGewc5DAQbVAvIjsN0OysnSZIv7vHwsd+vc796QsQziYY3swgp4S0yhEiiwXqzumnTvp+nbfX+DLQ2YODJoBtFmgUoVs9Go41ZcNdsFCFuJWxxPRuRR1x0A8/bGKs8VDLJPpIYpTEyLdwdLta1REIT1XVGSR56YynPYwrNFVPwP/nDBFABIAXBsCEIKJHKhK1pSmsh6HpVvx4i0QCJH0DfHAFxpevVAwvEytFgACueh9ooK1YijUqLB4C+Nsii/38CGMlHmFtYtezkdaCl+JHfOe/+OSxFtO+ISE8+tPIR1t5MMWxzzIuyD0+nTqDNQAepG+wMgDt3NfEDFIGBGTKwdyzk4hjIDvtTAbJ/UUh5u9Qr7XuAKcXDlnR9Bw65HjRF+TWsyf+Ug1BDR28g8QPELfr/wv+X/9PkJf9ev91vBmT1pp1ngJl1Clg7jS8itJGkkP8ZClS9p4AFP10uoQDNEIS6VeVCK+1UbCaROiW2Y/CsXTUufLamujGeJEyESbvWxFoQLavWumimcXXmGjoTKSNcULPXanav3RO7cklh0EdHkukSCmhdWNEUUrFMoq2xVZsW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DCYz6urhH4a+D6G7jt8oQQhZsbm8bRxpJbWhnhBew4OMUt3goQeEzB/EZA6VegAJ+HXsKclyuiDhDQOWj+NIDs4LECVxdrbOrct63W7ZlfY8NCwuwFDXimjNiE2DZgx3GDhyAgQLt5KnZFcOZvLBH+JQDva/Qy9/I6rPL/f///gRlO/wVAAQYQ4CiQkm+V2P9HrgDIyrOUfgoyY5Pv/gcg9wShLvu/Rvn/gn2NPMHj/7+pZBCt0/C4F6SgXTR1RTag0AVI4IiWFKZZkiawcGamXLLi5ElOiDWSx9MnBeGekyK6GSkj7J5ZofHtrrfCoQwbwIoLVBCeXwQTFEzBLQr+TBoXOlXkCQMRzwjTvDgoLHPiXhUbJ5hw5cZ/MwNYXDiNc/z3o21h76rultbh/59E/I8ytbbFbBzQ9+n6/9HYSiGVy5UZWaNF5COaiBLzBrm5RjPvXNf0EkppAhsTPd4w8znTjdruAX3z7a85301oLg4/iwsHczH5buAAJTmFVIsQCvVapVuLVsMIpVrp6RBq9BuwVqsutVqYguB6fXT6Vcc2w+EdVumlICUnp5SBnCi0r6fTq6/T5XpEs3hNOq6/iea1TQhl+Ph2aEtQkkpATq77yS9pAQbjMOW0ugfxaNai1l7d0smshpZLNXeQ2AEz++ATSK3RpRNmZV9SErDAnAOXrEiVBo2pUg0FMA0A') format('woff2');
        font-weight: normal;
        font-style: normal;
        font-display: swap;
    }
    
    
    body {
      margin: 0;
      padding: 0;
    }
    
    
    .container .block2 {
      font-family: "The Sunset";
      padding: 0px;
      top: 20px;
      left: 0px;
      width: 100%;
      text-align:center;
      position: absolute;
      font-size: 150px;
      font-weight: 400;
      background-image: linear-gradient(45deg, #ff7e5f, #feb47b, #ff7e5f);
      -webkit-background-clip: text;
      background-clip: text;
      color: transparent;
      border: 1px solid red;
    }
    <div class="container">
      <div class="block1">
        <h2 class="block2">SunSet</h2>
      </div>
    
      <div class="block3"></div>
      <div class="block4"></div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search