Как превратить элементы div grid в ссылку и наложить на изображение при наведении курсора мыши?

#html #css #css-grid

#HTML #css #css-сетка

Вопрос:

У меня есть следующий 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 = «#», но размеры полей изменились странным образом.

Ответ №1:

Это то, чего вы пытаетесь достичь? Надеюсь, это поможет.

 #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 для повторного использования этих свойств, чтобы избежать повторения.

 .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
}
  

Комментарии:

1. Это идеально!