Switch up the button hue as you scroll for a fresh look

Attempting to achieve: CSS change color on scroll / cut text - overflow z-index This code snippet uses the clip property to change the color of a button when scrolling. The goal is to make it change color as you move from the header to the body, but it's not working for me.

body { 
background:#F7FE2E;
margin: 0;
}

#header{ 
padding:200px 50px;
background-color: #6495ED;
}

/*HERE IS THE HEADER AND BODY CLIP*/
#header, 
#content {
clip: rect(auto, auto, auto, auto);
box-sizing: border-box;
}

/********** MENU ***********/

.menu{
top: 15px;
right: 40px;
font-size: 16px;
position:fixed;
}

.black {
   color: #000000;
}

.white {
   color: #FFFFFF;
}
<div id="wrapper">

  <div id="header">

<!--HERE IS THE FIXED MENU WHITE-->
   <div class="menu white">MENU</div>

</div>

<div id="content"> 

    <!--HERE IS THE FIXED MENU BLACK-->
    <div class="menu black">MENU</div>

    CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>...

    </div>

</div> 

Why isn't this working as expected?

Answer №1

Here is the solution you were looking for, although there are a few key guidelines that were overlooked. Please review the code snippet below for detailed explanations...

body {
  background: some-other-color; /* clipping doesn't utilize this particular color => */
  margin: 0;
}
#header {
  top: 0; /* [OPTIONAL] additional */
  padding: 200px 50px;
  background-color: #6495ED;
}


/* [MANDATORY] additional */
#content {
  top: 400px;                /* (2 x 200px) px, vw, %, choose accordingly*/
  background-color: #F7FE2E; /* => however, this bg-color is essential to blend */
}
/*******************/


/*HEADER AND BODY CLIP ARE DEFINED HERE*/
#header,
#content {
  position: absolute; /* [MANDATORY] necessary, clip requires absolute positioning */
  width: 100%;        /* [MANDATORY] must have, and width is crucial for positioning */
  clip: rect(auto, auto, auto, auto);
  box-sizing: border-box;
}
/********** MENU ***********/
.menu {
  top: 15px;
  right: 40px;
  font-size: 16px;
  position: fixed;
}
.black {
  color: #000000;
}
.white {
  color: #FFFFFF;
}
<div id="header">

  <!--FIXED WHITE MENU IS SHOWN HERE-->
  <div class="menu white">MENU</div>

</div>

<div id="content">

  <!--FIXED BLACK MENU APPEARS BELOW-->
  <div class="menu black">MENU</div>

  CONTENT
  <br>CONTENT
  <br>... many lines of content ...
  <br>CONTENT

</div>

</div>

Answer №2

If you want to change the color of the menu when scrolling, consider using jquery as shown below:

$(window).scroll(function() {
  if ($(this).scrollTop() > 380) {
    $(".menu").removeClass("white").addClass("black");
  } else {
    $(".menu").removeClass("black").addClass("white");
  }
});
body {
  background: #F7FE2E;
  margin: 0;
}
#header {
  padding: 200px 50px;
  background-color: #6495ED;
}
/*HERE IS THE HEADER AND BODY CLIP*/

#header,
#content {
  clip: rect(auto, auto, auto, auto);
  box-sizing: border-box;
}
/********** MENU ***********/

