Utilizing numerous buttons with the simultaneous activation of their :active state

.a {
  background-color: white;
}

.a:focus {
  background-color: green;
}

.b {
  background-color: yellow;
}

.b:focus {
  background-color: blue;
}
<button class="a">1</button>
<button class="a">2</button>
<button class="b">3</button>
<button class="b">4</button>

Is there a way to maintain the :focus state of a button even after clicking on another button, unless it belongs to the same class? For example, in the given code snippet, if I click on button 1 it will turn green. If then I click on button 3, 1 will be green and 3 will be blue. However, if I click on button 2 instead of 3, 1 will revert back to white and 2 will become green.

An ideal solution would involve using HTML or CSS only.

Answer №1

As pointed out in arieljuod's comment above, the behavior of <input> radio elements aligns with your description. By assigning the same name attribute to each radio button within a group, you can create a radio group.

If you're open to alternatives beyond using <button>, class, and :focus, consider employing

<input type="radio">
, name, and :checked, coupled with <label> for stylized button-like effects.

Note the usage of the adjacent sibling combinator +, which targets <span> only when it directly follows a checked input.

.radio-button input {
  display: none;
}

.radio-button span {
  padding: 0.1em 0.6em;
  background-color: rgb(239, 239, 239);
  border: 2px outset rgb(118, 118, 118);
  text-align: center;
}

.radio-button input[name="a"]:checked + span {
  background-color: green;
}

.radio-button input[name="b"]:checked + span {
  background-color: blue;
}

.radio-button input[name="c"]:checked + span {
  background-color: red;
}
<label class="radio-button">
  <input type="radio" name="a">
  <span>a1</span>
</label>
<label class="radio-button">
  <input type="radio" name="a">
  <span>a2</span>
</label>
<label class="radio-button">
  <input type="radio" name="b">
  <span>b1</span>
</label>
<label class="radio-button">
  <input type="radio" name="b">
  <span>b2</span>
</label>
<label class="radio-button">
  <input type="radio" name="b">
  <span>b3</span>
</label>
<label class="radio-button">
  <input type="radio" name="c">
  <span>c1</span>
</label>
<label class="radio-button">
  <input type="radio" name="c">
  <span>c2</span>
</label>

For more information, check out:
How to Style a Selected Radio Buttons Label?

Answer №2

Here is a fun JavaScript solution! When you click on button 1, it will change color to green. If you then click on button 2, button 1 will turn white and button 2 will turn green.

var buttonOne = document.querySelector(".1");
var buttonTwo = document.querySelector(".2");
var buttonThree = document.querySelector(".3");
var buttonFour = document.querySelector(".4");
buttonOne.style.backgroundColor = "white";
buttonTwo.style.backgroundColor = "white";
buttonThree.style.backgroundColor = "yellow";
buttonFour.style.backgroundColor = "yellow";
buttonOne.addEventListener("click", function() {
  if (buttonOne.style.backgroundColor == "white") {
    buttonOne.style.backgroundColor = "green";
  } else if (buttonOne.style.backgroundColor == "green") {
    buttonOne.style.backgroundColor = "white";
  } else {
    buttonOne.style.backgroundColor = "white";
  }
});
buttonTwo.addEventListener("click", function() {
  if (buttonTwo.style.backgroundColor == "white" && buttonOne.style.backgroundColor == "green") {
    buttonTwo.style.backgroundColor = "green";
    buttonOne.style.backgroundColor = "white";
  } else if (buttonTwo.style.backgroundColor == "white") {
    buttonTwo.style.backgroundColor = "green";
  } else if (buttonTwo.style.backgroundColor == "green") {
    buttonTwo.style.backgroundColor = "white";
  } else {
    buttonTwo.style.backgroundColor = "white";
  }
});
buttonThree.addEventListener("click", function() {
  if (buttonThree.style.backgroundColor == "yellow") {
    buttonThree.style.backgroundColor = "blue";
  } else if (buttonThree.style.backgroundColor == "blue") {
    buttonThree.style.backgroundColor = "yellow";
  } else {
    buttonThree.style.backgroundColor = "yellow";
  }
});
buttonFour.addEventListener("click", function() {
  if (buttonFour.style.backgroundColor == "yellow") {
    buttonFour.style.backgroundColor = "blue";
  } else if (buttonFour.style.backgroundColor == "blue") {
    buttonFour.style.backgroundColor = "yellow";
  } else {
    buttonFour.style.backgroundColor = "yellow";
  }
});
<button class="1">1</button>
<button class="2">2</button>
<button class="3">3</button>
<button class="4">4</button>

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

Failure to Establish a Session

I'm encountering an issue where the session remains empty despite having the following code in place. Could someone assist me in troubleshooting this problem? <?php session_start(); ob_start(); $ifLogged = $_SESSION["mbse"]; if(!isset($ifLogg ...

