• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

CSS Make :focus button last forever unless another button is :focus


New Coder
I have some CSS code for a list of buttons. I want the buttons changed colors from the focus to last forever until another button is in :focus.For example:
.buttons {
.buttons:focus {
However, when I press away from the button, the :focus styles go away. I understand this is normal behavior, but is there a way to change it so that the button is :focus until another button is :focus? Or is that impossible without JS?


King Coder
Yes, you'll need to post full, working code and please do so in code tags.
If you say "press away from the button" I take that to mean you click some other element, which means that that element gets focus and your button loses focus. Only one element can have the focus, as I think you're aware of. So, it's important that you are more specific and precise about what you are doing with the mouse and keyboard. A step-by-step description of the issue, as it were. My gut feeling is that you'll need JS coding in various event handlers, you probably can't do it with CSS only.


New Coder
Here is where I'm having a problem:
<div class="btns">
<button class="numbers">1</button> <button class="numbers">2</button>
<button class="numbers">3</button> <button class="numbers">4</button>
<button class="numbers">5</button>
Ignore the vars for colors. those aren't important
.numbers {
border-radius: 50%;
background-color: var(--vDB);
color: var(--mG);
border: none;
width: 30px;
height: 30px;
margin: 8.3px;
opacity: 0.4;
.numbers:focus {
background-color: var(--orange);
color: white;
opacity: 1


If I were to add some JS like this:
for (let ele of document.querySelectorAll("button.numbers")) {
ele.onclick = () => {
ele.dataset.focused = "true";
it would mean that the buttons would stay focused forever, even if another button is pressed. (FYI, focused will be used as a CSS hook)