2013-01-14 234 views
7

CSS可以用來使內容容器的左右邊緣看起來像這個圖像嗎?如果可能的話,我一直在試圖找出一種不使用圖像的方法。CSS曲線漸變和盒子陰影

image of left and right borders

這裏的jsfiddle,我一直在努力。 「頂級」類的CSS永遠不會被應用。儘管如此,「底部」類的CSS看起來工作正常。

http://jsfiddle.net/kXuQY/

HTML:

<div class="drop-shadow top bottom"> 
    Content here. 
</div> 

CSS:

.drop-shadow { 
/** Create container. Box-shadow here is to color the inside of the container **/ 
position:relative; 
width:50%; 
background: none repeat scroll 0 0 #FFFFFF; 
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset; 
padding:3em; 
margin: 2em 20px 4em; 
text-align:center 
} 

.top:before, 
.top:after { 
    /** Generate pseudo-elements ('before' and 'after') and push them behind the container box. Position pseudo-elements ('before', and 'after') and give them dimensions **/ 
content:""; 
position:absolute; 
z-index:-1; 
top:20px; 
left:0; 
width:40%; 
height:1em; 
max-width:150px; 
-webkit-box-shadow:0 15px 10px rgba(0, 0, 0, 0.7); 
-moz-box-shadow:0 15px 10px rgba(0, 0, 0, 0.7); 
box-shadow:0 15px 10px rgba(0, 0, 0, 0.7); 
-webkit-transform:rotate(70deg); 
-moz-transform:rotate(70deg); 
-o-transform:rotate(70deg); 
transform:rotate(70deg); 
} 

.top:after{ 
    /**One of the pseudo-elements then needs to be positioned on the other side of the element and rotated in the opposite direction. This is easily done by overriding only the properties that need to differ **/ 
right:0; 
left:auto; 
-webkit-transform:rotate(-70deg); 
-moz-transform:rotate(-70deg); 
-o-transform:rotate(-70deg); 
transform:rotate(-70deg); 
} 

.bottom:before, 
.bottom:after { 
    /** Generate pseudo-elements ('before' and 'after') and push them behind the container box. Position pseudo-elements ('before', and 'after') and give them dimensions **/ 
content:""; 
position:absolute; 
z-index:-2; 
top:90px; 
left:0; 
width:10%; 
max-width:150px; 
-webkit-box-shadow:0 15px 10px rgba(0, 0, 0, 0.7); 
-moz-box-shadow:0 15px 10px rgba(0, 0, 0, 0.7); 
box-shadow:0 15px 10px rgba(0, 0, 0, 0.7); 
-webkit-transform:rotate(99deg); 
-moz-transform:rotate(99deg); 
-o-transform:rotate(99deg); 
transform:rotate(99deg); 
} 

.bottom:after{ 
    /**One of the pseudo-elements then needs to be positioned on the other side of the element and rotated in the opposite direction. This is easily done by overriding only the properties that need to differ **/ 
right:0; 
left:auto; 
-webkit-transform:rotate(-99deg); 
-moz-transform:rotate(-99deg); 
-o-transform:rotate(-99deg); 
transform:rotate(-99deg); 
} 
+4

看看這個[鏈接](http://www.sitepoint.com/pure-css3-paper-curl/)它增加了一個底部捲曲外觀,但它可以幫助你弄清楚如何讓它工作在左邊和右邊。 –

+0

謝謝,我用jsfiddle和css更新了這個問題。我認爲它很接近但仍然有一些問題。 – Mdd

回答

6

您可以使用前後CSS屬性來實現這一目標。 檢查了這一點:http://nicolasgallagher.com/css-drop-shadows-without-images/demo/

另一種方式:

HTML:

<div id="box"> 
<h1>css-3-box-shadow</h1> 
<p>some text</p> 
</div> 

CSS:

#box:after { 
    left: auto; 
    right: 10px; 
    transform: rotate(3deg); 
} 
#box:before, #box:after { 
    background: none repeat scroll 0 0 rgba(0, 0, 0, 0.7); 
    bottom: 15px; 
    box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7); 
    content: ""; 
    left: 10px; 
    max-width: 300px; 
    position: absolute; 
    top: 80%; 
    transform: rotate(-3deg); 
    width: 50%; 
    z-index: -1; 
} 
#box:before, #box:after { 
    background: none repeat scroll 0 0 rgba(0, 0, 0, 0.7); 
    bottom: 15px; 
    box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7); 
    content: ""; 
    left: 10px; 
    max-width: 300px; 
    position: absolute; 
    top: 80%; 
    transform: rotate(-3deg); 
    width: 50%; 
    z-index: -1; 
} 
#box { 
    background: none repeat scroll 0 0 #DDDDDD; 
    border-radius: 4px 4px 4px 4px; 
    color: rgba(0, 0, 0, 0.8); 
    line-height: 1.5; 
    margin: 60px auto; 
    padding: 2em 1.5em; 
    position: relative; 
    text-shadow: 0 1px 0 #FFFFFF; 
    width: 60%; 
} 

編號:http://www.red-team-design.com/wp-content/uploads/2011/04/css3-box-shadow.html

希望有所幫助。

+0

感謝您的提示!我用jsfiddle和我迄今爲止所用的CSS更新了這個問題。這裏再次提琴:http://jsfiddle.net/kXuQY/我的「頂級」類的CSS似乎被覆蓋或導致它不被應用。 – Mdd

+1

@Mdd - 你可以做的是,創建3個單獨的DIV,然後將陰影(使用上面的代碼)應用到頂部和底部div(第一個div上的頂部陰影和第三個div上的底部陰影)。保持原有內容的中間位置。最後,放置所有的DIV,使整個事物看起來像一個統一的盒子。 – Learner

+0

謝謝!我更新了小提琴,我知道現在該做什麼。再次感謝! http://jsfiddle.net/kLhP8/ – Mdd