【问题标题】:How can I create this angled shadow (without images)?如何创建这个有角度的阴影(没有图像)?
【发布时间】:2013-11-03 23:31:24
【问题描述】:

我希望减少外部依赖——尤其是尽可能用 CSS 效果替换图像,例如在这种情况下:

HTML 和 CSS (as this fiddle shows) 如下。

HTML

<div id="one">
  <div></div>
</div>

CSS

#one{
  width: 940px;
  height: 350px;
  padding: 0 10px;
  margin: 10px 0;
  position: relative;
}

#one{
  background-image: url("http://webhost.ischool.uw.edu/~joatwood/portfolio/images/slider-shadow.png");
}

#one > div{
  background-color: purple;
  width: 100%;
  height: 100%;
}

我想要实现的是获得这三样东西:

  1. 仅在左右边缘从元素上脱落的倾斜阴影(否则无法隐藏溢出,该元素内的其他伪元素必须超出其边缘)
  2. 阴影应该从黑色渐变为白色,因为它在垂直方向上离中间更远
  3. 阴影不应该使用直边 - 它应该有点模糊,就像你应用 box-shadow: 5px 5px 5px 时的阴影一样

到目前为止,我已经尝试了两种方法,但都没有完全成功:

  1. I used transform: skewX on two container-sized pseudo-elements 带有渐变背景 - 它在 #1 和 #2 上完全成功,但我想不出用它实现 #3 的方法。
  2. I used transform: rotate on the afore-mentioned pseudo-elements 并尝试应用一些框阴影来模拟模糊 - 这种尝试仅在 #3 上成功,就 #1 和 #2 而言根本不起作用。

我可以使用 CSS3,只要它适用于当前的主流浏览器(Firefox、Chrome),但是我不能将任何伪元素应用于内部 &lt;div&gt;,因为它们'重新用于页面上的另一个设计元素。

【问题讨论】:

  • 添加一个box-shadow,并通过:before/:after覆盖三角形来掩盖它?
  • @JoshC Like this?除了看起来很奇怪的box-shadow 的剪裁(奇怪的深黑线),我不能限制box-shadow 垂直延伸,也不能让阴影更暗/更亮,因为它离容器中间更近/更远垂直。
  • 是的,这看起来不太好。不过我发现了一篇有趣的文章。我没有意识到你可以有多个 box-shadow 行.. nicolasgallagher.com/css-drop-shadows-without-images/demo
  • @JoshC 嗯,可能很有用。如果以后没人弄明白,我会考虑其中的一些例子再试一次。

标签: html css pseudo-element


【解决方案1】:

真的不打算解释太多,因为你已经知道它是如何工作的。对于那些不这样做的人 - 基本上,我在父级上添加了linear-gradient 背景。然后我在每一侧覆盖了 CSS 三角形(通过:before/:after 添加)以获得所需的外观。

jsFiddle example - 跟我很像。

它适用于所有主流浏览器。虽然它在 Chrome 中看起来更好,因为 FF 在三角形上生成锯齿状边缘。

HTML

<div id="two">
  <div></div>
</div>

CSS

#two {
    width: 940px;
    height: 350px;
    padding: 0;
    margin-left: 10px;
    position: relative;
    background: rgb(255,255,255);
    background: -moz-linear-gradient(top,  rgba(255,255,255,1) 2%, rgba(0,0,0,1) 40%, rgba(0,0,0,1) 60%, rgba(255,255,255,1) 98%);
    background: -webkit-linear-gradient(top,  rgba(255,255,255,1) 2%,rgba(0,0,0,1) 40%,rgba(0,0,0,1) 60%,rgba(255,255,255,1) 98%);
    background: linear-gradient(to bottom,  rgba(255,255,255,1) 2%,rgba(0,0,0,1) 40%,rgba(0,0,0,1) 60%,rgba(255,255,255,1) 98%);
}

#two > div {
    background-color: purple;
    width: 920px;
    height: 100%;
    margin: 0px auto;
}

#two:before, #two:after {
    content: "";
    width: 10px;
    height: 0px;
    border-top: 175px solid transparent;
    border-bottom: 175px solid transparent;
    position: absolute;
    top: 0;
}

#two:before {
    left: 0px;
    border-left: 10px solid white;
}

#two:after {
    right: 0px;
    border-right: 10px solid white;
}

让我知道您的想法 - 据我所知,它们看起来一样。可以稍微调整渐变以使它们相同。

【讨论】:

  • 这看起来与我提供的 transform: skew 示例非常相似(尽管它似乎在 webkit 浏览器中不起作用?screenshot for posterity),但就像它一样,它缺少阴影/模糊效果。 (提醒我,我现在会在问题中澄清这一点)
  • @Nightfirecat 我的错 - 没有看到因为我使用的是 Chrome .. 我试图弄清楚如何在不影响子元素的情况下使用 filter: blur(5px);。见jsfiddle.net/FKgnH
  • @Nightfirecat 相当骇人听闻,但请检查一下.. 注意 - 它目前仅适用于 Chrome,因为 blur 似乎不适用于 FF.. jsfiddle.net/9a2t7
【解决方案2】:
<style type="text/css">
#container {
    width: 350px;
    height: 250px;
    position: relative;
}
#box {
    height: 100%;
    width: 100%;
    background-color: #909;
    position: absolute;
}
.shadow {
    width: 10px;
    height: 50%;
    position: absolute;
    background: -moz-linear-gradient(top,  rgba(0,0,0,0) 0%, rgba(0,0,0,0.9) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0)), color-stop(100%,rgba(0,0,0,0.9))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0.9) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0.9) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0.9) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(0,0,0,0) 0%,rgba(0,0,0,0.9) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00000000', endColorstr='#e6000000',GradientType=0 ); /* IE6-9 */
    z-index: -1;
}
#TL {
    transform:rotate(-2deg);
    -ms-transform:rotate(-2deg); /* IE 9 */
    -webkit-transform:rotate(-2deg); /* Safari and Chrome */
    left: -3px;
}
#TR {
    transform:rotate(2deg);
    -ms-transform:rotate(2deg); /* IE 9 */
    -webkit-transform:rotate(2deg); /* Safari and Chrome */
    right: -3px;
}
#BR {
    transform:rotate(178deg);
    -ms-transform:rotate(178deg); /* IE 9 */
    -webkit-transform:rotate(178deg); /* Safari and Chrome */
    right: -3px;
    bottom: 0px;
}
#BL {
    transform:rotate(182deg);
    -ms-transform:rotate(182deg); /* IE 9 */
    -webkit-transform:rotate(182deg); /* Safari and Chrome */
    left: -3px;
    bottom: 0px;
}
</style>

<div id="container">
    <div id="box"></div>
    <div class="shadow" id="TL"></div>
    <div class="shadow" id="TR"></div>
    <div class="shadow" id="BR"></div>
    <div class="shadow" id="BL"></div>
</div>

【讨论】:

  • 这看起来与the first example I gave 非常相似,但它不使用伪元素... 这种方法是否允许像background-image 示例中那样模糊阴影?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-08
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 2011-12-25
相关资源
最近更新 更多