The white background of the div conceals the shadow of the Bootstrap navbar

I am using Bootstrap's navbar with .shadow-sm. I have two divs beneath it, one without background and the other one with a white background. The shadow appears as expected in the div with no background specified, but it is being obscured by the white ...

Tips for utilizing a particular style from a font collection?

I am currently in the process of building my first website and I am experimenting with different fonts available on fontsquirrel. However, I am encountering an issue where I am only able to utilize certain fonts that I have downloaded from the site. One ...

Parsing all HTML elements using Nokogiri

I've been searching everywhere and all I could find was how to use CSS selection with Nokogiri. What I really need is to completely remove all HTML tags. For instance, this: <html> <head><title>My webpage</title></head& ...

Arranging Bootstrap inputs horizontally with lengthy labels

When placing two input fields in a row, such as a drop down menu, with labels of varying lengths, the alignment of the inputs can become skewed. This issue persists even when attempting to add breaks in the code due to unpredictable wrapping behavior at di ...

Encountered a MySQL error while attempting to insert an image into the database

I have a task to work on a website where users can upload product images that will be stored in a MySQL database. The code for my HTML form is: <FORM action="testimage1.php" ENCTYPE="multipart/form-data" method="post"> <div st ...

Omit child DIV element in JavaScript and the Document Object Model

I have a situation with two div elements. One is <div class="card" id="openWebsite"> and the other is a sub-division <div class="card__btn"> Here's the issue: When someone clicks on the main div, they get red ...

What steps can be taken to ensure the Instagram logo remains visible even when it is being hovered over?

Struggling to incorporate a social media icon on my website, I am facing an issue where the Instagram icon outline disappears on hover due to the gradient styling in CSS3. Below is my current code: .social-icons li.instagram a { border-radius: 50%; ...

Encountering a NullPointerException while trying to load a resource from a directory that is not within

I'm working with JavaFX and trying to incorporate a CSS file into my scene. scene.getStyleSheets().add(Main.class.getResource("res/application.css").toExternalForm(); An NPE is being thrown. I suspect it's because the css file isn't locate ...

Creating a FusionCharts time series with a sleek transparent background

Currently, I am attempting to achieve a transparent background for a time-series chart created with FusionCharts. Despite trying the standard attributes that usually work on other chart types and even hardcoding a background color, none of these seem to af ...

Electron generates a unique landing page for your first launch

Can you help me create a feature on a webpage that only appears once and then never shows again? Similar to the "Never show me again" checkbox in Visual Studio Code upon first launch. ...

Using weasyprint to convert a single HTML page into a PDF

I have encountered a challenge while attempting to convert an HTML page with a single table (containing images or text in the td elements) to a PDF using the Python module weasyprint. The issue I am facing is that weasyprint is inserting page breaks withi ...

What is the best way to conceal an element when another element is given a specific class?

Imagine you have 2 buttons The first button <button class="arguments variation-one">Some text</button> - this button has dynamic classes like: variation-one, variation-two, variation-three, but the .arguments class remains static. a ...

Trimming text from the start and inserting ellipsis in a multi-line passage

My goal is to present a paragraph on the webpage that will be dynamically updated based on user actions. I want to restrict the content to a specified number of lines and if the user tries to exceed this limit, the beginning of the paragraph should be trun ...

Unconventional border effect while hovering over image within navigation container of Bootstrap

Whenever I hover over the image inside the navigation bar, there is a strange white/gray border that appears. It's quite annoying. Does anyone know how to remove this border? Here is the code snippet causing the issue: <ul class ...

What is the best method for inserting CSS into a webpage using a Chrome extension?

I am seeking guidance on how to incorporate CSS into a webpage using a Chrome extension. The CSS code I am attempting to inject is as follows: body { background: #000 !important; } a { color: #777 !important; } Here is the content of my manifest.j ...

I find myself with just one button in my javascript function, but I actually need two

I'm currently working on a class assignment where I'm trying to create a javascript function that uses a button to display the value on a slider component. However, I'm running into an issue where only one button appears instead of two, even ...

Issue with displaying spinner in bootstrap version 4.0.0

I am having trouble implementing a spinner on my website. I have followed the code below but still can't see the spinner showing up. Can someone please help me troubleshoot this? <!-- Bootstrap CSS --> <link rel="stylesheet" href= ...

Troubleshooting Problems with Positioning and Floating in HTML and CSS

I'm facing some challenges with clearing floats (or it could be something else?). My goal is to have the #newsbar div free from the previous floats so that its width can extend 100% across the page/browser. I believe I've tried everything within ...

Can you explain the process for setting the css property text-fill-color in IE and Mozilla browsers, similar to how I set -webkit-text-fill-color for Webkit?

The color displays correctly in Chrome, but how can I make it work in both IE and FF? CSS #Grid td.note div.has-note i { -webkit-text-fill-color: #b4efa8; } ...