2

I have a simple layout consisting of three full height divs and a fixed header bar.

body,html {
  padding:0;
  margin:0;
}

.header {
  position:fixed;
  width:100%;
  height:50px;
  color:white;
  margin:20px;
}

.section1 {
  background:black;
  height:100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color:white;
}

.section2 {
  background:white;
  height:100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.section3 {
  background:black;
  height:100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color:white;
}
<div class="header">
  Header Content
</div>
<div class="wrapper">
  <div class="section1">
    Section One Content
  </div>
  <div class="section2">
    Section Two Content
  </div>
  <div class="section3">
    Section Three Content
  </div>
</div>

I am trying to make the header bar text colour change to black when it rolls over the section with the white background.

I have seen a couple of jQuery plugins which should work to achieve this but I am trying to avoid any unnecessary scripts.

Would the CSS mix-blend-mode function allow me to achieve this? Does anybody have an example they can point me to of something similar being achieved?

לבני מלכה
  • 15,925
  • 2
  • 23
  • 47
fightstarr20
  • 11,682
  • 40
  • 154
  • 278

2 Answers2

7

You can use mix-blend-mode:difference

body,
html {
  padding: 0;
  margin: 0;
}

.header {
  position: fixed;
  width: 100%;
  height: 50px;
  margin: 20px;
  color:#fff;
  mix-blend-mode: difference;
}

.section1,
.section2,
.section3{
  background: black;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.section2 {
  background: white;
  color:#000;
}
<div class="header">
  Header Content
</div>
<div class="wrapper">
  <div class="section1">
    Section One Content
  </div>
  <div class="section2">
    Section Two Content
  </div>
  <div class="section3">
    Section Three Content
  </div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Worth noting that IE and Edge doesn't currently support this so you'll require a fallback of some kind if you want this to work in those browsers. – Hidden Hobbes Aug 01 '18 at 10:18
  • @HiddenHobbes it's not a surprise, such things never and will never work on IE and edge and honestly I don't care ... if you want to use IE or Edge then don't use fancy things or make things complicated with JS – Temani Afif Aug 01 '18 at 10:21
  • It's not a surprise but it is necessary to consider given the users visiting the site may use those browsers. – Hidden Hobbes Aug 01 '18 at 10:23
  • According this answer:https://stackoverflow.com/questions/32613896/how-to-hack-unsupported-mix-blend-mode-css-property the OP can use some diffrentt `color`(as:`grey` and ect) if `mix-blend-mode` not support in browser – לבני מלכה Aug 01 '18 at 10:25
  • Thanks all, perfect example and super simple! – fightstarr20 Aug 01 '18 at 11:38
0

You can do it with jquery, when something happens, do:

$('yourdiv1').css({"color": "black"})
$('yourdiv2').css({"background-color": "white"})

To set it to default just:

$('yourdiv1').css({"color": ""})
$('yourdiv2').css({"background-color": ""})

Also add transitions to make it look really nice.

Duma
  • 124
  • 2
  • 12