Home > other >  i want to change my text when i hover the button
i want to change my text when i hover the button

Time:02-19

hi im making an exercise for pseudo classes an i got a question how can i change the text when i hover the button (i just want to change not ADD) here is my code:

body{
    background-color: #e9c46a;
}
#mybutton{
    width: 100px;
    height: 45px;
    border-radius: 10px;
    background-color: #1b4332;
    color: #b9fbc0;
    border-color: #1b4332;
    text-align: center;
}
#mybutton:hover{
   font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
   transition: 3s; 
   background-color: white;
    border-color: white;
    color: #1b4332;
    font-size: 20px;
   
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="unt.css">
</head>
<body>
    <button id="mybutton">Click !</button>
</body>
</html>

CodePudding user response:

Wrap the content you want to change with span (button.textContent) and use css pseudo element :before/:after with content to change it

*I remove the position property as I comment out, but you could add it by your own

body{
 background-color: #e9c46a;
}
#mybutton{
    width: 100px;
    height: 45px;
    border-radius: 10px;
    background-color: #1b4332;
    color: #b9fbc0;
    border-color: #1b4332;
    text-align: center;
}
#mybutton:hover span{
display:none;
}
#mybutton:hover:after{
   font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
   transition: 3s; 
   background-color: white;
    border-color: white;
    color: #1b4332;
    font-size: 20px;
    content:'hello'
   
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="unt.css">
</head>
<body>

    <button id="mybutton" ><span>Click !</span></button>
</body>
</html>

CodePudding user response:

You can use :after selector. If you remove "Click !" text from you button and add these codes to your css, it should work as expected.

#mybutton::after {
 content: "Click!";
}

#mybutton:hover::after {
 content: "Hover!";
}

CodePudding user response:

You can change it with a simple JQuery function. Here's an example:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>test</title>
    <link rel="stylesheet" href="Style.css">
  </head>
  
  <body>
    <button onm ouseover="change_text()" onm ouseout="change_text2()"><p id="demo">Hello</p></button>

  <script>
    function change_text(){
      document.getElementById("demo").innerHTML = "Bye"  
    }
    function change_text2(){
      document.getElementById("demo").innerHTML = "Hello"  
    }
  </script>
    
  </body>
</html>
  • Related