Active CSS Class

I am encountering an issue with CSS and jQuery. I have two buttons and I would like to change the background color of the button that is clicked to blue using the 'active' CSS property. However, it seems to not be working as intended. The problem arises when the button is no longer active, causing it to lose its background color. Below is the code snippet: HTML:

<div id='hey'>
  <div class='b' id='b1' href="#">Button One</div>
  <div class='b' id='b2' href="#">Button Two</div>
</div>

CSS:

body,html { 
  font: bold 14px/1.4 'Open Sans', arial, sans-serif;
  background: #000;
}
#hey { 
  margin: 150px auto 0; 
  padding: 0; 
  list-style: none; 
  display: table;
  width: 600px;
  text-align: center;
}

.b {
  color: #fff;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 0.15em;

  display: inline-block;
  padding: 15px 20px;
  position: relative;
}
.b:active {
  background-color: blue;
}

JS:

$(".b").click(function() {
    $(this).addClass('active');
});

Answer №1

:active is a pseudo-class that signifies "while in the activated state" (usually when the mouse button is pressed down on it).

Instead of using a class called active, you should opt for a class selector: .active (with a . instead of a :).


It's advisable to avoid naming classes after pseudo-classes to prevent confusion.

Answer №2

:active is a pseudo-element that you are attempting to include as a class name. Therefore, the correct CSS syntax would be:

.b.active {
  background-color: blue;
}

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

I am encountering issues with my PostCSS plugin not functioning properly within a Vue-cli 3 project

I developed a custom postcss plugin that was working perfectly according to the postcss guidelines until I tried to implement it in a real project. For reference, here's the plugin on GitHub My goal is to integrate it into a Vue-cli app using Webpac ...

Dynamic text modification through background color adjustments with Javascript

I have created a JavaScript function that changes the background color of a website, but I am wondering if there is a way to also change the text in the body of the page when this color change occurs. function updateContent(element, curColorNumber){ ...

I was looking to implement back face culling for my sphere model, but I'm currently struggling with defining the vertices for my wireframe sphere

Currently, I am stuck while creating a wireframe sphere object and defining vertices for it. Additionally, I need help with applying back-face culling to the created sphere. Can anyone assist me with this task? In my HTML file, I have been using the three ...

Display more/hide less form using div elements in a NextJS project

Looking to create a hidden block of amenities on my hotel website that can be expanded and collapsed with buttons in NextJS using tailwind-css. How can I achieve this functionality? Example 1: https://i.stack.imgur.com/BbrUj.png Example-2: https://i.stac ...

How can data be transferred from props to state in React Native?

I'm facing a problem with my homescreen button that is supposed to toggle the text in the AlertBar. When I click the button, the text in AlertBar should change based on the status of isParked. However, currently nothing happens when I press the butto ...

What steps should I take to create my own category organizer?

Currently, I am in the process of creating a category organizer for my website. Radio buttons can quickly become overwhelming, while select/option buttons are not very noticeable. After some searching, I came across this unique category organizer and I am ...

What is the process for changing the timestamp "2021-04-18T00:00:00Z" to UTC+8 timezone?

I'm feeling a bit lost with this date format (2021-04-18T00:00:00Z). Can someone please explain to me what type of format is this and how can I convert it to UTC+8? I am currently working on creating a message extension for the Microsoft Teams app, u ...

Having trouble with Bootstrap's justify-content property? The "between" or "mr-auto" options

Having trouble with a simple problem, 1 - Why doesn't it work when I add an independent CSS class to the nav_element and set flex and justify-content: between to create space between the anchor tag and the div? 2 - What is the correct approach in Bo ...

How can I use jQuery to show the text value of a selected checkbox, dropdown menu, and flipswitch in a popup?

This form's HTML is set up for input with a popup display feature upon submission. Currently, only text can be displayed in the popup. It doesn't show the text of selected checkboxes or dropdown lists - just numbers for the dropdown and "ON" for ...

Exploring React and NextJS Select: Leveraging fetch to iterate over an object and retrieve existing values exclusively

My goal is to access player stats for a specific season using the NHL api. I have a utility function that includes various season options: export const seasonOptions = [ { value: "19861987", label: "1986/1987" }, { value: "1987 ...

Refreshing the page is the only way to trigger the loading of the Angular subscription. It does not initialize

I am facing an issue with my Angular app featuring a dashboard and several components accessible through router links. For example: Dashboard Devices The routing is functioning correctly, but the problem lies in my subscription not loading in ngOninit w ...

Tips on utilizing returned data following a successful ajax call within an if statement

Currently, I am facing an issue where I have a script that returns a string value. When using the alert function, the correct value is displayed. However, after implementing an if condition to compare the data with the value from login.php, the alert mes ...

What is the best way to give an input box an id/className/refs in React and then access it through a variable?

As a new React learner, I am diving into building my very first project - an expense tracker. Despite being close to finishing, I am struggling with incorporating ids and classNames in React. I've experimented with using refs, but unfortunately, it&ap ...

Trigger the child component's function from the parent component by clicking a button in Angular2

One of the functions in my child component is as follows: reload() { clearInterval(this.points); this.initiateInterval(); this.redrawImages(); } This function redraws a few images on the window.resize event. Now, in the parent component, I h ...

React: Despite my efforts to return a value from render, nothing was actually returned

My current project involves creating nested components to display the dataset I have. export const MyComponent = (props) => { const groupMilestoneByYear = (data) => { // Take Milestone Data array as input and group it by Year let yearGroup ...

Updating or removing fullCalendar events in Symfony

I'm struggling to figure out how to update or delete fullcalendar events in my Symfony project. When adding a new event, I open a modal window with a form for submitting the event and inserting it into the database. This is my working controller: $ ...

A Guide to Connecting a JavaScript File to an HTML Page with Express and Node.js

Struggling with integrating my JavaScript file into my simple NodeJS app. Traditional methods like placing the script in the header doesn't seem to work with Node. I've attempted using sendFile and other approaches, but none have been successful ...

JavaScript linking in HTML anchor tag

Can someone please assist me in getting this code to function properly? I am having difficulty with the numerous quotation marks and unsure of where to place them correctly. Any guidance on resolving this issue would be greatly appreciated. <script typ ...

Problem with special characters in Localstorage for Internet Explorer 8

After developing a jQuery script that retrieves data from a service, stores it in local storage, and displays it on the page upon a specific event, I encountered an issue with displaying Chinese characters only on IE8+ browser. Unfortunately, IE is unabl ...

Choose an option using an input field in HTML and Angular 6

Is it possible in HTML/Angular 6 to create an input field with an arrow on the right that, when clicked, displays a dropdown list of options like a select tag? I tried using datalist but it doesn't give me the desired result. When I enter a value not ...