Home > Enterprise >  Color button using css html
Color button using css html

Time:12-27

i have code html

<button onclick=location=URL>Refresh</button>

how is code given css color?for example as below

CSS code `

.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 8px 8px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}

.button2 {background-color: #008CBA;} /* Blue */

` in html

<button >Refresh</button>

but if implemented in this code

<button onclick=location=URL>Refresh</button>

an error occurs

can anyone help me with this?

CodePudding user response:

<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>
</head>
<style>
    .button {
        background-color: #4CAF50;
        /* Green */
        border: none;
        color: white;
        padding: 8px 8px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        margin: 4px 2px;
        cursor: pointer;
    }

    .button2 {
        background-color: #008CBA;
    }

    /* Blue */
</style>

<body>
    <button 
        onclick="window.location.href='https://stackoverflow.com/questions/74922452/color-button-using-css-html';">Refresh</button>
</body>

</html>````

CodePudding user response:

onclick is deprecated and hard to read. Consider to use a listener to your button!

The code to refresh a page is window.location.reload();

    let btn_01;
    document.addEventListener("DOMContentLoaded", onReady);
    function onReady(){
        btn_01 = document.getElementById("button_1");
        btn_01.addEventListener("click",click_1);
    }
    function click_1(){
        window.location.reload();
    }
.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 8px 8px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}

.button2 {background-color: #008CBA;} /* Blue */
<button id="button_1" >Refresh</button>

CodePudding user response:

you must change 2 things:

  • atribute onclick: you mus have this: " around the javascript. like
<button onclick="location=URL" >Refresh</button>
  • if you like to use style with .button, you must add atribute . like this:
<button onclick="location=URL"  >Refresh</button>   

or, you can use other think in style: instead of .button{ ... }, use button{ ... }.

CodePudding user response:

I can't see how you are using the onclick. but something is wrong here:

<button onclick=location=URL>Refresh</button>

Parenthesis are missing. Maybe try something along the line of this

<button onclick="location.href='../'">
  • Related