У меня есть следующий код HTML и CSS:
#project_section{
display: grid;
grid-template-columns: 300px 300px;
grid-template-rows: 300px 300px;
grid-column-gap: 150px;
grid-row-gap: 60px;
justify-content: center;
}
#box-1{
background: LightSkyBlue;
color: white;
border-color: black;
border-style: solid;
}
#box-2{
background:LightSalmon;
color: white;
border-color: black;
border-style: solid;
}
#box-3{
background:PaleTurquoise;
color: white;
border-color: black;
border-style: solid;
}
#box-4{
background:PaleGreen;
color: white;
border-color: black;
border-style: solid;
}
.project_name{
text-align: center;
position: relative;
top: 30%;
font-size: 30px;
}
<div id = project_section>
<div id = "box-1"><p class = "project_name">link 1</p></div>
<div id = "box-2"><p class = "project_name">link 2</p></div>
<div id = "box-3"><p class = "project_name">link 3</p></div>
<div id = "box-4"><p class = "project_name">link 4</p></div>
</div>
Я хотел бы, чтобы изображение появлялось над каждым «полем» div в моей сетке при наведении курсора (имеет те же размеры, что и существующее поле под ним), щелкнув это изображение, вы перейдете к ссылке.
Я попытался обернуть каждый блок div тегом привязки с href = "#" в качестве начала, но размеры блоков странно изменились.
Это то, чего вы пытаетесь достичь? Надеюсь это поможет.
#project_section{
display: grid;
grid-template-columns: 300px 300px;
grid-template-rows: 300px 300px;
grid-column-gap: 150px;
grid-row-gap: 60px;
justify-content: center;
}
.box {
color: white;
border-color: black;
border-style: solid;
position: relative;
display: flex;
justify-content: center;
align-items: center;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-ms-transition: all .5s ease;
-o-transition: all .5s ease;
transition: all .5s ease;
}
.box-1{
background: LightSkyBlue;
}
.box-2{
background:LightSalmon;
}
.box-3{
background:PaleTurquoise;
}
.box-4{
background:PaleGreen;
}
.project_name{
position: absolute;
font-size: 30px;
z-index: 1;
margin: 0;
}
.box:hover {
cursor: pointer;
background: transparent;
}
.box:hover .project_link{
opacity: 1;
}
.project_link {
position: absolute;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-ms-transition: all .5s ease;
-o-transition: all .5s ease;
transition: all .5s ease;
}
.project_image {
width: 100%;
height: 100%;
object-fit: cover;
position: relative;
}
.project_link:after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 100%;
height: 100%;
background: rgba(0,0,0,.3);
}
<div id = project_section>
<div class = "box box-1">
<p class = "project_name">link 1</p>
<a href = "" class = "project_link">
<img src = "https://cdn.pixabay.com/photo/2015/05/15/14/42/monkeys-768641__340.jpg" alt = "" class = "project_image">
</a>
</div>
<div class = "box box-2">
<p class = "project_name">link 2</p>
<a href = "" class = "project_link">
<img src = "https://cdn.pixabay.com/photo/2016/11/19/11/37/automobile-1838782__340.jpg" alt = "" class = "project_image">
</a>
</div>
<div class = "box box-3">
<p class = "project_name">link 3</p>
<a href = "" class = "project_link">
<img src = "https://cdn.pixabay.com/photo/2019/04/03/09/24/cherry-blossom-4099835__340.jpg" alt = "" class = "project_image">
</a>
</div>
<div class = "box box-4">
<p class = "project_name">link 4</p>
<a href = "" class = "project_link">
<img src = "https://cdn.pixabay.com/photo/2019/02/28/13/06/sea-4025901__340.jpg" alt = "" class = "project_image">
</a>
</div>
</div>
Итак, что я сделал, так это подправил некоторые ваши css и html. Я заметил, что вы неоднократно использовали эти свойства color: white; border-color: black; border-style: solid;
, поэтому я создал класс .коробка, чтобы повторно использовать эти свойства, чтобы избежать повторения.
.box {
color: white;
border-color: black;
border-style: solid;
position: relative;
display: flex;
justify-content: center;
align-items: center;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-ms-transition: all .5s ease;
-o-transition: all .5s ease;
transition: all .5s ease;
}
Я добавил этот тег привязки с классом .project_link и добавил изображение внутри него. И добавил класс .project_image на изображение
<a href = "" class = "project_link">
<img src = "yourimageurl" alt = "" class = "project_image">
</a>
затем стилизуя привязку и изображение, я добавил эти строки
.project_link {
position: absolute;
top: 0;
width: 100%;
height: 100%;
/*not visible at first so that when
box was hovered it will make a popover effect*/
opacity: 0;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-ms-transition: all .5s ease;
-o-transition: all .5s ease;
transition: all .5s ease; // added a transition
}
.project_image {
width: 100%;
height: 100%;
object-fit: cover;
position: relative;
}
.project_link:after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 100%;
height: 100%;
background: rgba(0,0,0,.3);
}
И, наконец, сделать эффект наведения
.box:hover {
cursor: pointer;
background: transparent; // make the background transparent
}
.box:hover .project_link{
opacity: 1; // transition from 0 opacity to 1
}
Это потрясающе!