Creating gradient text using CSS selectors

I have a code structure where I want to apply a gradient to 3 specific letters, but currently they are being applied individually which doesn't look good. I need the gradient to span across these letters without changing the overall structure as it is used in an animation.

.gradient-text {
  font-size: 24px; 
  font-weight: bold; 
  display: inline-block; 
}

.gradient-text div {
  display: inline; 
  color: black; 
}

.gradient-text div:nth-child(n+4):nth-child(-n+6) span {
  /* Targets letters L to B */
  background: linear-gradient(to right, blue, red);
  -webkit-background-clip: text;
  color: transparent;
}
<div class="gradient-text">
  <div><span>A</span></div>
  <div><span>P</span></div>
  <div><span>P</span></div>
  <div><span>L</span></div> <!-- Start of the range -->
  <div><span>E</span></div>
  <div><span>B</span></div> <!-- End of the range -->
  <div><span>A</span></div>
  <div><span>L</span></div>
  <div><span>L</span></div>
</div>

Answer №1

Here are two potential solutions:

  1. If maintaining the structure of <div> and <span> is crucial for your CSS animation, you could implement three separate smaller gradients for each individual letter.

    .gradient-text {
      font-size: 24px; 
      font-weight: bold; 
      display: inline-block; 
    }
    
    .gradient-text div {
      display: inline; 
      color: black; 
    }
    
    .gradient-text #letterL {
      background: linear-gradient(to right, blue, #4d00b2);
      -webkit-background-clip: text;
      color: transparent;
    }
    .gradient-text #letterE {
      background: linear-gradient(to right, #4d00b2, #92006d);
      -webkit-background-clip: text;
      color: transparent;
    }
    .gradient-text #letterB {
      background: linear-gradient(to right, #92006d, red);
      -webkit-background-clip: text;
      color: transparent;
    }
    <div class="gradient-text">
      <div><span>A</span></div>
      <div><span>P</span></div>
      <div><span>P</span></div>
      <div><span id="letterL">L</span></div> <!-- Start of the range -->
      <div><span id="letterE">E</span></div>
      <div><span id="letterB">B</span></div> <!-- End of the range -->
      <div><span>A</span></div>
      <div><span>L</span></div>
      <div><span>L</span></div>
    </div>

  2. If you can group the letters within the same affected element, the gradient will apply to all letters in that element.

2a) Utilizing the existing <div>, <span>, and gradient:

.gradient-text {
  font-size: 24px; 
  font-weight: bold; 
  display: inline-block; 
}

.gradient-text div {
  display: inline; 
  color: black; 
}

.gradient-text div:nth-child(n+4):nth-child(-n+4) span {
  /* Targets letters L to B */
  background: linear-gradient(to right, blue, red);
  -webkit-background-clip: text;
  color: transparent;
}
<div class="gradient-text">
  <div><span>A</span></div>
  <div><span>P</span></div>
  <div><span>P</span></div>
  <div><span>L E B</span></div> <!-- Range: 4th-4th child (only this one) -->
  <div><span>A</span></div> 
  <div><span>L</span></div>
  <div><span>L</span></div>
</div>

2b) Introducing a wrapper and utilizing background-image:

.gradient-text {
  font-size: 24px;
  font-weight: bold;
  display: inline-block;
}

.gradient-text div {
  display: inline;
  color: black;
}

.gradient-text #gradient >* {
  /* Targets letters L to B */
  background-image: linear-gradient(to right, blue, red);
  background-attachment: fixed;
  -webkit-background-clip: text;
  color: transparent;
  background-size: 10%;
  background-position: 10%;
}
<div class="gradient-text">
  <div><span>A</span></div>
  <div><span>P</span></div>
  <div><span>P</span></div>
  <div id="gradient">
    <div><span>L</span></div> <!-- Start of the range -->
    <div><span>E</span></div>
    <div><span>B</span></div> <!-- End of the range -->
  </div>
  <div><span>A</span></div>
  <div><span>L</span></div>
  <div><span>L</span></div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

On mobile devices, the text is showing up without the image, while on desktops the image is displaying correctly alongside the text in

I have designed a responsive UI page using bootstrap, featuring a responsive image on the left side and text on the right side of the section. Currently, the image appears on desktop but not on any mobile device when tested using developer tools. Below i ...

Using jquery to target and manipulate elements with the div selector

I need assistance with adding a selector on my webpage where users can choose one option and retrieve the id of the selected part. This id will then be used in a link. I am new to using jQuery, so I am having trouble implementing this feature. Below is an ...

Attempting to utilize jQuery for altering the background URL leads to the unexpected removal of the -webkit-background-clip:text; effect