.menu {
  top: 15px;
  right: 40px;
  font-size: 16px;
  position: fixed;
}
.black {
  color: #000000;
}
.white {
  color: #FFFFFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="wrapper">

  <div id="header">

<!--HERE IS THE FIXED MENU WHITE-->
   <div class="menu white">MENU</div>

</div>

<div id="content"> 

    CONTENT<br>CONTENT<br>CONTENT<br>... (Content continues)

    </div>

</div>

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

color settings on a search icon as a background

I'm struggling to make the background color of my search glyph fill the entire box. I want it to resemble this image. However, at the moment, it looks more like this. I can't figure out what's causing the issue. Below is the HTML code I&a ...

Using a new hue for a hyperlink

I currently have the following CSS styling for my links: DIV#tt { background: #fff; color: #333; margin: 0 0 15px; position: relative; padding: 1px; } DIV#tt A, DIV#tt SPAN.link { color: #990000; } These styles work well and chang ...

Unlock the power of Angular pipes in window.open with URL parameters!

<div class="member-img" onclick="window.open(childEpisode.File_URL | fullPath)"> </div> The fullPath function concatenates the domain part to the relative URL stored in file_URL. However, there seems to be an issue as it i ...

Checkbox fails to trigger onChange when the checked value is controlled by state management

Currently, I am working with a material-ui Table and have been in the process of implementing multi-select functionality on it. The behavior I am aiming for my multi select to exhibit is as follows: Checkboxes should only appear at the beginning of a ro ...

Highchart mortgage calculator for accurate financial planning

I am looking to create a dynamic high chart similar to the one on bankrate.com, showcasing principal, balance, and interest over various years. Despite my efforts, I have been struggling to organize the data by year as demonstrated in the example above: ...

jQuery functions as expected in a test environment, but encounters issues when implemented on a live website

Recently, I implemented a piece of code that enables content to be copied from one div to another when a button is clicked: <a id="btn123">CLICK ME</a> <div id="test1"></div> <div id="test2"> <h1>Heading</h1> < ...

Implementing Hover Effects on Images in WordPress

I'm searching for the best way to create an Image Transparency - Hover Effect in WordPress. I have a set of company logos that I want to fade until hovered over. Is there a simple solution out there? Any recommended code snippets or CSS suggestions wo ...

Getting to a different page while not maintaining scroll position using React

I'm currently working on a project using Next/React. I encountered an issue where, upon clicking a button that is supposed to navigate to another page, the scroll position is retained, causing the new page to load with the content X pixels down from t ...

Adjust the dimensions of an SVG element using CSS within an li element's ::before pseudo-element

I'm working on a list where SVGs are loaded as list items. How can I adjust the height and width of the ::before pseudo-element to be 30px x 30px? Setting the height and width directly didn't work for me. Can someone assist with this? <div cl ...

I'm having trouble locating the source of the popstate loop that is generating numerous history entries

I am currently working on a project to create a dynamic webpage where the content of the main div gets replaced when certain navigation links are clicked. I have successfully implemented the pushstate function to update the div content and change the URL a ...

I'm confused as to why the icon color isn't changing on the Blogger homepage for each individual user

I'm experimenting with changing the eye color based on visitor count. It works perfectly fine on static posts in my Blogger, but it fails to update the eye color properly on my homepage according to the set numeric values. Instead of displaying differ ...

HTML/CSS Selecting Tables: First, Last, and More

Is there a way to combine selector tags in order to style the first <td> inside the first <tr>? I have attempted various methods to merge these selectors, albeit unsuccessfully. I am simply seeking guidance on whether or not this is even possi ...

Error ENCOUNTERED when deploying React App to AzureThis is my own fresh

My React website is running on Azure and serving pages successfully. Although, I frequently encounter error messages in the Web App logs like this: 2021-03-01T15:01:33.957751902Z 15:01:33 0|static-page-server-8080 | [2021-03-01T15:01:33.956Z] Error while ...

What is the best way to create a menu that can be hovered over but not clicked on

This HTML code includes a dropdown menu created using Bootstrap. It contains navigation links for Home, Add User, Export, and Logout. <ul class="nav navbar-nav navbar-right"> <li <?php if($tabs=='home' or $tabs=='') ech ...

What is the best way to reset an event back to its original state once it has been clicked on again

As a newcomer to web development, I'm currently working on creating my own portfolio website. One of the features I am trying to implement is triangle bullet points that can change direction when clicked - kind of like an arrow. My idea is for them to ...

place an icon on the left side of a tag in tailwind CSS while keeping text in the center

Is there a way to align an icon to the left side of a link while keeping the text centered at the same time? <div className="max-w-screen-2xl mx-auto sm:px-6 lg:px-8"> <Link href="#" className="px-6 py-3 mt-2 fle ...

"Implemented a user-friendly header and footer that stick to the top and

My goal is to create a fixed header and footer, allowing the main content to scroll underneath while keeping the right navigation in place. To achieve this effect, I've been experimenting with different solutions. One of my attempts can be viewed her ...

Card image floating to the right using Bootstrap

I have a set of three horizontal cards, not in a card deck as it's not responsive, and I want to align images in the bottom right corner of each. Bootstrap 4 comes with a class for card-img-left which works perfectly. I've attempted using float-r ...

Open the overflow div within a table data cell

My goal is to create a drag-and-drop schedule tool similar to Fullcalendar. I have decided to use a table layout with a rectangular div inside each TD cell to represent tasks. <table onDragEnd={dragend}> <tr> <th>0</th> ...

Using Javascript to Manuever an Element via Arrow Inputs

Currently, I am in the process of developing a Tetris game with a rotating Tetris piece controlled by pressing the space bar. My next objective is to enable the movement of objects to the left and right using the arrow keys. After exploring various simila ...