To keep a link active, it needs to remain in a pressed state until a new

I was experimenting with HTML and CSS, but I couldn't get it to function as desired.

What I am trying to achieve is that the first link/tab should appear pressed when the site is opened. It should then change to an unpressed state when another link is clicked. Additionally, there is a green line at the top which I want to change its color when a navigation tab/link is pressed simultaneously.

body {}

header {
  position: fixed;
  width: 100%;
  border-top: 5px solid #8dc63f;
  height: 120px;
  background: white;
  z-index: 1000;
  border-bottom: 2px solid #fff;
  top: 0;
}

h1 {
  font-size: 70px;
  color: red;
  padding-top: 500px;
}

h2 {
  font-size: 70px;
  color: blue;
  padding-top: 500px;
}

nav {
  position: absolute;
  right: -17px;
  bottom: 0;
}

nav ul {
  list-style: none;
}

nav ul li {
  display: block;
  float: left;
}

nav ul li a {
  font-family: 'Open Sans', sans-serif;
  margin-right: 2px;
  color: white;
  width: 100px;
  padding: 10px;
  display: block;
  text-align: center;
  position: relative;
  text-decoration: none;
}

nav ul li a:hover {
  color: #fff;
}

nav ul li a::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #8dc63f;
  z-index: -1;
  transform: skew(-40deg);
  margin-bottom: -2px;
}

nav ul li a::before {
  content: '+';
}

a:hover::after {
  background: #cbda29;
}

HTML

<header>
  <nav>
    <ul>
      <li><a href="#link1">Link1</a></li>
      <li><a href="#link2">Link2</a></li>
      <li><a href="#link3">Link3</a></li>
      <li><a href="#link4">Link4</a></li>
    </ul>
  </nav>
</header>
<div id="link1">
  <h1>Hello. I am Link1. If you see this the first link/button should stay pressed/clicked/lightgreen background until
    you click another link. The top line "border-top" should change its color as well if you click a link.
  </h1>
</div>
<div id="link2">
  <h2>Oh hello i am the second link you clicked. The first button shouldn't be clicked anymore. Now link2 should appear
    as a clicked button.
  </h2>
</div>

Answer №1

When you click the navigation link, assign it a class like "active" and remove this class from any other links that are currently considered "active".

By using the appropriate CSS styling, you can also assign the same "active" class to the header.

a.active::after {
  background-color:#cbda29; 
}
header.active {
  border-top-color:#cbda29; 
}

Revised code snippet:

$("nav > ul > li > a").click(function() {
    $("a.active").removeClass("active");
    $(this).addClass("active");
    $("header").css("border-top-color", $(this).data("color"))
});
a.active::after {
  background-color:#cbda29; 
}
header.active {
  border-top-color:#cbda29; 
}

body {} // Some additional styles

// Remaining CSS styles here
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
  <nav>
    <ul>
      <li><a href="#link1" data-color="red">Link1</a></li>
      <li><a href="#link2" data-color="blue">Link2</a></li>
      <li><a href="#link3" data-color="purple">Link3</a></li>
      <li><a href="#link4" data-color="green">Link4</a></li>
    </ul>
  </nav>
</header>
<div id="link1">
  <h1>Hello. I am Link1. If you see this the first link/button should stay pressed/clicked/lightgreen background until you click another link. The top line "border-top" should change its color as well if you click a link. </h1>
</div>
<div id="link2">
  <h2>Oh hello i am the second link you clicked. The first button shouldn't be clicked anymore. Now link2 should appear as a clicked button.</h2>
</div>

Answer №2

Upon opening the site, ensure that the first link/tab appears pressed.

To achieve a different color for the first tab upon page load, apply an active class to the list item (li) to alter its color. When clicking on menu items, remember to remove/add a class like active to the corresponding li element to update the color of the menu button accordingly.

Utilize the following JavaScript code to toggle class names:

$('nav a').on('click', function() {
   $('nav li').removeClass('active');
   $(this).parent().addClass('active');
})

$('nav a').on('click', function() {
   $('nav li').removeClass('active');
   $(this).parent().addClass('active');
   $('header').css('border-top-color', 'red');
})
body {}

header {
  position: fixed;
  width: 100%;
  border-top: 5px solid #8dc63f;
  height: 120px;
  background: white;
  z-index: 1000;
  border-bottom: 2px solid #fff;
  top: 0;
}

h1 {
  font-size: 70px;
  color: red;
  font-size: 60px;
  padding-top: 500px;
}
...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
...
<div id="link1">
...
<div id="link2">
...

Update after comment

Each button should have a distinct background color when clicked. This color should match the border color.

var prevClass = 'link1';
$('nav a').on('click', function() {
   var newClass = $(this).attr('id');

   if(prevClass) {
      $('header').removeClass(prevClass);
   }
   $('header').addClass(newClass);
   prevClass = newClass;
})
body {}
...

header.link1 nav li a#link1::after {
  background: red;
}
header.link2 nav li a#link2::after {
  background: yellow;
}
header.link3 nav li a#link3::after {
  background: purple;
}...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class='link1'>
...
<div id="link1">
...
<div id="link2">
...

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

Implementing HTML template into Express.js for seamless web development

