onclick event in css code example

Example 1: javascript onclick

document.getElementById("myBtn").addEventListener("click", function() {
  alert("Hello World!");
});

Example 2: javascript button

<button onclick="Function()">Text</button>

Example 3: css on click

<!DOCTYPE html>
<html>
<head>
  <style>
    /* On click: */
    a:active {
      background-color: yellow;
    }
    button:focus {
      background-color: yellow;
    }
    /* On hover: */
    h1:hover {
      background-color: red;
    }
    p:hover {
      background-color: red;
    }
  </style>
</head>
<body>
  <a href="#">Click Me</a>
  <button>Click me</button>
  
  <h1>Hover over me</h1>
  <p>Hover over me</p>
</body>
</html>

Example 4: css on click

something:active {}

Tags:

Misc Example