What technique is used in this CSS code to give each line of text a unique horizontal color gradient?

I stumbled upon a fascinating jsfiddle that can create color gradients on specific lines of text. Each line has a unique two-color gradient applied in a consistent pattern: Black to Red, Red to Black, Black to Blue, and Blue to Black.

Despite its intriguing result, I must admit that I'm clueless about how this magic works. Can someone unravel the mystery for me?

If you're curious, here's the link to the mesmerizing jsfiddle:

https://jsfiddle.net/cers/o9s70yjq/2/

And if you want to dive into the enchanting code behind it, here you go:

HTML

<p><em>Nobody is perfect.</em> At times we have difficulty managing our finances, we don’t always take our medications as planned, and sometimes we don’t perform up to par at work. However, research shows that people experience these problems to different degrees. Across financial strata, research reveals that the financially less well-off engage in these behaviors more often than those who are financially stable (1). These behaviors are particularly concerning, because, for those with limited financial resources, they can lead to poverty as well as perpetuate it.</p>

<p>In their article, “<a href="http://www.sciencemag.org/content/341/6149/976">Poverty Impedes Cognitive Function</a>,” which appears in the latest issue of Science, University of Warwick Professor Anandi Mani and several other social scientists (2) suggest poverty, and the ever-present concerns that come with it, places an undue burden on an individual’s limited mental resources. Compared with those who are free from poverty, this burden leaves those in poverty with fewer cognitive resources with which to make choices and take action. Mani et al. write, the poor “are less capable not because of inherent traits, but because the very context of poverty imposes load and impedes cognitive capacity.”</p>

<p>However, it is important to note that their explanation is not limited to the traditional populations of poverty, defined by a specific income level or ability to access basic human needs. The authors define poverty “broadly as the gap between one’s needs and the resources available to fulfill them.” That is, people in poverty are those who feel “poor,” who feel they have less than they need.</p>

Ready to explore the enigmatic CSS that powers this mystical effect? Here's a glimpse:

