如何在元素的一侧添加框阴影?
我需要在某些 block
元素上创建一个框阴影,但仅(例如)在其右侧。我这样做的方法是将带有 box-shadow
的内部元素包装到带有 padding-right
和 overflow:hidden;
的外部元素中,这样阴影的其他三个侧面不可见。
有没有更好的方法来实现这一目标?喜欢 box-shadow-right
吗?
编辑:我的目的是仅创建阴影的垂直部分。与规则 background:url(shadow.png) 100% 0% Repeat-y
的 repeat-y
的作用完全相同。
I need to create a box-shadow on some block
element, but only (for example) on its right side. The way I do it is to wrap the inner element with box-shadow
into an outer one with padding-right
and overflow:hidden;
so the three other sides of the shadow are not visible.
Is there some better way to achieve this? Like box-shadow-right
?
EDIT: My intentions are to create only the vertical part of the shadow. Exactly the same as what repeat-y
of the rule background:url(shadow.png) 100% 0% repeat-y
would do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
是的,您可以使用 box-shadow 规则的shadow spread属性:
第四个属性
-2px
是阴影扩散,你可以用它来改变阴影的扩散,使阴影看起来只在一侧。这也使用阴影定位规则
10px
将其发送到右侧(水平偏移),而0px
将其保留在元素下方(垂直偏移)。5px
是模糊半径:)这里为您提供示例。
Yes, you can use the shadow spread property of the box-shadow rule:
The fourth property there
-2px
is the shadow spread, you can use it to change the spread of the shadow, making it appear that the shadow is on one side only.This also uses the shadow positioning rules
10px
sends it to the right (horizontal offset) and0px
keeps it under the element (vertical offset.)5px
is the blur radius :)Example for you here.
clip-path
现在(2020 年)是在元素的特定侧面实现框阴影的最简单方法之一,特别是当所需效果是在特定边缘处“干净剪切”阴影时 (我相信这就是OP最初想要的),如下所示:...与这样的衰减/减少/细化阴影相反:
只需将以下 CSS 应用于相关元素:
其中:
Tpx
设置顶部边缘Rpx
右侧Bpx
底部Lpx
left对于应隐藏阴影的任何边缘输入值 0,对于应隐藏阴影的任何边缘输入负值(与框阴影模糊半径 -
Xpx
相同)应该显示。clip-path
is now (2020) one of simplest ways to achieve box-shadows on specific sides of elements, especially when the required effect is a "clean cut" shadow at particular edges (which I believe was what the OP was originally looking for), like this:...as opposed to an attenuated/reduced/thinning shadow like this:
Simply apply the following CSS to the element in question:
Where:
Tpx
sets the shadow visibility for the top edgeRpx
rightBpx
bottomLpx
leftEnter a value of 0 for any edges where the shadow should be hidden and a negative value (the same as the box-shadow blur radius -
Xpx
) to any edges where the shadow should be displayed.我自制的解决方案很容易编辑:
HTML:
css:
Demo:
http://jsfiddle.net/jDyQt/103
My self-made solution which is easy to edit:
HTML:
css:
Demo:
http://jsfiddle.net/jDyQt/103
要获得最多两侧的剪切效果,您可以使用具有背景渐变的伪元素。
将为通常构成文档的元素的左侧和右侧添加漂亮的类似阴影的效果。
To get the clipped effect on up to two sides you can use pseudo elements with background gradients.
will add a nice shadow-like effect to the left and right of the elements that normally make up a document.
只需使用 ::after 或 ::before 伪元素即可添加阴影。将其设置为 1px 并将其放置在您想要的任何一侧。下面是顶部的示例。
Just use ::after or ::before pseudo element to add the shadow. Make it 1px and position it on whatever side you want. Below is example of top.
这是我的例子:
Here is my example:
这是一个 codepen 来演示每一方,或者一个工作片段:
Here is a codepen to demonstrate for each side, or a working snippet:
这是我做的一个小技巧。
1. 在我想要创建单侧框阴影的元素正下方创建一个
(在在这种情况下,我想要一个来自底部的
id="element"
的单侧插入阴影)2. 然后我创建了一个常规的
box-shadow 使用负垂直偏移将阴影向上推到一侧。
Here's a little hack that I did.
1. Create a
<div class="one_side_shadow"></div>
right below the element that I want to create the one-side box shadow (in this case I want a one-sided inset shadow forid="element"
coming from the bottom)2. Then I created a regular
box-shadow
using a negative vertical offset to push the shadow upwards to one-side.这可能是一种简单的方法
将其分配给任何 div
This could be a simple way
Assign this to any div
这个网站帮助了我:https://gist.github.com/ocean90/1268328(请注意在该网站上,截至本文发布之日,左侧和右侧已颠倒……但它们按预期工作)。 它们在下面的代码中得到纠正。
This site helped me: https://gist.github.com/ocean90/1268328 (Note that on that site the left and right are reversed as of the date of this post... but they work as expected). They are corrected in the code below.
我所做的是为阴影创建一个垂直块,并将其放置在我的块元素应该所在的位置旁边。然后,这两个块被包装到另一个块中:
jsFiddle 示例此处。
What I do is create a vertical block for the shadow, and place it next to where my block element should be. The two blocks are then wrapped into another block:
jsFiddle example here.
好的,这是再试一次。使用伪元素并在其上应用阴影盒属性。
html:
sass:
https://codepen.io/alex3o0/pen/PrMyNQ
Ok, here is one try more. Using pseudo elements and aplying the shadow-box porperty over them.
html:
sass:
https://codepen.io/alex3o0/pen/PrMyNQ