设置宽度等于高度

Set width equal to hight

我想制作这些盒子。默认情况下位于中间的图像应该在悬停时位于顶部。正如他们所做的那样。但是我想在图像周围有一个 "Border" 用于在没有悬停的情况下使框变暗。

有人知道我如何创建这个 "Border" 吗?我还没有找到任何解决方案如何将 img-containers 宽度设置为等于它的高度。

作为 Youtube 上的示例,但他们使用了字体,我想使用自定义图像。

* {
  margin: 0;
}

body {
  background-color: #171717;
}

.box {
  margin: 5px;
  display: inline-block;
  width: 300px;
  height: 200px;
  overflow: hidden;
  border-radius: 5px;
  cursor: pointer;
}

.box .headline {
  position: relative;
  width: 100%;
  height: 100%;
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box .headline .img-container {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.2);
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box .headline .img-container img {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  height: 80%;
  padding: 10%;
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box:hover .headline {
  height: 33%;
 }
 
.box:hover .headline .img-container {
  position: relative;
  background-color: rgba(0, 0, 0, 0.2);
  height: 100%;
  border-radius: 50%;
}

 
 .box:hover .headline .img-container img {
  height: 70%;
  border-radius: 50%;
}
 
 .box .content {
   opacity: 0;
   padding: 5px;
   max-height: calc(66% - 10px);
   overflow: hidden;
   transition: 0.5s;
  -webkit-transition: 0.5s;
 }
 
.box:hover .content {
    opacity: 1;
  }
<div class="box" style="background-color: rgb(255, 131, 96);">
  <div class="headline">
    <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(125, 206, 130)">
  <div class="headline">
    <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
   </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(232, 226, 136)">
  <div class="headline">
        <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(0,255,245)">
  <div class="headline">
        <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

感谢您的帮助。

您可以为悬停添加宽度。我认为你的问题是你的容器比实际的视图框大,所以 50% 比你想象的要大。

* {
  margin: 0;
}

body {
  background-color: #171717;
}

.box {
  margin: 5px;
  display: inline-block;
  width: 300px;
  height: 200px;
  overflow: hidden;
  border-radius: 5px;
  cursor: pointer;
}

.box .headline {
  position: relative;
  width: 100%;
  height: 100%;
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box .headline .img-container {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.2);
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box .headline .img-container img {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  height: 80%;
  padding: 10%;
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box:hover .headline {
  height: 33%;
 }
 
.box:hover .headline .img-container {
  position: relative;
  background-color: rgba(0, 0, 0, 0.2);
  height: 100%;
  width: 22.5%;
 border-radius: 50%;
}

 
 .box:hover .headline .img-container img {
  height: 70%;
  border-radius: 50%;
}
 
 .box .content {
   opacity: 0;
   padding: 5px;
   max-height: calc(66% - 10px);
   overflow: hidden;
   transition: 0.5s;
  -webkit-transition: 0.5s;
 }
 
.box:hover .content {
    opacity: 1;
  }
<div class="box" style="background-color: rgb(255, 131, 96);">
  <div class="headline">
    <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(125, 206, 130)">
  <div class="headline">
    <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
   </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(232, 226, 136)">
  <div class="headline">
        <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(0,255,245)">
  <div class="headline">
        <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

因此,在 @g-cyrillus 的帮助下,我创建了我的代码:

* {
  margin: 0;
}

body {
  background-color: #171717;
}

.box {
  margin: 5px;
  display: inline-block;
  width: 300px;
  height: 200px;
  overflow: hidden;
  border-radius: 5px;
  cursor: pointer;
}

.box .headline {
  position: relative;
  width: 100%;
  height: 100%;
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box .headline .img-container {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.2);
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box .headline .img-container img {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  height: 80%;
  transition: 0.5s;
  -webkit-transition: 0.5s;
}

.box:hover .headline {
  height: 33%;
 }
 
.box:hover .headline .img-container {
  position: relative;
  background-color: rgba(0, 0, 0, 0.2);
  height: 100%;
  border-radius: 50%;
  width: calc(200px * 0.33);/* Height of box multiplied with height of headline (Percentage) */ 
  margin-top: 2%;
}

 
 .box:hover .headline .img-container img {
  border-radius: 50%;
}
 
 .box .content {
   opacity: 0;
   padding: 5px;
   max-height: calc(66% - 10px);
   overflow: hidden;
   transition: 0.5s;
  -webkit-transition: 0.5s;
 }
 
.box:hover .content {
    opacity: 1;
  }
<div class="box" style="background-color: rgb(255, 131, 96);">
  <div class="headline">
    <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(125, 206, 130)">
  <div class="headline">
    <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
   </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(232, 226, 136)">
  <div class="headline">
        <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>

<div class="box" style="background-color: rgb(0,255,245)">
  <div class="headline">
        <div class="img-container">
      <img src="https://image.flaticon.com/icons/svg/1041/1041820.svg">
    </div>
  </div>
  <div class="content">
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
    Lorem ipsum<br />
  </div>
</div>