p {
  margin: 0 0 1.5em 0;
  line-height: 1.5em;
  padding: 0;
  background: url(data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%...*CSS code continues*
}

em {
  font-weight: bolder;
  font-style: normal;
}

a {
  text-decoration-color: black;
}

Answer №1

One of the clever tricks used in this jsfiddle is:

Firstly, setting a background to the <p>, which means the red and blue colors are coming from the background-image property.

https://i.stack.imgur.com/xkPGa.png

Secondly, applying the background-clip CSS property to the text, which only applies the background-image to the text within the <p> tag.

Lastly, using the -webkit-text-fill-color property to make the text fill color transparent.

The end result can be seen below:

p {
        margin: 0 0 1.5em 0;
        line-height: 1.5em;
        padding: 0;
        background: url(data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%02%00%00%00%3C%08%02%00%00%00%C3%A4%FC%AE%00%00%...
      

          <p><em>Nobody is perfect.</em> At times we have difficulty managing our finances...</p>

          <p>In their article, “<a href="http://www.sciencemag.org/content/341/6149/976">Poverty Impedes Cognitive Function</a>,” which appears in the latest issue of Science...</p>

          <p>However, it is important to note that their explanation is not limited to the traditional populations of poverty...</p>
        
      

Answer №2

To enhance clarity, consider removing the -webkit-background-clip rule. This particular rule instructs the browser to apply background definition only to the pixels filled by rendered text rather than all pixels in the designated area. Removing this style can significantly improve visibility and appearance.

Take a look at how the result changes without this rule:

https://i.stack.imgur.com/BDQAi.png

Another technique is using an inlined background image that contains the gradient within itself. Here's an example:

https://i.stack.imgur.com/xkPGa.png

If you resize the image, the effect will remain consistent:

https://i.stack.imgur.com/rjpMQ.png

The remaining styles are designed to ensure that the background size adequately covers the entire paragraph.

Answer №3

The background:url(...) property is used to set the image with a gradient effect. A similar result can be achieved using the background:linear-gradient property.

The -webkit-background-clip: text property determines how far the background extends within an element.

The

-webkit-text-fill-color:transparent
property specifies the color used to fill characters of text.

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

The website does not display properly on larger screens, while showing a scrollbar on smaller screens

I'm encountering an issue with my website where I can't seem to find a solution no matter what I try. My goal is to ensure that my website looks consistent across all computer screen sizes. However, when viewed on larger screens, there's a ...

Using an HTML5 image icon as an input placeholder

Is there a way to incorporate an image icon into an input placeholder and have it disappear when the user starts typing, across all browsers? In trying to achieve this, I successfully implemented a solution for webkit (Safari+Chrome) using ::-webkit-input ...

On iPhone, links located under a fixed-positioned div can be easily clicked

I am facing an issue on our mobile website where the footer with fixed positioning overlaps with a scrolling feed underneath it, causing the links in the feed to be clickable from the footer area. Footer CSS: .footer { position: fixed; bottom: 0; right: ...

What could be causing the unpredictable behavior of my Material UI Tabs component, where it seems to be overriding other

I am facing an issue with the styling of tabs in my React app dashboard that uses Material UI Tabs. Specifically, I have a tab in the dashboard that opens a modal for adding new users to the system. The modal itself also contains tabs, but no matter how I ...

Optimizing CSS for Landscape Letter Print Alignment

When attempting to print in letter size and landscape orientation, the two divs fail to align properly. However, when using "@size a4 landscape" for printing, the alignment issue is resolved. What could be missing from my print CSS that would allow them to ...

Maintain the expanded sub-menu when the mouse leaves the area, but a sub-option has been

Implementing a side menu with parent and child options that dynamically display content in the main div on the right when a child option is selected. The parent options are initially shown upon page load, and the child options appear when the mouse hovers ...

How come padding-bottom isn't effective in increasing the position of an element?

My goal is to adjust the position of the orange plus sign (located in the lower right corner of the screen) so that it aligns better with the other icons, directly below them. I've tried using padding-bottom, but it doesn't seem to work, while pa ...

Manipulating CSS rules using JavaScript/jQuery

My goal is to create a function that generates a grid based on table data. While the function itself seems to be working, I am encountering an issue where the classes applied are not resulting in any style changes. Below is a snippet of my function: $(doc ...

``I am experiencing issues with the Ajax Loader Gif not functioning properly in both IE

I am brand new to using ajax and attempting to display a loading gif while my page loads. Interestingly, it works perfectly in Firefox, but I cannot get it to show up in Chrome or IE. I have a feeling that I am missing something simple. The code I am using ...

How can I trigger the execution of textarea HTML code with an onClick event using JavaScript?

When my fingers are lifted from the keyboard, some code will be automatically executed. I want to trigger this function when I click a button (onclick) instead of using onkeyup. <div id="result"></div> <textarea ...

What is the best way to ensure that a piece of content remains stationary while the neighboring content shifts in position?

Hello there! I'm currently working on my very first HTML and CSS project, and one of the tasks involves aligning 4 icons side by side horizontally. The goal is to have the icons "grow" a bit when the mouse hovers over them. However, I've encounte ...

FlexNav excluding

I have implemented FlexNav for my website navigation. The demo I used sets the width of top-level menu items to 20%, which works well with five menu items. .flexnav li { . . . width: 20%; } However, in my menu, the text lengths of the top ...

How about this: "Leveraging the power of #IEroot to achieve a

Has anyone successfully implemented the IE CSS hack #IEroot? I came across it while reading this informative article, but unfortunately, it doesn't seem to be working for me. I'm currently trying to resolve an inline styling bug in order to achi ...

Steps for incorporating error messages in jQuery Validator:1. Begin by including the

I am currently facing an issue with displaying error messages on my "contact me" form. Although I have successfully implemented validation, I am struggling to comprehend how the errorPlacement function should be utilized. Could someone provide me with a ...

Pressing the reset button will restore the table to its original

As a new React developer with experience mainly in hooks, I have been struggling to find a good example involving hooks. Currently, I am working on implementing an antd table with search functionality. My question is, when a user types something into the ...

Is there a way to override the padding of a container?

Working with Wordpress for the first time has been quite a challenge. Adjusting to everything is causing me some major headaches. So, I've got this container div and added a lazy 10px padding. However, for the theme I'm attempting to develop, I ...

The Jquery navigation and image display features are experiencing technical difficulties in Internet Explorer

My website is currently in the development stage and functions well on all browsers except for IE10. Interestingly, both the menu bar and photo gallery, which rely on Jquery, are not functioning properly in IE10. Below is the code snippet: <script ty ...

What is the method to create a link that doesn't take up the entire page?

As a beginner in HTML, I have been working on a website for fun and facing an issue with buttons. When I add a button, the entire area becomes clickable, even if there is empty space around it. This means that you can click on the left or right side of the ...

Looking for a solution to toggle the visibility of a div based on whether search results are found or not using JavaScript

Running this code <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Searc ...

"Effortless CSS Formatting in VS Code - A Guide to Automatic Styling

Recently started using VS code and I'm on the lookout for a solution to automatically format CSS whenever I save my work. Strangely, my searches haven't led me to any reliable extensions or clear guides on how to achieve auto formatting in VS cod ...