2017-03-15 101 views
6

This is what i am trying to achiveCSS透明形狀

我有:

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

最後,我不知道如何使它旋轉並與邊緣切割像圖片

回答

3

一個快速的例子會使用一個僞元素,並在後臺設置圖像。

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>

+0

我們基本上有同樣的答案...因此+1不能被阻止^ _ ^你的只是更有魅力...> _> – Christoph

1

您可以將position: absolute用於圖片,position: relative用於覆蓋圖,廣告根據您的需求調整頂部位置和寬度。這是一個Fiddle。希望這可以幫助!

編輯:這是小提琴的updated version演示img容器的邊界和溢出屬性。正如CBroe所說,在這種情況下,旋轉一個圓圈可能不是一個很好的利用你的時間。另外,我絕對同意使用僞元素比嵌套圖像更簡潔。

+0

我明白了,但仍然沒有得到我,我想(見圖片的例子)我需要旋轉它,使看到的效果它喜歡它只是部分OA更大的圈子 – johnnyshrewd

+0

如果比如我把形狀做的更大,如果只是這樣做的話......我想把它做得更大,讓他父母把它切掉 – johnnyshrewd

+1

不,你不需要旋轉任何東西 - 旋轉一個圓圈是毫無意義的,它看起來是一樣的不管你多麼「旋轉」它。爲了讓它變大並只顯示其中的一部分,你只需要玩大小和位置。將「overflow:hidden」添加到容器中,以切斷將出去的圓的部分。 – CBroe

3

代替嵌套元素,你可以只使用一個僞元素。這放在容器div的底部。爲此,您需要在容器格上使用position:relativeoverflow:hidden。另外,僞元素總是需要content聲明。

要修改邊界半徑,只需使用pseudo元素的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>

+0

你可能想在僞命令上使用'%'高度,否則它不會是儘可能快地迴應。 – jbutler483

+1

@ jbutler483感謝您指出這一點!相應地改變了它。 – Christoph