This code is a front-end web component that demonstrates a dynamic background and text-inversion effect using HTML, CSS, and JavaScript. It features a navigation box with three buttons ("Default", "Red Background", and "Green Background") that allow users to switch between different themed sections on the page. Each section utilizes a split-color background gradient (divided down the middle) and showcases CSS blending techniques—specifically using mix-blend-mode and filter: invert(1) — to dynamically invert the text colors as they sit across contrasting background halves.
The code works by leveraging CSS classes to control layout and visibility. In the HTML document, the page is structured with a button container (.link_box) and three wrapper divs (.invert_wrapper) representing the black, red, and green themes, each containing headings with specific styling classes. The CSS styles these elements using a grid layout for the buttons, custom linear gradients for the backgrounds, and blending properties for the headings to create a striking visual contrast. By default, the wrapper divs are hidden (display: none), except for the one possessing an .active class.
JavaScript drives the interactivity of the page by listening for user actions. When the window loads, the script automatically applies the .active class to the first theme section, making it visible. It then loops through all the color buttons, attaching a click event listener to each. When a user clicks a button, the script removes the .active class from the currently displayed section, updates the active index, and applies the .active class to the newly selected theme wrapper, seamlessly swapping the displayed content on the screen.
To use this code, you simply need to combine the HTML, CSS, and JavaScript into a fully functioning web page. Place the HTML code inside the <body> tags of an HTML5 document, insert the CSS code inside a <style> tag in the <head> section (or link it via an external stylesheet .css file), and place the JavaScript code inside a <script> tag just before the closing </body> tag (or in an external .js file). Once opened in a browser, you can click the top navigation buttons to instantly toggle between the different color-themed sections and observe the inverted text effects.