Can the -webkit-background-clip:text; effect be preserved when using jQuery to switch the background url? I am experiencing issues where changing the URL removes the text clipping effect. jQuery(function($) { var whatToSwitch = $('.homepage_ove ...

Modify the hover color of heading 1 only for hyperlink texts

I am attempting to adjust the hover color specifically for text that contains a link. Below is the CSS code I have tried, but it changes the color regardless of whether there are links present or not. h1, h2, h3, h4 { color:#3F3F3F; } h1:hover, h2:hove ...

Embed one div within another div in a flexible layout design

I am working on a design where I need to place a div (profile picture) inside another div (profile banner) in a way that the profile picture is slightly outside of the outer div. This may sound complicated, but I only need it to display this way on desktop ...

Unexpectedly, the jQuery get function retrieves the entire webpage

I'm encountering difficulties when using .get requests to update elements through page load or button press. Here is the code snippet... $(document).ready(function() { //updateVariable(); $('#dannychoolink').html('<?php dan ...

Browsersync and Gulp Continuously Refreshing CSS

My Gulp and Browsersync setup is causing the CSS in the Style.css file to reset every time I run npm start. While the changes to index.html persist and I can successfully push the initial CSS changes to Github, I am puzzled why the CSS additions keep reset ...

The arrows on the Slick Slider appear cropped on ios devices like the iphone 8, however, they display perfectly when tested on Chrome and Firefox

I am currently facing an issue with my slick slider setup at https://www.labtag.com/shop/product/clear-cryogenic-labels-for-frozen-vials-1-75-x-1-aha-224/. It is configured for 4 slides on desktop and 2 slides on mobile devices (768px breakpoint). The pr ...

Why does the script source direct me to localhost:5000?

https://i.sstatic.net/X0TKs.png Hello fellow developers! I've encountered an issue while attempting to keep my javascript file separate from my .ejs files. Despite pointing my script tag to "../client/index.js," which is the correct location of the f ...

Creating a PHP and AJAX script that tracks and stores user's screen resolutions

I'm currently working on a code that captures the screen sizes of users and then redirects them to another website. However, I seem to be facing an issue as the code is not functioning properly. Despite multiple syntax checks, I can't pinpoint wh ...

Assistance required for posting Jquery files

Attempted to upload a file using jQuery with a hidden form and an Input element, triggering the file browser with jQuery. The changed input is then sent to a PHP server script. Encountered an issue where submitting the form with $("form1").submit(); worke ...

Clicking "View All" will reset the glossary items

Struggling to reset and display all glossary terms when clicking "View All". Any assistance with this issue would be highly appreciated. I'm also exploring the possibility of hiding navigation letters if there are no entries starting with that specif ...

Is there a way to define the class for a <dt> element within a Zend_Form configuration?

Is it possible to set a specific width for a group of < dt > elements within a Zend_Form by applying a class? I need the end result to look like this: <dt id="name-label" class="xyz" > <label class="required" for="name">Name ...

A new WordPress plugin that includes a custom designed stylesheet featuring a hidden display property within a specific

After reloading the page, my plugin displays a constructed stylesheet. However, when I access it for the first time or reload with ctrl + f5, the constructed stylesheet does not appear. I have meticulously searched through all of the plugin code looking f ...

The designated <input type=“text” maxlength=“4”> field must not include commas or periods when determining the character limit

In the input field, there are numbers and special characters like commas and dots. When calculating the maxLength of the field, I want to ignore special characters. I do not want to restrict special characters. The expected output should be: 1,234 (Total ...

Customize your QTabBar icons with Qt Stylesheet: Adjusting the icon mode

I am currently working on customizing a QTabBar with a stylesheet in my tool. I use QIcon to create icons for the tabs, allowing me to set different modes such as normal, disabled, and selected. The challenge I am facing is trying to apply a global stylesh ...

What are the pros and cons of embedding background image data into CSS as Base64?

While examining the source code of a greasemonkey userscript, I came across some interesting CSS: .even { background: #fff url(data:image/gif;base64,R0lGODlhBgASALMAAOfn5+rq6uvr6+zs7O7u7vHx8fPz8/b29vj4+P39/f///wAAAAAAAAAAAAAAAAAAACwAAAAABgASAAAIMAAVCBxIsK ...

Getting user input with JQuery and dynamically updating CSS properties

<img src="purplemoon.png" alt="Purple moon" id="moon-photo" /> <table> <tr> <th colspan="4">Control panel</th> </tr> <tr> <td> <!-- attempting to retrieve value from the input field ...

Implementing CKEditor instances within AngularJS Controller scopes

Greetings everyone, Recently, I have been exploring Angular JS. Everything is going smoothly - my controllers, services, and so on. However, when attempting to make a Div editable that is within the ng-controller, the ckeditor toolbar fails to appear. On ...

Tips for preventing font distortions caused by transform-rotation

I have implemented the transform-rotation property in CSS to rotate a div and use it as fixed Facebook and Twitter links on my webpage. However, I have noticed that this rotation is distorting my current text font. When I remove the rotation, the text retu ...