How to add shadow only for the border?

shadow css
box-shadow examples
box-shadow generator
box-shadow only bottom
box-shadow: inset
box-shadow opacity
box-shadow css tricks
text-shadow css

I want to add shadow around collectionview cells. But the shadow is around the images and labels in the cell, and no shadow around the cell. Does someone know how to work it out?

    cell.contentView.layer.cornerRadius = 2.0
    cell.contentView.layer.borderWidth = 1.0
    cell.contentView.layer.borderColor = UIColor.clear.cgColor
    cell.contentView.layer.masksToBounds = true

    cell.layer.masksToBounds = false
    cell.layer.shadowColor = UIColor.black.cgColor
    cell.layer.shadowOpacity = 0.5
    cell.layer.shadowOffset = CGSize(width: -1, height: 1)
    cell.layer.shadowRadius = 1

Shadow around the content(not the border)

You are applying shadow to contentView directly hence it is not working. You need to take a view inside your contentView, put all your content in this view and give some padding (e.g. 8pt) from all side.

Suppose I have named this view as vwContainer, then:

@IBOutlet weak var vwContainer: UIView!
let shadowPath = UIBezierPath(rect: vwContainer.bounds)
vwContainer.layer.masksToBounds = false
vwContainer.layer.shadowColor = UIColor.blackColor().CGColor
vwContainer.layer.shadowOffset = CGSizeMake(0.0, 5.0)
vwContainer.layer.shadowOpacity = 0.5
vwContainer.layer.shadowPath = shadowPath.CGPath

If you are trying to achieve any other UI than please update your question with image.

CSS Box Shadow, Used in casting shadows off block-level elements (like divs). .shadow will be on the right of the box, a negative offset will put the shadow on the left of the box. you can get squeeze in a box shadow and only push it off one edge of a box. You can try using it with border-radius to create a curved edge. On Windows 10, each window has a colored window border and a shadow. You can change the color of your window borders and even disable the shadow, if you like.

extension UIView {
    func addShadow(color: UIColor, radius: CGFloat = 1, offset: CGSize) {
        self.layoutIfNeeded()
        self.layer.masksToBounds = false
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOpacity = 0.5
        self.layer.shadowOffset = offset
        self.layer.shadowRadius = radius
        self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
    }
}

Use it as:

let offset = CGSize(width: -1, height: 1)

cell.addShadow(color: .black, offset: offset)

box-shadow, The example shows how you can use that to create multiple borders: (CSS3 only) */ box-shadow: 0 0 0 10px hsl(0, 0%, 80%), 0 0 0 15px  Default value. No shadow is displayed: Play it » h-offset: Required. The horizontal offset of the shadow. A positive value puts the shadow on the right side of the box, a negative value puts the shadow on the left side of the box: Play it » v-offset: Required. The vertical offset of the shadow.

You need to set background color for the contentView as below

cell.contentView.backgroundColor = .white

CSS box-shadow property, Add shadows to different <div> elements: #example1 { The box-shadow property attaches one or more shadows to an element. border: 1px solid #​BFBFBF; Custom Page Border Settings and Styles Other than the box border, you can also apply a border with a shadow, a “3D” style border, and a custom border with mix-and-match options for each side. To do that, select one of the options from the “Setting” section in the “Page Border” tab of the “Borders and Shading” dialog box.

box-shadow, If a border-radius is specified on the element with a box shadow, the box If only two values are given, they are interpreted as <offset-x><offset-y> values. …for a long, straight shadow edge, this should create a color  The CSS text-shadow property applies shadow to text. In its simplest use, you only specify the horizontal shadow (2px) and the vertical shadow (2px): Text shadow effect!

CSS3 Box Shadow, only top/right/bottom/left and all · GitHub, If two boxes are stacked and you want to add a left or right shadow, there will be gaps like in below screenshot. image. with border removed and with darker  Then, click the “Design” tab. In the “Page Background” section of the “Design” tab, click “Page Borders”. The “Borders and Shading” dialog box displays. On the “Page Border” tab, select the type of border under “Setting”. Then, select the “Style”, “Color”, and “Width” of the line in the center section of the dialog box.

Flutter, Adding a shadow or glow to your UI can add a nice finishing touch to the design. It's a centered Container with a circle shape and a border: Tip: If you only care about the decoration, you can use DecoratedBox directly as  the problem is that the light mode windows in your first screenshot do have a border, but it is white and merges with the window background. in dark mode the border is black but the window background is dark grey, so the border is still visible. i'm afraid that the only way is to change the accent colour to something other than black or white.

Comments
  • I don't understand what you want? You want the cell to have a shadow but not the contents of the cell?
  • @Brandon Just now add a picture in the post, that's the shadow with the content, but not the border