Change element color to black or white based on background color


AntunCrnja

I'm building a new website and I want to change the logo color (ie ) position: fixedbased on the background color or image .

I found an example on this site . You can see how the logo and certain elements change color based on the background.

body {
  margin: 0;
}

.logo img {
  position: fixed;
  width: 100px;
}

.black {
  background: black;
  width: 100%;
  height: 400px;
}

.yellow {
  background: yellow;
  width: 100%;
  height: 400px;
}

.red {
  background: red;
  width: 100%;
  height: 400px;
}

.imageBg {
  background: url(https://images.pexels.com/photos/2208895/pexels-photo-2208895.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
  width: 100%;
  height: 400px;
  background-size: cover;
}
<div class="logo">
  <img src="http://www.clker.com/cliparts/n/y/a/M/G/h/fdl-logo.svg">
</div>

<!-- change logo color to: black -->
<section class="yellow"></section>

<!-- change logo color to: white -->
<section class="black"></section>

<!-- change logo color to: white -->
<section class="red"></section>

<!-- change logo color to: black -->
<section class="yellow"></section>

<!-- change logo color to: white -->
<section class="imageBg"></section>

Here is my fiddle : https://jsfiddle.net/o0gnayht/

Udetetis

By using, filter:invert()you can change the logo color from black to white. If you use jquery in the onscrollevent , you can call filterbased on the position closest to the topsection

$(window).scroll(function ()
{
	var windowTop = Math.max($('body').scrollTop(), $('html').scrollTop());
	$('section').each(function (index)
	{
		if (windowTop > ($(this).position().top - 100))
		{
			if ($(this).hasClass('yellow'))
			{
				$('.logo img').css('filter', 'invert(0)');
			}
			else
			{
				$('.logo img').css('filter', 'invert(1)');
			}
		}
	});
}).scroll();
body {
  margin: 0;
}

.logo img {
  position: fixed;
  width: 100px;
}

.black {
  background: black;
  width: 100%;
  height: 400px;
}

.yellow {
  background: yellow;
  width: 100%;
  height: 400px;
}

.red {
  background: red;
  width: 100%;
  height: 400px;
}

.imageBg {
  background: url(https://images.pexels.com/photos/2208895/pexels-photo-2208895.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
  width: 100%;
  height: 400px;
  background-size: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="logo">
  <img src="http://www.clker.com/cliparts/n/y/a/M/G/h/fdl-logo.svg">
</div>

<!-- change logo color to: black -->
<section class="yellow"></section>

<!-- change logo color to: white -->
<section class="black"></section>

<!-- change logo color to: white -->
<section class="red"></section>

<!-- change logo color to: black -->
<section class="yellow"></section>

<!-- change logo color to: white -->
<section class="imageBg"></section>

Related


Change element color to black or white based on background color

AntunCrnja I'm building a new website and I want to change the logo color (ie ) position: fixedbased on the background color or image . I found an example on this site . You can see how the logo and certain elements change color based on the background. body {

Change element color to black or white based on background color

AntunCrnja I'm building a new website and I want to change the logo color (ie ) position: fixedbased on the background color or image . I found an example on this site . You can see how the logo and certain elements change color based on the background. body {

Change element color to black or white based on background color

AntunCrnja I'm building a new website and I want to change the logo color (ie ) position: fixedbased on the background color or image . I found an example on this site . You can see how the logo and certain elements change color based on the background. body {

Change the white background to a specific color

hadesfv Hello, I'm trying to add a colored background to a black and white photo, first convert the pixels to white or black, then replace the white with a specific color (148, 105, 39), then save it. Here is the image I am working on This is my function so fa

Change the white background to a specific color

hadesfv Hello, I'm trying to add a colored background to a black and white photo, first convert the pixels to white or black, then replace the white with a specific color (148, 105, 39), then save it. Here is the image I am working on This is my function so fa

Kiwi: Change the background color to white

James L I want to have an app with black buttons and labels, with white text, therefore, I want to have a space to separate the widgets. I guess in order to do this it is necessary to convert the background from the default color (black) to white. What's the b

Change the white background to a specific color

hadesfv Hello, I am trying to add a colored background to a black and white photo, first convert the pixels to white or black, then replace the white with a specific color (148, 105, 39), then save it. Here is the image I am working on This is my function so f

Change the white background to a specific color

hadesfv Hello, I'm trying to add a colored background to a black and white photo, first convert the pixels to white or black, then replace the white with a specific color (148, 105, 39), then save it. Here is the image I am working on This is my function so fa

Change the white background to a specific color

hadesfv Hello, I'm trying to add a colored background to a black and white photo, first convert the pixels to white or black, then replace the white with a specific color (148, 105, 39), then save it. Here is the image I am working on This is my function so fa

Error changing background color from white to black

Jade I am trying to change the background color from white to black. So I am trying to iterate through all the pixels and check if it is white and if so change the value to 0. But something went wrong. this is my code Mat img = imread("t.PNG"); for (int x = 0;

sjPlot - change plot line color to black/white

MKS.bln I am plotting a regression model with sjPlot plot_model(). I want to change line color from sjPlot theme (red and blue lines) to black and white or grayscale. set_theme(theme_bw())However, the appearance of the plot does not change when I utilize it (

sjPlot - change plot line color to black/white

MKS.bln I am plotting a regression model with sjPlot plot_model(). I want to change line color from sjPlot theme (red and blue lines) to black and white or grayscale. set_theme(theme_bw())However, the appearance of the plot does not change when I utilize it (

sjPlot - change plot line color to black/white

MKS.bln I am plotting a regression model with sjPlot plot_model(). I want to change line color from sjPlot theme (red and blue lines) to black and white or grayscale. set_theme(theme_bw())However, the appearance of the plot does not change when I utilize it (

How to change rxvt color to white on black?

Nickolai Leschov By default, on Ubuntu, the rxvt terminal is black on white. How can I change it to white on black? I am running Lubuntu 16.04 LTS Terence Note: Updated answer to include desktops that are not loaded .Xresourcesor archived by default , like GNO

sjPlot - change plot line color to black/white

MKS.bln I am plotting a regression model with sjPlot plot_model(). I want to change line color from sjPlot theme (red and blue lines) to black and white or grayscale. set_theme(theme_bw())However, the appearance of the plot does not change when I utilize it (

How to change rxvt color to white on black?

Nickolai Leschov By default, on Ubuntu, the rxvt terminal is black on white. How can I change it to white on black? I am running Lubuntu 16.04 LTS Terence Note: Updated answer to include desktops that are not loaded .Xresourcesor archived by default , like GNO

Black and white color bars

username I usually use matplotlib's "quake" colorbar. For the needs of the publication, I have to use black and white colors. I want to keep the same aspect as "quake" (i.e. the blackest value of max min and max) as shown in the picture: How do I do the conver

Black and white color bars

username I usually use matplotlib's "quake" colorbar. For the needs of the publication, I have to use black and white colors. I want to keep the same aspect as "quake" (i.e. the blackest value of max min and max) as shown in the picture: How do I do the conver

Black and white color bars

username I usually use matplotlib's "quake" colorbar. For the needs of the publication, I have to use black and white colors. I want to keep the same aspect as "quake" (i.e. the blackest value of max min and max) as shown in the picture: How do I do the conver

Black and white plus color

Lake When red is mixed with green, I get yellow as expected. RGB for Red: [255, 0, 0] RGB for Green: [0, 255, 0] Result: [255, 255, 0] But when white is mixed with black I should get normal grey but I get white. Should I not be greyed out? RGB for B

Black and white color bars

username I usually use matplotlib's "quake" colorbar. For the needs of the publication, I have to use black and white colors. I want to keep the same aspect as "quake" (i.e. the blackest value of max min and max) as shown in the picture: How do I do the conver