设置宽度等于高

时间:2020-02-27 21:15:41

标签: html css height width border

我想创建这些盒子。默认情况下,图像悬停在中间时应该在顶部处于顶部。就像他们一样。但是我想在图像标记周围有一个“边框”,该标记用于使框变暗,而无需悬停。

有人知道我如何创建此“边框”吗?我还没有找到任何解决方案来设置img容器的宽度等于其高度。

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>

谢谢您的帮助。

2 个答案:

答案 0 :(得分:3)

您可以为悬停添加一个宽度。我认为您的问题是您的容器大于实际的视图框,因此50%超出了您的想象。

/mingw64/bin:/usr/bin:/c/Users/user123/bin
* {
  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;
  }

答案 1 :(得分:1)

因此,在@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>