Alter the color as the text moves beneath a see-through layer

Imagine a scenario where there is a page with text and on top of that, there is a transparent div that is smaller than the text area. Is it feasible to dynamically alter the color of the text that scrolls underneath the transparent div? Picture the transparent div as a tinted glass layer above a piece of paper with text printed on it... that's the visual effect that I am aiming for. Check out this example for reference: Partially highlighted text

Answer №1

If you're looking for a solution to this problem, there is one available, but unfortunately, not all browsers support it. One way to achieve the desired effect is by using the CSS property mix-blend-mode. Different blend modes are available for you to experiment with, and you can learn more about them in the Mozilla Developer Network article.

.box {
  position: relative;
  width: 400px;
  height: 150px;
  overflow-y: scroll;
  color: black;
}

.overlay {
  position: fixed;
  width: 80px;
  height: 80px;
  top: 50px;
  left: 100px;
  border: 2px solid red;
  background-color: blue;
  mix-blend-mode: screen;
}
<div class="box">
  <div class="overlay">
  </div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi excepturi explicabo, animi et voluptatibus molestiae earum tempora ad perspiciatis accusantium impedit voluptates. Veritatis ex, officia velit maiores provident quasi dolorem. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores, commodi. Repellat dolore id repudiandae, harum quae perferendis, illum eum perspiciatis, cupiditate autem sapiente consectetur obcaecati et incidunt! Enim voluptas repellat reprehenderit voluptates perspiciatis quam repellendus reiciendis neque sed culpa deleniti, labore rerum non esse aut architecto earum illum sit accusamus ratione inventore dicta debitis, voluptatem ipsam iure. Quas, quae recusandae! Assumenda, nihil vero iusto iure. Dolores numquam libero odio assumenda nulla repudiandae quibusdam optio, aspernatur eligendi, ex, aliquid accusantium recusandae, asperiores laboriosam nemo. Obcaecati explicabo distinctio, mollitia fuga non ducimus harum facere, eos nisi sapiente eligendi, in quis sint laudantium ullam iusto id officia consequatur. Fugit aliquam expedita facilis harum libero id quaerat placeat nobis? Officiis fugit eos sint vero repellendus cum maxime aperiam, officia illo at tempore adipisci nulla quo consequuntur et nesciunt repudiandae consequatur enim, rem modi quaerat totam. Officia nihil velit totam placeat tempora, voluptates necessitatibus suscipit eligendi praesentium cupiditate enim magnam blanditiis aut? Reiciendis quaerat fugit expedita aperiam, veritatis provident error, quasi eos dignissimos dicta est, quidem, sequi. Corporis tempore odit modi, repellendus, adipisci mollitia est rerum. Harum inventore ab praesentium optio ea quis molestiae tempore quidem aut dolores alias, officia, autem voluptates eaque illo dolorem, laboriosam nobis eos. Quam odit, atque voluptate unde molestiae pariatur a nostrum consequuntur alias veniam soluta nihil officiis libero cum impedit dignissimos vero similique ratione cupiditate et quibusdam omnis esse nulla, facilis! Magnam dolorem molestias cumque fugiat, voluptatum atque possimus perferendis inventore dolore, nihil, asperiores eligendi ab est, illo placeat dolor aliquid ex quod. Culpa porro, minus id neque asperiores cum corrupti esse, est, excepturi ratione, in ut repudiandae. Blanditiis nisi nobis voluptatum omnis doloribus illum totam, aliquam asperiores optio repudiandae provident mollitia aspernatur, sunt perspiciatis rem expedita ad maxime illo, quaerat a impedit. Voluptas quia ipsum odit sint placeat itaque totam dolorem eaque aliquam numquam, consequatur nulla fuga ipsam suscipit voluptate pariatur incidunt minus blanditiis, magni ullam nisi id a. Quisquam neque minus officia reiciendis vel vitae tempore hic labore dicta mollitia, quis rem suscipit consequatur porro corrupti ullam atque reprehenderit odio obcaecati quo, placeat dolore nemo nostrum soluta eius! Consequatur neque voluptates reprehenderit, deserunt commodi aperiam mollitia veniam quisquam deleniti fugiat. Ea omnis accusamus rerum veniam vero laudantium saepe impedit pariatur voluptas esse illum labore est quia sint, vel culpa, soluta ex? Dicta molestiae fugiat, eveniet, sit excepturi sequi accusamus debitis maiores quaerat reprehenderit similique vero enim, nesciunt!