Is there a way to include my HTML/JS/CSS files in express.js? Can I manage templates in a specific way? I am looking to use HTML exclusively, without the use of Jade. <head> <DATA> </head> <header> <? ...

I am currently working with CakePHP and am interested in learning how to expire the admin session

I'm having issues with the code in my UserController. When I open the admin panel in two tabs within the same browser and log out from one tab, I am unable to redirect to the login page from the other tab when clicking on any menu. function beforeFil ...

Using inline CSS to apply conditional styles to a component in React is a great way to customize the

I'm currently working on customizing the styles of some buttons depending on their 'active' status and whether the user is hovering over them. So far, it's partially working but I'm encountering behavior that I can't fully com ...

Exploring the Implementation of Multiple Form Validations in SharePoint using PreSaveAction

My knowledge of Javascript is limited to what I can gather from online resources. Currently, I'm facing a challenge with a SharePoint form where I need to set up specific validations that trigger when a user hits the "Save" button. The validations I& ...

What prevents me from calling a function while it is being defined in the prototype?

I am experimenting with inheritance through an example. I want to access all properties of objects abc and pqr, so I decided to use Object.create. However, when I try to get the value of r by calling the getr() function, it doesn't seem to work as exp ...

Ways to slightly tilt an image in Material-UI

I am having trouble displaying my cactus partially above my dialog without it getting cut off when I apply certain styles. Could someone please point out what I might be doing wrong? Here are the styles for the cactus: cactus: { position: 'absolut ...

Occupying the entire width of a screen

Struggling to tidy up a website I'm working on for a client. I've spent countless hours trying to solve this issue! With my limited knowledge, I could really use some help with the code. The main challenge is making the top section of the site (o ...

Incorporate content upon button click using Javascript

Looking to create a unique Survey layout that includes: Question Name Options The question name is editable and can be changed with a click. Initially, there is one option with a checkbox to select it. This option should also update when clicked. Addit ...

What are some ways to connect my CSS styles without encountering a MIME error?

Working on a Nodejs - Express - Vanillajs project, I encountered an issue when testing my routes. The index page failed to load CSS, displaying the following error message: Refused to apply style from 'http://localhost:3000/public/css/bootstrap.min. ...

Some inquiries regarding the fundamentals of JavaScript - the significance of the dollar sign ($) and the error message "is not a function"

Teaching myself JavaScript without actually doing any explicit research on it (crazy, right?) has led to a few mysteries I can't quite crack. One of these mysteries involves the elusive dollar sign. From what I gather, it's supposed to be a con ...

A complete guide on utilizing *ngFor in Angular to display data rows

I am facing an issue with using *ngFor to create new "rows" for adding new categories dynamically. Although there are no errors displayed when I run the program, the intended functionality is not achieved. I have tried making some modifications but it see ...

Guide to generating a dropdown menu and linking it with data received from a server

I am completely new to Angular and recently received a project involving it. My task is to create a nested dropdown list for Json data retrieved from a server using Rest Api calls. The data contains a Level attribute that indicates the hierarchy level of ...

Prevent jQuery from scrolling once the fixed image reaches the bottom of the page

I am struggling to figure out how to keep an image fixed at the top of the footer div while scrolling to the bottom of a webpage. My approach involved calculating the height of the footer in order to adjust the scrolling behavior by subtracting it from th ...

Hide the cursor when the text box is in focus

Is there a way to remove the cursor from a textbox when it is clicked on? I want the cursor to disappear after clicking on the textbox. jQuery(document).ready(function($) { $(function(){ $('#edit-field-date-value-value-datepicker-popup-0&ap ...

Tips for preventing a div from overflowing in col-md-12 in Bootstrap 3

Is there a way to maintain the same width for the image and div below? Everything looks good until the window size is between 545px to 575px. The .header width changes only at col-md-12. How can I preserve the aspect ratio? VIEW SCREENSHOT CODEPEN LINK ...

What is the best way to ensure that a canvas created using JavaScript spans 100% of the width?

Working with a canvas element generated by Javascript, see the code snippet below: http://jsfiddle.net/mtvzgnmm/ <div id="circle"></div> <script> $('#circle').circleProgress({ value: 0.75, size: 400, ...

Using jQuery functionality on rendered results in Angular

Currently, I'm working on truncating a string based on its character count and then adding an ellipsis if the number of characters exceeds 21. I've included a truncate class in the HTML div using jQuery. Everything seems to be functioning correc ...

Take off the wrapping from the package

I need help with my code on how to remove the wrapper span tag without removing the text. <ul> <li> <a href="#"> </a> <ul> <li> <a href="#"> ...

What Makes Sports Illustrated's Gallery Pages Load So Quickly?

After thoroughly examining their code, I'm still puzzled. Are they loading complete new pages, or are they utilizing jQuery to modify the browser's URL while keeping most of the page unchanged? I'm currently inspecting the source of this pa ...

The `getScript` function in jQuery is not working as expected, even though the path is accurate and the script was successfully downloaded

Recently, I created a website using Mustache.js and incorporated templates loaded via AJAX with jQuery. During the testing phase on my local environment, everything worked perfectly. However, upon uploading the site to my school server, an issue arose whe ...