图像上的 CSS 透明形状



这就是我想要实现的目标

我有:

    #image1 {
    position: absolute;
    bottom: 0px;
    align-self: auto;
    background-color: #dc022e;
    width: 340px;
    height: 100px;
    border-radius: 50% / 100%;
    border-bottom-left-radius: 0;
    /*transform: rotate(10deg);*/
    border-bottom-right-radius: 0;
    opacity: 0.8;
    }
    
    #image2 img {
    width: 80%;
    }
<div>
  <div id="image2">
    <img src="http://t1.gstatic.com/images?q=tbn:ANd9GcThtVuIQ7CBYssbdwtzZjVLI_uw09SeLmyrxaRQEngnQAked5ZB">
  </div>
  <div id="image1"></div>
</div>

最后我不知道如何让它旋转并像图片中一样切割边距

一个简单的示例是使用伪元素并将图像设置为背景。

div {
  position: relative;
  height: 300px;
  width: 500px;
  background: url(http://lorempixel.com/500/300);/*image path*/
  overflow: hidden;/*hides the rest of the circle*/
}
div:before {
  content: "";
  position: absolute; /*positions with reference to div*/
  top: 100%;
  left: 50%;
  width: 0;/*define value if you didn't want hover*/
  height: 0;
  border-radius: 50%;
  background: tomato;/*could be rgba value (you can remove opacity then)*/
  opacity: 0.5;
  transform: translate(-50%, -50%);/*ensures it is in center of image*/
  transition: all 0.4s;
}
/*Demo Only*/
div:hover:before {/*place this in your pseudo declaration to remove the hover*/
  height: 100%;
  width: 150%;/*this makes the shape wider than square*/
  transform: translate(-50%, -50%) rotate(5deg);/*ensures it is in center of image + rotates*/
}
div {/*This stuff is for the text*/
  font-size: 40px;
  line-height: 300px;
  text-align: center;
}
<div>HOVER ME</div>

您可以使用伪元素代替嵌套元素。它被放置在容器div 的底部。为此,您需要在容器div 上position:relativeoverflow:hidden。此外,伪元素始终需要content声明。

要修改边框半径,您只需使用伪元素left | width | height即可。您不需要任何旋转。

除了十六进制颜色和不透明度之外,您还可以使用"新"颜色空间rgba(r,g,b,a)其中a是不透明度值。

对于密码,您只需使用 border 声明。

#image2{
    position:relative;
    border:10px solid #888;
    overflow:hidden;
    box-shadow:0 0 4px #aaa;
}
#image2::after {
    content:"";
    display:block;
    position: absolute;
    bottom: 0;left:-10%;
    background-color: #dc022e;
    width: 120%;
    height: 60%;
    border-radius: 100% 100% 0 0;
    opacity: 0.8;
}
    
#image2 img {
    width: 100%;
    display:block;
    position:relative;
}
<div id="image2">
    <img src="http://t1.gstatic.com/images?q=tbn:ANd9GcThtVuIQ7CBYssbdwtzZjVLI_uw09SeLmyrxaRQEngnQAked5ZB">
  </div>

您可以只将position: absolute用于图像,position: relative用于叠加层,根据需要调整顶部位置和宽度。这是一个小提琴。希望这有帮助!

编辑:这是小提琴的更新版本,演示了img容器上的边框和溢出属性。正如CBroe所提到的,在这种情况下,旋转一个圆圈可能不是对时间的良好利用。另外,我绝对同意使用伪元素是一种比嵌套图像更干净的方法。

最新更新