css transparent shape over image

2019-02-22 01:39发布

问题:

This is what i am trying to achive

i have :

    #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>

Finally I don't know how to make it rotated and with the margins cut like in the picture

回答1:

A Quick example of this would use a pseudo element and have the image set in the background.

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>



回答2:

Instead of nested elements, you can just use a pseudo element. This is placed at the bottom of the container div. For this to work, you need position:relative and overflow:hidden on the container div. Also, pseudo elements always need the content declaration.

To modify the border radius, you just play around with left | width | height of the pseudo element. You don't need any rotation.

Instead of hex color and opacity you can as well use the "new" color space rgba(r,g,b,a) where a is the opacity value.

For the passepartout you simply use the border declaration.

#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>



回答3:

You can just use position: absolute for your image and position: relative for your overlay, adjusting the top position and width according to your needs. Here's a Fiddle. Hope this helps!

Edit: Here's an updated version of the Fiddle demonstrating border and overflow properties on the img container. As CBroe mentioned, rotating a circle is probably not a good use of your time in this case. Also, I definitely agree that using a pseudo element is a much cleaner approach than nesting images.