</div>

Answer №2

To ensure full browser compatibility, one option is to replicate the element, position it absolutely, and specify to conceal overflowing content. Here's a sample code snippet to achieve this:

$.fn.glassify = function(options) {
    
    
  var $glass = $("<div class='glass'>").insertAfter(this);
  var $wrapper = $("<div class='wrapper'>").appendTo($glass);
  
  // Make a copy of the paragraph:
  $(this).clone().appendTo($wrapper);
  
  var pos = $.extend({
    top: 0,
    right: 0,
    bottom: 0,
    left: 0
  }, options );
  
  
  $glass.css('top', pos.top);
  $glass.css('right', pos.right);
  $glass.css('bottom', pos.bottom);
  $glass.css('left', pos.left);
  
  $wrapper.css('top', -pos.top);
  $wrapper.css('right', -pos.right);
  $wrapper.css('bottom', -pos.bottom);
  $wrapper.css('left', -pos.left);
};

$("p").glassify({
  
  top: 50,
  right: 100,
  bottom: 20,
  left: 200
});
.container {
  position: relative;
}

.glass {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: white;
  outline: 1px solid red;
  color: blue;
  overflow: hidden;
}

.wrapper {
  position: absolute;
}

p {
  margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris quis auctor ex. Maecenas ut nisl justo. Etiam vitae velit vitae libero congue aliquet at non ipsum. Donec pulvinar risus at pellentesque rhoncus. Vestibulum vitae nibh sit amet sem facilisis pellentesque. Phasellus condimentum pellentesque neque, at tincidunt lacus mollis tincidunt. Mauris vestibulum leo et libero lobortis pharetra. Aliquam sagittis felis molestie sapien rhoncus dignissim. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc posuere in orci sit amet ullamcorper. Fusce tempor sem eget aliquam efficitur. Suspendisse sagittis facilisis eros eget semper. Curabitur feugiat, quam eu aliquam vulputate, tellus odio scelerisque neque, a sodales est ligula in magna. Nullam vehicula eleifend dolor eget venenatis. Fusce tempus elit sit amet rutrum mattis.
  </p>
</div>

Answer №3

An economical method is to utilize hsla or rgba colors and adjust the transparency level.

background-color: hsla(48, 100%, 50%, 0.5);

However, it is important to note that the text color will be a combination of the set color and the background color of the containers above it.

While you may be seeking full transparency with only the text color changing, this approach may benefit others as well.

Check out the Fiddle

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

Scheduled Job unable to complete post request

(I am completely new to the world of JavaScript, node.js, and Heroku so I apologize in advance if my question is not very clear) I recently set up a Heroku node.js application with a scheduled task that should run every hour. The task does run as expecte ...

Trigger ng-change event for each dropdown selection made in AngularJS

Currently, I have a dropdown menu that allows users to select a report for generation. When a user picks a report from the dropdown, it generates and downloads the report for mobile viewing. By utilizing ng-change, the system only detects when a user wants ...

assign the loading screen image to a swf file

I have an HTML file that includes a 3MB SWF flash intro. I would like to display a loading preloader using jQuery or AJAX until the SWF is fully loaded. Can you please provide the code to set up this preloader? Thank you in advance. Here is the code I a ...

At which location within the script should I insert the document.title in order to update the title every xx milliseconds?

I have been working on a script that refreshes certain #id's. Additionally, I would like to update the page title, which involves some flask/jinja2. I've attempted placing document.title = {% block title %} ({{online_num}}) Online Players {% en ...

Transforming hover interactions into onclick events involves adjusting the CSS and JavaScript

Is there a way to convert this hover menu into an onclick menu? <div id='cssmenu'> <ul> <li class='has-sub'><a href='#'><span>Users</span></a> <ul> ...

What could be causing this error I'm receiving: instance versus prototype difference?

Can someone help me understand the difference between .prototype and regular instances in JavaScript? I am confused about why this code is not working and getting a type error: "undefined is not a function". I am experimenting with the Ninja() class and ...

Is the AJAX call returning JSON data?

Currently, I am utilizing AJAX technology to scrape player names and their respective ratings from a website. The approach I have chosen involves having AJAX return a JSON object which is then manipulated using jQuery to append each element of the JSON obj ...

Modify my JavaScript array by separating each element with a comma

JavaScript or jQuery is what I'm comfortable using Including an additional database table column has resulted in: 122461,4876192 Now there are 2 records So it displays like this: https://i.sstatic.net/7mjFY.png $.each(alldata.Data, function (in ...

Breaking down a jQuery array into separate JSON objects

Hi there, I am currently working on constructing a post request. Here is the format I need to send: const data = { categories: [ { id: 9 }, { id: 14 } ], } However, what I have is ["29", "23", "22&quo ...

How can I retrieve the identifier in Socket.io?

Is there a way to retrieve the unique Id from the server using socket.io? I attempted using clients[socket.id] = socket; However, I encountered an error stating: connections property is deprecated. use getconnections() method Does anyone have any sugg ...

When adding text to a React Material UI Box, it can sometimes extend beyond the boundaries

I'm currently working on a straightforward React page, but I've encountered an issue right from the start: my text is overflowing the box and I can't figure out why. Could someone take a look at my styles? Here's a picture showing the ...

The X path and CSS selector for a particular table will vary on every page

Hello, I am new to HTML and currently working on a project that involves scraping data from html tables using RSelenium. I have managed to use the following code successfully: for(i in 1:50){ remDR$navigate(URLs[i]) CPSHList[[i]] <- remDR$getPageSou ...

Is it possible to send two parameters to a JavaScript function using HTML?

Seeking help to develop an .html page where two string inputs are passed as parameters to a .js function, which then returns the longer of the two strings based on their lengths. Initially, I successfully created a functional .js script in VS CODE. Here i ...

Getting a Node Express 404 error while attempting to display an image that was uploaded using multer

I am facing an issue with uploading images using multer. Previously, everything was working perfectly fine on localhost. The images were getting uploaded and I could view them using the provided URL link in the code. However, after uploading it to a server ...

What is the best way to conceal the URL of a link leading to a file?

My website is built with C#, Kendo MVC, Razor and features a Kendo grid where one of the cells contains a hyperlink to a PDF file. The issue I'm facing is that when users click on the link, the URL is visible in the browser and can be altered to acces ...

I'm baffled as to why the page is not loading despite my best efforts. Could someone please help identify where I went wrong?

My page isn't loading properly and I can't seem to figure out what's wrong. The error message that appears is: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/deals/2015/03/first-lady/ Django 1.6 Python 2.7 ...

The AJAX call fails to refresh the secondary table in CodeIgniter

I have a situation where I need to display data from two tables - car producer and car model, pulled from a database. My goal is to filter the second table to only show cars from a specific producer when that producer is clicked in the first table. I attem ...

Update the value of a td element when a select option is changed in another td element within the same table row

I have a table set up as follows: Header1 | Header2 | Header3 | Header 4 | Header 5 Row 1 |<span>Some Text</span> | <select></select> Row 2 Row 3 . . . . Rows generated dynamically. My objecti ...

Unable to find the Popper component in Material UI

Material-UI version "@material-ui/core": "^3.7.0" I have a requirement to display Popper on hover over an element, but unfortunately, the Popper is not visible. This section serves as a container for the Popper component. import PropTypes from &apos ...

What are the steps to make a div with no content inside?

Presently, I am dealing with 2 overlapping transparent div's each containing unique buttons and functionalities in the following manner: .item1{ height:10rem; width:10rem; position:absolute; border:0.5rem solid red; background-color:trans ...