How to keep :active css style after click a button

后端 未结 3 1551
不知归路
不知归路 2020-12-08 00:44

Once the button is clicked I want it to stay with the active style instead of going back to normal style. Can this be done with CSS please? Im using blurb button from DIVI

3条回答
  •  攒了一身酷
    2020-12-08 01:11

    CSS

    :active denotes the interaction state (so for a button will be applied during press), :focus may be a better choice here. However, the styling will be lost once another element gains focus.

    The final potential alternative using CSS would be to use :target, assuming the items being clicked are setting routes (e.g. anchors) within the page- however this can be interrupted if you are using routing (e.g. Angular), however this doesnt seem the case here.

    .active:active {
      color: red;
    }
    .focus:focus {
      color: red;
    }
    :target {
      color: red;
    }
    
    
    Target 1
    Target 2
    Target 3

    Javascript / jQuery

    As such, there is no way in CSS to absolutely toggle a styled state- if none of the above work for you, you will either need to combine with a change in your HTML (e.g. based on a checkbox) or programatically apply/remove a class using e.g. jQuery

    $('button').on('click', function(){
        $('button').removeClass('selected');
        $(this).addClass('selected');
    });
    button.selected{
      color:red;
    }
    
    
    
      

提交回复
热议问题