Is it possible to dynamically change the text color based on the background color using CSS, or JS if it's not possible with CSS? I have a image that comes under the text. My text color is white, but the image has a white shape on. And when text goes above the white shape, I need to change the text color in a darker color.
是否可以使用CSS动态更改基于背景颜色的文本颜色,或者如果CSS无法使用JS,则可以动态更改文本颜色?我有一个文字下面的图像。我的文字颜色为白色,但图像上有白色。当文本超出白色形状时,我需要更深颜色的文本颜色。
HTML:
HTML:
<div class="biography">
<img src="http://s16.postimg.org/c09zw94jp/author_photo.png" alt="author" class="bio-img">
<div class="biography-text">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit <br>
Vestibulum pellentesque auctor quam a sollicitudin.<br>
Pellentesque accumsan a sem eget dictum.
</p>
<p>
Morbi dictum lorem tortor, id consequat libero gravida ut.<br>
Nullam dictum sed massa et bibendum.
</p>
<p>Praesent sed dui mattis, dictum urna sit amet, imperdiet purus.</p>
<p> Suspendisse potenti.</p>
</div>
</div>
CSS:
CSS:
body {
background-color: #7ccbe6;
}
.biography {
min-height: 410px;
}
.biography .biography-text {
position: relative;
margin-top: -420px;
padding: 82px 130px 0 380px;
z-index: 1;
}
.biography-text {
color: #fff;
}
Here's a Codepen to see the better picture.
这是一个Codepen,可以看到更好的图片。
4 个解决方案
#1
3
Probably you need this:
可能你需要这个:
Change text color based on brightness of the covered background area?
根据覆盖的背景区域的亮度更改文本颜色?
var rgb = [255, 0, 0];
setInterval(function() {
var c = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
var o = Math.round(((parseInt(rgb[0]) * 299) + (parseInt(rgb[1]) * 587) + (parseInt(rgb[2]) * 114)) / 1000);
(o > 125) ? $('#bg').css('color', 'black'): $('#bg').css('color', 'white'); //http://www.w3.org/TR/AERT#color-contrast
$('#bg').css('background-color', c);
rgb[0] = Math.round(Math.random() * 255);
rgb[1] = Math.round(Math.random() * 255);
rgb[2] = Math.round(Math.random() * 255);
}, 1000);
的jsfiddle
Updated:
更新:
Use this example on JSFiddle, just change background color
在JSFiddle上使用此示例,只需更改背景颜色
#2
2
I do realize you are looking for a JS solution. However, if anyone wants a css solution here is a solution http://codepen.io/hellouniverse/pen/JNwvyL
我确实意识到你正在寻找一个JS解决方案。但是,如果有人想要一个css解决方案,这里有一个解决方案http://codepen.io/hellouniverse/pen/JNwvyL
$bg-color: black; // This can change
$white : #fff;
$black : #000;
@function set-text-color($color) {
@if (lightness( $color ) > 50) {
@return $black; // Lighter color, return black
}
@else {
@return $white; // Darker color, return white
}
}
.text--color {
background: $bg-color;
color: set-text-color($bg-color);
}
// this is here just for demon purpose
.text {
padding: 10px;
text-align: center;
}
<p class="text text--color">I change in color</p>
#3
1
For this kind of problem I'd use a pure CSS solution to outline the text...
对于这类问题,我会使用纯CSS解决方案来概述文本......
.biography-text {
color: #fff;
font-weight: bold;
letter-spacing: 2px;
text-shadow: 1px 1px 0 #333,
-1px -1px 0 #333,
1px -1px 0 #333,
-1px 1px 0 #333,
2px 2px 5px rgba(0,0,0,0.65);
}
Change the letter-spacing
and the final directive of the text-shadow
property according to your needs.
根据您的需要更改text-shadow属性的字母间距和最终指令。
#4
0
There are some ways to achieve this. You could render the image on a (hidden) canvas and parse the image colors to know which color the text should be. You could also do the same with php and pre process the image color information.
有一些方法可以实现这一目标。您可以在(隐藏)画布上渲染图像并解析图像颜色以了解文本应该是哪种颜色。您也可以使用php执行相同操作并预处理图像颜色信息。
I also found this JavaScript library that could help you: http://www.kennethcachia.com/background-check/. (You could wrap each letter in a span element to determine the right color to use per letter.)
我还发现这个JavaScript库可以帮到你:http://www.kennethcachia.com/background-check/。 (您可以在span元素中包装每个字母,以确定每个字母使用的正确颜色。)
#1
3
Probably you need this:
可能你需要这个:
Change text color based on brightness of the covered background area?
根据覆盖的背景区域的亮度更改文本颜色?
var rgb = [255, 0, 0];
setInterval(function() {
var c = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
var o = Math.round(((parseInt(rgb[0]) * 299) + (parseInt(rgb[1]) * 587) + (parseInt(rgb[2]) * 114)) / 1000);
(o > 125) ? $('#bg').css('color', 'black'): $('#bg').css('color', 'white'); //http://www.w3.org/TR/AERT#color-contrast
$('#bg').css('background-color', c);
rgb[0] = Math.round(Math.random() * 255);
rgb[1] = Math.round(Math.random() * 255);
rgb[2] = Math.round(Math.random() * 255);
}, 1000);
的jsfiddle
Updated:
更新:
Use this example on JSFiddle, just change background color
在JSFiddle上使用此示例,只需更改背景颜色
#2
2
I do realize you are looking for a JS solution. However, if anyone wants a css solution here is a solution http://codepen.io/hellouniverse/pen/JNwvyL
我确实意识到你正在寻找一个JS解决方案。但是,如果有人想要一个css解决方案,这里有一个解决方案http://codepen.io/hellouniverse/pen/JNwvyL
$bg-color: black; // This can change
$white : #fff;
$black : #000;
@function set-text-color($color) {
@if (lightness( $color ) > 50) {
@return $black; // Lighter color, return black
}
@else {
@return $white; // Darker color, return white
}
}
.text--color {
background: $bg-color;
color: set-text-color($bg-color);
}
// this is here just for demon purpose
.text {
padding: 10px;
text-align: center;
}
<p class="text text--color">I change in color</p>
#3
1
For this kind of problem I'd use a pure CSS solution to outline the text...
对于这类问题,我会使用纯CSS解决方案来概述文本......
.biography-text {
color: #fff;
font-weight: bold;
letter-spacing: 2px;
text-shadow: 1px 1px 0 #333,
-1px -1px 0 #333,
1px -1px 0 #333,
-1px 1px 0 #333,
2px 2px 5px rgba(0,0,0,0.65);
}
Change the letter-spacing
and the final directive of the text-shadow
property according to your needs.
根据您的需要更改text-shadow属性的字母间距和最终指令。
#4
0
There are some ways to achieve this. You could render the image on a (hidden) canvas and parse the image colors to know which color the text should be. You could also do the same with php and pre process the image color information.
有一些方法可以实现这一目标。您可以在(隐藏)画布上渲染图像并解析图像颜色以了解文本应该是哪种颜色。您也可以使用php执行相同操作并预处理图像颜色信息。
I also found this JavaScript library that could help you: http://www.kennethcachia.com/background-check/. (You could wrap each letter in a span element to determine the right color to use per letter.)
我还发现这个JavaScript库可以帮到你:http://www.kennethcachia.com/background-check/。 (您可以在span元素中包装每个字母,以确定每个字母使用的正确颜色。)