【问题标题】:How can I simulate a basic drop shadow using border-image and linear-gradient?如何使用边框图像和线性渐变模拟基本投影?
【发布时间】:2017-02-15 22:06:21
【问题描述】:

我想使用边框图像和线性渐变来模拟阴影效果(出于滚动性能的原因,我没有使用原生的盒子阴影效果)。

从下面的示例和fiddle 中可以看出,我尝试的方法包括使用border-image 作为渐变,使用border-image-outset 将阴影移出内容框,使用border-width 来显示只有底部边缘。

诚然,我不太了解边框图像,尤其是在将它与线性渐变一起使用时。通过反复试验,我取得了似乎令人满意的结果。但事实证明,当 div 的宽度足够短时,“阴影”就完全消失了。

我可以做些什么来实现像顶框一样的阴影,但无论框大小如何都可以工作?非常感谢您的帮助!

.box
{
  /* the "shadow" */
  border-image:  linear-gradient(to bottom, rgba(0,0,0,0.5) 0%, rgba(0,0,0,0.5) 10%, rgba(0,0,0,0) 100%) 100 repeat;
    border-image-outset: 0px 0px 6px 0px;
    border-width: 0px 0px 6px 0px;
    border-style: solid; 
  
  /* other stuff */
  font-family: sans-serif;
  font-size: 20px;
  color: #FEFEFE;
  background: #007277;
  margin: 10px 0px;
  float: left;
  clear: left;
  padding: 50px;
}
<div class="box">
Here's longer text, where the "shadow" appears how I want it to.
</div>

<div class="box">
Short
</div>

【问题讨论】:

标签: html css border-image


【解决方案1】:

要使短边框起作用,您需要更改

100 repeat;

0 0 100 0 repeat;

.box
{
  /* the "shadow" */
  border-image:  linear-gradient(to bottom, rgba(0,0,0,0.5) 0%, rgba(0,0,0,0.5) 10%, rgba(0,0,0,0) 100%) 0 0 100 0 repeat;
    border-image-outset: 0px 0px 6px 0px;
    border-width: 0px 0px 6px 0px;
    border-style: solid; 
  
  /* other stuff */
  font-family: sans-serif;
  font-size: 20px;
  color: #FEFEFE;
  background: #007277;
  margin: 10px 0px;
  float: left;
  clear: left;
  padding: 50px;
}
<div class="box">
Here's longer text, where the "shadow" appears how I want it to.
</div>

<div class="box">
Short
</div>

此链接可能对您的边界成像有所帮助https://css-tricks.com/understanding-border-image/

【讨论】:

  • 太好了,谢谢!我会更好地了解边界图像。让我有点困惑的是线性梯度如何没有图像大小,那么切片百分比指的是什么?边框宽度是否决定线性渐变的“图像大小”?
  • 我做了一些研究,我相信border-image-width 决定了切片百分比。我发现这个其他链接对理解border-image 的工作方式最有帮助。 tympanus.net/codrops/css_reference/border-image
猜你喜欢
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
相关资源
最近更新 更多