Change color of button text by clicking

Asked

Viewed 18,875 times

1

I’d like to know how to change the color of the text of the button writing when I click on it, I want me to click on the button and it comes out of the golden color and stay in the white color until I click on it again, but what I’ve achieved so far is that it changes color only when the mouse gets on top. Below is the css and html code I’ve made so far: css:

#login{
        @extend .table;
        cursor:pointer;
        position:relative;
        width: 100%;
        height: 79px;

        #centralizar_menu{
            @extend .row;
            text-align: center;
            height: 4.8em;}

        &:hover{
            #texto_cliente #titulo{
                display: block!Important;
                @extend .fonte_branca_13;
            }
            background-color: $dourado;
        }

        #texto_cliente{
            width: 100%;
            height: 1.5em;
            padding: 0.5em 0em;

            #titulo{
                @extend .fonte_dourado_13;
            }
        }
    }

HTML:

<div id="login">
    <div id="centralizar_menu">
        <div class="middle">
            <div id="texto_cliente">
                <div id="titulo">Login</div>
                <div class="clearfix"></div>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
    <div class="clearfix"></div>
</div>
  • you only want with css? or want to use with jquery?

  • Preferably only with css

  • Ever tried using the pseudo-class ":active" ? up there you used the Hover, which is when you pass the mouse cursor.

4 answers

3

You can use the pseudo class

&:active{
   //aqui vem como você quer que ele fique quando for clicado
}

the same way you used Hover

  • I am using &:Hover, already tried with &:active, and it remains golden

  • I mean, it turns white only when I hold the mouse button clicked

  • how would I do with jQuery?

  • @Bernardokowacic, show me how the code looks with &:active. Probably, what you only want with javascript/jquery

  • &:active{ #text_client #title{ display: block! Important; @extend . fonte_branca_13; } background-color: $golden; }

  • The problem with this answer is that it will not create a class toggle, it will just keep the color change while the clique is maintained. After release, it will get lost.

  • @Celsomtrindade, so how do you think it could be done?

  • If you are already using jQuery, take a look at the answer below, from Pedro Camara Junior, it should work as you wish.

  • I’m not actually using any jquery on this site

Show 4 more comments

1


Following button option: OBS: Using Sass:

.botao_licitacao {
    background: #003366;
    border: none;
    box-shadow: none;
    border-radius: 0px;
    height: em(45);
    font-weight: bold;
    &:hover{
        background: #072440;
    }
    span{
        color: #fff;
    }
    p{
        color: #fff !important;
        @extend .fonte_light;
        font-size: 15px;
        text-decoration: none;
        box-shadow: none;
        text-shadow: none;
        width: 100%;
        text-align: center;
        margin: 0px !important;
    }
    &:hover{
        background: #072440;
    }
    &:hover{
        background: #072440;
    }
    &:visited{
        background: #072440;
    }
}
  • 1

    If you do not use Sass, just create the class with the property next to it, example: . botao_licitacao p{}

1

I would use jQuery for that;

var $divLogin = $("#login");
$divLogin.click(function(){
if ($divLogin.hasClass("dourado"))
    $divLogin.addClass("branco").removeClass("dourado");
else
    $divLogin.addClass("dourado").removeClass("branco");
});

In your div initially add the class you want

<div id="login" class="dourado">Login</div>

And finally, define the css for each class

.dourado {
    color: yellow;
}

.branco {
    color: white;
}

Basically what is done is to check which class is active in the div and exchange it for the other.

0

My problem was the same but simple to solve:

btn{
/*defini o botão*/
}

btn:hover{
/*Como ele vai ficar ao passar o mouse, ou para o mouse encima*/
}

btn:focus{
/*aqui está a resolução aqui define a cor que o botão deve receber depois de clicar*/
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.