How could I make only the part of the text on the red div white? (so top stays dark grey, but bottom part white)
Asked
Active
Viewed 47 times
-5
-
2You've been on StackOverflow long enough to know that providing a screenshot usually does not constitute enough information for proper troubleshooting. Please share your markup and CSS, or ideally an MCVE. – Terry Mar 24 '17 at 09:47
-
You want to change color of text? – exoslav Mar 24 '17 at 09:48
-
so you want to have 2 colors for the same text ? – Ahmed Eid Mar 24 '17 at 09:48
-
linear gradient on text – Denis Tsoi Mar 24 '17 at 09:49
-
Try this: https://css-tricks.com/snippets/css/gradient-text/ – Aasim Hussain Khan Mar 24 '17 at 09:51
-
Somewhat relevant: http://stackoverflow.com/questions/16981763/invert-css-font-color-depending-on-background-color and https://css-tricks.com/reverse-text-color-mix-blend-mode/ – Terry Mar 24 '17 at 09:54
1 Answers
2
You could do it something like this
.box {
height: 60px;
line-height: 60px;
position: relative;
text-align: center;
width: 80px;
}
.box:before {
content: '';
display: block;
position: absolute;
background: red;
left: 0;
right: 0;
height: 30px;
bottom: 0;
}
span {
color: #fff;
position: absolute;
left: 0;
right: 0;
height: 30px;
line-height: 0;
bottom: 0;
line-height: 0;
overflow: hidden
}
<div class="box">
60%
<span>
60%
</span>
</div>
Update: Here is another code. Now you just need to change the height of :before and :after
pseudo elements which is commonly defined and you need to change it at just one place. Just change the value from 50% to any value that you want. I hope it helps:
.box {
align-items: center;
display: flex;
height: 60px;
justify-content: center;
position: relative;
visibility: hidden;
width: 80px;
}
.box:before,
.box:after {
bottom: 0;
content: attr(data-value);
display: block;
height: 50%;
left: 0;
line-height: 0;
position: absolute;
right: 0;
text-align: center;
visibility: visible;
}
.box:before {
color: #000;
z-index: 2;
}
.box:after {
background: red;
color: #fff;
overflow: hidden;
z-index: 3;
}
<div class="box" data-value="60%">
60%
</div>

Simrandeep Singh
- 569
- 3
- 15
-
This works find, but only for the combination 60px and 30px. If you change the 30px to e.g. 25px or 20px, then the white and black parts of the text no longer align... – Danny Mar 24 '17 at 10:59
-
Other values will have to be adjusted accordingly. I will try to modify the code for you so it remains perfect for all the values. – Simrandeep Singh Mar 24 '17 at 11:01
-
What I'm trying to achieve is to be able to set the height of the span to e.g. 30% or 50%, and have everything changed dynamically... – Danny Mar 24 '17 at 11:03
-