0

Is there any way to change the background color and/or border color of a button when it is in active state (i.e. Clicked) using inline css?

<button class="btn btn-xs btn-primary btn-inverse dropdown-toggle" type="button" data-toggle="dropdown" style="margin-top: 12px; background-color: rgba(0, 0, 0, 0); border-color:rgba(0, 0, 0, 0); margin-right:20px">

Something like this but for when the button is clicked?

iainn
  • 16,826
  • 9
  • 33
  • 40
Potterapple
  • 55
  • 2
  • 9

2 Answers2

2

You can't change the background color and/or border color of a button when it is in active state (i.e. Clicked) using inline css, You can use :active in calss to change baground color or border color

Nitrick
  • 131
  • 4
  • Thanks for the answer! Is there any reason why we can't access btn:active inline? – Potterapple Sep 22 '16 at 13:02
  • 1
    I think we can't use btn:active is the event which is going to happen after button is clicked and that and only by css or js by triggering it and etc. In short that class will be not abel to render properly by inline so we can't use it. – Nitrick Sep 22 '16 at 17:51
2

Considering that css :active is just a css pseudo-class and not a Dom property or attribute you can't have an inline equivalent for that.

But, if in your case, the click event could be a valid alternative, you can do something like that...

<script>
  function toggleBg(element, color) {
    if(!color) {
      color = element.dataset.normalColor;
    } else {
      element.dataset.normalColor = element.style.backgroundColor;
    }

    element.style.backgroundColor = color;
  }
</script>

<button onmousedown="toggleBg(this,'red')" onmouseup="toggleBg(this)">RED ON PRESS</button>

Just a note, inline-styling or inline-javascript isn't a good practice, if you can, use css:

<style>
  button:active { background: red; }
</style>

<button>RED WHEN ACTIVE</button>
Hitmands
  • 13,491
  • 4
  • 34
  • 69