'why link using anchor tag not working on image?
I have created the following using html and css. But my link is not working whenever i try to click on the image.I have used the flex box in this an =d have used multiple elements in a row but giving the code for one element only.
<div id="CONTESTS">
<div class="contests">
<h2>Upcoming Contests</h2>
<div class="same-row">
<div class="row">
<div class="list-col">
<a href="https://www.codechef.com/contests" >
<img src="codechef.png" alt="">
</a>
<div class="layer">
<!-- CodeChef -->
</div>
</div>
</div>
</div>
</div>
</div>
SCSS code
.contests{
width:80%;
margin:auto;
text-align: center;
padding-top: 50px;;
}
.course-box{
// padding:8vw 8vw 0 8vw;
text-align: center;
img{
width: 100%;
height:100%;
background-size: cover;
background-position: center;
}
}
.list-col{
flex-basis: 100%;
border-radius: 10px;
// margin-bottom: 30px;
position: relative;
overflow: hidden;
}
.list-col img{
width: 100%;
}
.layer{
// background: rgba(226, 0, 0, 0.7);
background: transparent;
height:100%;
width:100%;
position: absolute;
top:0px;
left:0;
transition: 0.5s;
}
.layer:hover{
cursor: pointer;
background: rgba(255, 0, 0, 0.7);
}
.same-row{
display: flex;
flex-wrap: wrap;
}
.same-row > div {
flex: 25%; /* or - flex: 0 50% - or - flex-basis: 50% - */
/*demo*/
// box-shadow: 0 0 0 1px black;
// box-shadow: 2.5px 5px rgb(164, 164, 164);
margin-bottom: 10px;
margin-top:40px;
margin-left: 20px;
border-radius: 10px;
}
My main aim is to direct me to the page wheneevr i click on the image . I am not able to figure out the error in my code . Any help would be appreciated.
Solution 1:[1]
*,
*::before,
*::after {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.contests {
width: 80%;
margin: auto;
text-align: center;
padding-top: 50px;
}
.course-box {
padding: 8vw 8vw 0 8vw;
text-align: center;
img {
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
}
}
.list-col {
flex-basis: 100%;
border-radius: 10px;
margin-bottom: 30px;
position: relative;
overflow: hidden;
}
.list-col img {
object-fit: cover;
}
.layer {
background: transparent;
border-radius: 10px;
height: 100%;
width: 100%;
position: absolute;
top: 0;
right: 0;
transition: all 0.5s ease-in-out;
}
.layer:hover {
cursor: pointer;
background: rgba(255, 0, 0, 0.7);
pointer-events: none;
}
.same-row {
display: flex;
flex-wrap: wrap;
}
.same-row > div {
flex: 25%; /* or - flex: 0 50% - or - flex-basis: 50% - */
/*demo*/
// box-shadow: 0 0 0 1px black;
// box-shadow: 2.5px 5px rgb(164, 164, 164);
margin-bottom: 10px;
margin-top: 40px;
margin-left: 20px;
border-radius: 10px;
}
<div id="CONTESTS">
<div class="contests">
<h2>Upcoming Contests</h2>
<div class="same-row">
<div class="row">
<div class="list-col">
<a href="https://www.codechef.com/contests">
<img src="codechef.png" alt="">
</a>
<div class="layer">
<!-- CodeChef -->
</div>
</div>
</div>
</div>
</div>
</div>
Solution 2:[2]
Perhaps you'd like to try onClick event in img tag
<img src="codechef.png" alt="" onclick="window.open('https://www.codechef.com/contests')">
Regards
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Alfredo Campos EnrĂquez |
