Relative positioning can break the background-clip attribute for text elements

I am experimenting with using background-clip: text on an element that contains some child <p> elements. I wanted to enhance the design by adding some absolutely positioned :after elements to the <p> tags, covering up the text for animation effects. However, when I applied position: relative to the <p> tags, they mysteriously disappeared. It seems to be related to the interaction between background-clip and color: transparent. Is there a solution to make this work seamlessly?

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,body {
  display: flex;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
}
body {
  flex-direction: column;
}
.container {
  background: linear-gradient(to bottom, white, black);
  background-clip: text;
  -webkit-background-clip: text;
  text-align: center;
}

.container p {
  font-size: 5em;
  line-height: 1;
  font-weight: 900;
  text-transform: uppercase;
  color: transparent;
  -webkit-text-fill-color: transparent;
}
.container.relative p {
  position: relative;
}

/* .container.relative p:after {
  content: "";
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background: black;
} */
<p>The text below is set as relative but appears invisible</p>
<div class="container relative">
  <p>Some Relative</p>
  <p>Text</p>
</div>

<p>The text below is not set as relative and remains visible</p>
<div class="container">
  <p>Some</p>
  <p>Text</p>
</div>

Link to Example

Answer №1

Here is a unique concept featuring a more dynamic gradient effect without the need for position:relative.

Experience the hover effect:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,body {
  display: flex;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
}
body {
  flex-direction: column;
}
.container {
  background: linear-gradient(to bottom, white, black);
  background-clip: text;
  -webkit-background-clip: text;
  text-align: center;
}

.container p {
  font-size: 5em;
  line-height: 1;
  font-weight: 900;
  text-transform: uppercase;
  color: transparent;
  -webkit-text-fill-color: transparent;
}
.container p {
  background:linear-gradient(#000,#000);
  background-repeat:no-repeat;
  background-size:0% 100%;
  background-position:left;
  transition:1s;
}
.container p:hover {
  background-size:100% 100%;

}
<p>The text below is <strong>not</strong> relative and is visible</p>
<div class="container">
  <p>Some</p>
  <p>Text</p>
</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

If the image is not found, add a count instead of showing a new one each time

I have implemented a function to retrieve the two-letter country code: $ipaddress = $_SERVER['REMOTE_ADDR']; function ip_details($ip) { $json = file_get_contents("http://ipinfo.io/{$ip}"); $details = json_decode($json); return $detai ...

Combining the lower margin of an image with the padding of a container: a step-by-step guide

There are two divs with a 50px padding, making them 100px apart. The top div contains an image floated to the right with paragraphs wrapping around it. The requirements team requests a 20px margin below the image if text flows under it, but no margin if th ...

Creating an Angular 7 Template using HTML CSS and Javascript

I recently acquired a template that comes with HTML, CSS, and Javascript files for a static webpage. My goal is to integrate these existing files into my Angular project so I can link it with a Nodejs application to create a full-stack webpage with backend ...

Displaying the content of a modal window

I am working on a modal that needs to print only the content within it, nothing else. Here is the setup for the button inside the modal: This should not be printed... <button id="btnPrint">Print (this btn should not be printed!)</button> ...

What is the best way to style only textboxes with CSS without affecting other input types such as checkboxes?

If attribute selectors were universally supported by all browsers, we could effortlessly implement the following styling: input[type='text'] { font:bold 0.8em 'courier new',courier,monospace; } input[type='radio'] { margin:0 ...

Can flexbox elements be animated as they scroll?

Wondering if it's feasible to animate flex elements upwards when scrolled? Attempting to replicate this effect: https://codepen.io/Sergiop79/pen/bxjGEe I want to apply this to the elements below (styled in flexbox), either the entire "row" or each i ...

Activate a button utilizing jQuery keyboard functionality

Here is what I have accomplished so far: http://jsfiddle.net/qEKfg/ I have created two buttons that activate on click and resemble keyboard keys. My goal is to make them animate only when the corresponding keys (CTRL and ...

What is the best approach for scaling @material-ui Skeleton in a grid row with variable heights?

I am working on creating a grid of Avatar images with a transition state where I want to show a skeleton representation of the image. To achieve this, I am using @material-ui/lab/Skeleton. The issue I'm facing is that since my images are set to autos ...

Incorporating PHP into Dynamic Variables

I'm curious about how PHP coding can be utilized in PHP variables. Let's say, for instance, I wish to achieve something like: $Start = " IF($lala == "0"){ echo '"; $End = " '; }"; and be able to utilize it as: echo ''.$star ...

When combining Symfony, Twig, and Semantic UI, users may encounter an issue where the background image fails to display

Can someone assist me in identifying the issue with my code? I am using Symfony 2.8, Semantic UI, and Twig, attempting to set a background image on my site. The modification is being made to my base.html.twig file. I have attempted: Using different ima ...

Choose a navigation item from the list containing a nested span element

I've implemented selectnav from GitHub and it's functioning perfectly. However, my menu consists of list items with a description span inside each one, resulting in menu items structured as shown below: <li><a href="somelink.html">Ch ...

Generating dynamic anchor tags in Vue.JS

I have a JavaScript object that I want to convert into HTML elements and display it in Vue.js. So far, my approach has been to convert the object into strings representing HTML elements and then add them to the template. However, even though this method di ...

Extracting data from website tables

I've been attempting to retrieve salary information for CSU employees from a specific webpage (). Despite my efforts with the urllib2 and requests libraries, I have not been successful in extracting the table data from the webpage. My suspicion is tha ...

Why is $(window).load() being executed twice?

When using $(window).load() to resize images in a thumbnail gallery and set up a slideshow, the code seems to be running twice. This can be observed by noticing that a div is being wrapped around another div twice when inspecting the HTML on page load. Af ...

The search bar is visible on desktop screens and can be expanded on mobile devices

Check out my code snippet below. <style> #searchformfix { width: 50%; border-right: 1px solid #E5E5E5; border-left: 1px solid #E5E5E5; position: relative; background: #fff; height: 40px; display: inline-block; border: ...

Creating interactive and adaptable maps

Currently, I am working on making a Joomla website responsive. However, I have encountered an issue with the SobiPro Map. The problem arises when displaying a map (background img) and points (a link + img) over it with an absolute position. As a result, wh ...

Identifying when a page-loading or reloading event has been canceled

When the Cancel button is pressed during page load/reload, the content of the page loads partially. Is there a more effective way to address this issue rather than displaying incomplete content? For instance, consider showing a message such as "Page load ...

Generate a catalog of individual files within nested folders

I have a structure: [catalogues] [catalogue-name-1] [data_html] name-1.html - table of posted values from another form name-2.html - table of posted values from another form name-3.html - table of posted values from another form ... name-n.html - ta ...

Achieving Perfect Alignment in Website Layout using CSS and HTML

I have a customized CSS layout that was made for me, but I am no longer in contact with the original creator. I wanted to increase the size of a specific div (#rightcolumn) by 55px. To achieve this, I resized the iframe within the div and it automatically ...

Switching between Login Form and Register Form in VueJS template

Recently, I attempted to switch between the 'Login Form' and 'Register Form' using code that I found on codepen Flat HTML5/CSS3 Login Form. While the code functioned properly on its own, when integrated into a Vue Template, the form fai ...