在倾斜 div 上创建部分框阴影
Creating a partial box shadow on a skewed div
我正在尝试在倾斜的 div 上创建部分阴影,尽可能靠近此广告素材。
现在我一直在尝试使用伪元素(具体之前)来执行此操作,但我发现每当我倾斜应用它们的元素时,这些元素都会表现得很奇怪。伪元素一直出现在我的 div 之上,即使 z-index 设置为 -1。无论我用 z-index 做什么,它都会保持在它之上。我希望它位于所应用的 div 之后,并位于下面的 div 之前,就像在广告中一样。
这是我的 ::before 代码和代码笔 link
CSS
/*! Shadows */
#test-title {
position: relative;
}
#test-title:before {
z-index: -1;
position: absolute;
content: "";
bottom: 15px;
left: 10px;
width: 50%;
top: 80%;
max-width:300px;
box-shadow: 0 15px 10px #777;
-webkit-transform: rotate(-3deg);
-moz-transform: rotate(-3deg);
-o-transform: rotate(-3deg);
-ms-transform: rotate(-3deg);
transform: rotate(-3deg);
}
我已经试过了,它并不完美,但是,它更接近我想要的样子,恕我直言:
<div id="test-title">
<h3>What our clients say about us</h3>
</div>
<div id="shadow1"></div>
所以,我添加了新的 html 元素(阴影),而不是使用伪元素...现在,我已经正确设置了 z-index 和位置,以隐藏旋转的阴影 div 在第一个 div 后面,并添加这个 css:
#shadow1 {
position:absolute;
width:50%;
height:150px;
background:black;
top:50px;
left:11%;
z-index:6;
opacity:1;
transform: rotate(-5deg);
box-shadow: 15px 56px 50px -12px rgba(0,0,0,0.80);
}
演示:http://codepen.io/anon/pen/vGwNqY
您可以玩旋转、框阴影、位置、高度...但是,这可能是一个好的开始(也许)。 P.S。类似的逻辑可以应用于第二个 div.
更简单的方法是在第一个框之后的每个框的顶部放置阴影。这将解决各种 z-index 问题,因为每个盒子都比它上面的盒子高 1 级..它允许阴影位于容器内部而不是外部。
我还更改了您的阴影样式,使用径向渐变*代替方框阴影,因为在这种情况下更容易控制,也更接近您的设计。我也做了一些定位,让它看起来更好一点,并得到 skew1
和 skew2
的单独边
我已将您的上一个规则集更改为:
.test-info:before {
position: absolute;
content: "";
width: 100%;
left: 0;
top: 0;
height: 30px;
}
.test-info.skew1:before {
background: radial-gradient(ellipse farthest-side at 30% top, rgba(0,0,0,0.5) 0%, rgba(0,0,0,0) 100%);
}
.test-info.skew2:before {
background: radial-gradient(ellipse farthest-side at 70% top, rgba(0,0,0,0.5) 0%, rgba(0,0,0,0) 100%);
}
* 注意:您可能希望 check/add 对我在使用之前添加的渐变提供额外的浏览器支持。
尝试使用 :before
伪 https://jsfiddle.net/0andpzsp/
为第二个元素制作框阴影
.cont {
width: 1000px;
height: 500px;
}
div[class^="d"] {
width: 70%;
height: 50%;
position: relative;
margin-left: 40px;
margin-top: 50px;
}
.d0 {
background: linear-gradient(to right, #005f8a 0%,#00486c 50%,#003a59 100%);;
transform: skew(20deg)
}
.d1 {
background: linear-gradient(to right, #005f8a 0%,#00486c 50%,#003a59 100%);;
overflow: hidden;
top: -50px;
left: 20%;
transform: skewX(-20deg);
z-index: -1
}
.d1:before {
content: '';
border-radius: 30%;
position: absolute;
display: block;
width: 600px;
height: 70px;
z-index: 9999;
top: -100px;
left: -70px;
box-shadow: -50px 60px 90px 0px #000;
transform: rotate(-5deg)
}
<div class="cont">
<div class="d0"></div>
<div class="d1">
</div>
</div>
Skew parent 然后将 child 以相同的度数倾斜。
* {
box-sizing: border-box
}
body {
padding: 40px 0
}
section {
width: 60vw;
clear: both;
overflow: hidden;
min-height: 100px;
margin: 0 auto;
position: relative;
background: #035076
}
section article {
width: 60%;
padding: 20px;
color: white;
margin: 0 auto
}
section:nth-child(even) {
transform: skew(-45deg) translate(5vw);
box-shadow: inset 0 0 2px 0 black;
}
section:nth-child(odd) {
transform: skew(45deg);
}
section:nth-child(even) article {
transform: skew(45deg) translate(5vw);
}
section:nth-child(odd) article {
transform: skew(-45deg);
}
section:before,
section:after {
content: '';
position: absolute;
}
section:nth-child(even):before {
width: 100%;
height: 0;
bottom: 100%;
left: 0;
z-index: 6;
opacity: 1;
transform: rotate(-10deg) translateY(-30px);
box-shadow: 0px 0px 64px 30px rgba(0, 0, 0, 0.8);
}
section:nth-child(odd):not(:first-child):before {
width: 100%;
height: 0;
bottom: 100%;
right: 0;
z-index: 6;
opacity: 1;
transform: rotate(10deg) translateY(-30px);
box-shadow: 0px 0px 64px 30px rgba(0, 0, 0, 0.8);
}
<section>
<article>What our clients say About Us</article>
</section>
<section>
<article>Read More!</article>
</section>
<section>
<article>Read More!</article>
</section>
<section>
<article>Read More!</article>
</section>
我正在尝试在倾斜的 div 上创建部分阴影,尽可能靠近此广告素材。
现在我一直在尝试使用伪元素(具体之前)来执行此操作,但我发现每当我倾斜应用它们的元素时,这些元素都会表现得很奇怪。伪元素一直出现在我的 div 之上,即使 z-index 设置为 -1。无论我用 z-index 做什么,它都会保持在它之上。我希望它位于所应用的 div 之后,并位于下面的 div 之前,就像在广告中一样。
这是我的 ::before 代码和代码笔 link
CSS
/*! Shadows */
#test-title {
position: relative;
}
#test-title:before {
z-index: -1;
position: absolute;
content: "";
bottom: 15px;
left: 10px;
width: 50%;
top: 80%;
max-width:300px;
box-shadow: 0 15px 10px #777;
-webkit-transform: rotate(-3deg);
-moz-transform: rotate(-3deg);
-o-transform: rotate(-3deg);
-ms-transform: rotate(-3deg);
transform: rotate(-3deg);
}
我已经试过了,它并不完美,但是,它更接近我想要的样子,恕我直言:
<div id="test-title">
<h3>What our clients say about us</h3>
</div>
<div id="shadow1"></div>
所以,我添加了新的 html 元素(阴影),而不是使用伪元素...现在,我已经正确设置了 z-index 和位置,以隐藏旋转的阴影 div 在第一个 div 后面,并添加这个 css:
#shadow1 {
position:absolute;
width:50%;
height:150px;
background:black;
top:50px;
left:11%;
z-index:6;
opacity:1;
transform: rotate(-5deg);
box-shadow: 15px 56px 50px -12px rgba(0,0,0,0.80);
}
演示:http://codepen.io/anon/pen/vGwNqY
您可以玩旋转、框阴影、位置、高度...但是,这可能是一个好的开始(也许)。 P.S。类似的逻辑可以应用于第二个 div.
更简单的方法是在第一个框之后的每个框的顶部放置阴影。这将解决各种 z-index 问题,因为每个盒子都比它上面的盒子高 1 级..它允许阴影位于容器内部而不是外部。
我还更改了您的阴影样式,使用径向渐变*代替方框阴影,因为在这种情况下更容易控制,也更接近您的设计。我也做了一些定位,让它看起来更好一点,并得到 skew1
和 skew2
我已将您的上一个规则集更改为:
.test-info:before {
position: absolute;
content: "";
width: 100%;
left: 0;
top: 0;
height: 30px;
}
.test-info.skew1:before {
background: radial-gradient(ellipse farthest-side at 30% top, rgba(0,0,0,0.5) 0%, rgba(0,0,0,0) 100%);
}
.test-info.skew2:before {
background: radial-gradient(ellipse farthest-side at 70% top, rgba(0,0,0,0.5) 0%, rgba(0,0,0,0) 100%);
}
* 注意:您可能希望 check/add 对我在使用之前添加的渐变提供额外的浏览器支持。
尝试使用 :before
伪 https://jsfiddle.net/0andpzsp/
.cont {
width: 1000px;
height: 500px;
}
div[class^="d"] {
width: 70%;
height: 50%;
position: relative;
margin-left: 40px;
margin-top: 50px;
}
.d0 {
background: linear-gradient(to right, #005f8a 0%,#00486c 50%,#003a59 100%);;
transform: skew(20deg)
}
.d1 {
background: linear-gradient(to right, #005f8a 0%,#00486c 50%,#003a59 100%);;
overflow: hidden;
top: -50px;
left: 20%;
transform: skewX(-20deg);
z-index: -1
}
.d1:before {
content: '';
border-radius: 30%;
position: absolute;
display: block;
width: 600px;
height: 70px;
z-index: 9999;
top: -100px;
left: -70px;
box-shadow: -50px 60px 90px 0px #000;
transform: rotate(-5deg)
}
<div class="cont">
<div class="d0"></div>
<div class="d1">
</div>
</div>
Skew parent 然后将 child 以相同的度数倾斜。
* {
box-sizing: border-box
}
body {
padding: 40px 0
}
section {
width: 60vw;
clear: both;
overflow: hidden;
min-height: 100px;
margin: 0 auto;
position: relative;
background: #035076
}
section article {
width: 60%;
padding: 20px;
color: white;
margin: 0 auto
}
section:nth-child(even) {
transform: skew(-45deg) translate(5vw);
box-shadow: inset 0 0 2px 0 black;
}
section:nth-child(odd) {
transform: skew(45deg);
}
section:nth-child(even) article {
transform: skew(45deg) translate(5vw);
}
section:nth-child(odd) article {
transform: skew(-45deg);
}
section:before,
section:after {
content: '';
position: absolute;
}
section:nth-child(even):before {
width: 100%;
height: 0;
bottom: 100%;
left: 0;
z-index: 6;
opacity: 1;
transform: rotate(-10deg) translateY(-30px);
box-shadow: 0px 0px 64px 30px rgba(0, 0, 0, 0.8);
}
section:nth-child(odd):not(:first-child):before {
width: 100%;
height: 0;
bottom: 100%;
right: 0;
z-index: 6;
opacity: 1;
transform: rotate(10deg) translateY(-30px);
box-shadow: 0px 0px 64px 30px rgba(0, 0, 0, 0.8);
}
<section>
<article>What our clients say About Us</article>
</section>
<section>
<article>Read More!</article>
</section>
<section>
<article>Read More!</article>
</section>
<section>
<article>Read More!</article>
</section>