How to use an anchor tag In HTML
The first approach is to wrap the entire <div> element inside an anchor tag (<a>). This way, clicking anywhere inside the <div> will trigger the link.
Syntax:
<a href="yourLink">
<div>
<!-- Your content goes here -->
</div>
</a>
Example: In this example, we will use the anchor tag to make the background clickable.
<!DOCTYPE html>
<html>
<head>
<title>Clickable Background</title>
<style type="text/css">
.box {
width: 200px;
height: 200px;
background-color: #666764;
cursor: pointer;
}
.box:hover {
background-color: #000000;
}
</style>
</head>
<body>
<h1 style="color:green">w3wiki</h1>
<a href=
"https://media.w3wiki.org/wp-content/uploads/w3wiki-17.png">
<div class="box">
Click me!
</div>
</a>
</body>
</html>
Output:
How to make the background of a div clickable in HTML ?
To make the background of a div clickable in HTML, add an onclick attribute to the div element with JavaScript code or wrap the div’s content in an anchor tag. Alternatively, utilize CSS pseudo-elements like ::before or ::after and apply pointer-events: none to the parent div.
Table of Content
- Using an anchor tag
- Using JavaScript