Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 Box shadow size – percent units?

I'm working on a project that needs to use CSS3 box-shadow property. That's fine, but I have found out that spread size of shadow can't be set to a percentage of parent object.

I fully understand that box-shadow is not additive, thus it doesn't take the size of a parent as a reference.

But given the fact that I need to have a fully responsive site with objects scaling fluidly (not only on breakpoints), but this also poses a problem – I can set shadow to spread property only in absolute units (em or px).

Is there any solution to this? I thought about using inner container (for content) within container (for "shadow" – it's without blur), but this creates another problem – vertical centering of inner container.

Any solution? No jQuery please, just pure CSS.

like image 522
Jozko Remen Avatar asked Dec 27 '12 20:12

Jozko Remen


People also ask

How do you scale a box shadow in CSS?

By using Scale we can change the size of the element based on its width and height ratio. Box shadow is used to create a drop shadow beneath the element. In the above code snippet, we have specified scale property that accepts the scale ratio as parameter. Accordingly it changes the height and width of the element.

What is the correct CSS property to set shadow?

The text-shadow property adds shadow to text. This property accepts a comma-separated list of shadows to be applied to the text.

For what CSS3 shadow property is used?

The text-shadow CSS property adds shadows to text. It accepts a comma-separated list of shadows to be applied to the text and any of its decorations . Each shadow is described by some combination of X and Y offsets from the element, blur radius, and color.

Which of the following values are required for the CSS box shadow property?

The offset-x and offset-y values are required for the CSS box-shadow property. The color value is not required, but since the default for the box-shadow is transparent, the box-shadow will not appear unless you specify a color value. The larger the blur-radius value, the bigger the blur.


2 Answers

I have found out that spread size of shadow can't be set to percentage of parent object

True. But you can set font-size on the parent object, then define the object size in ems, and use the same ems to define the box-shadow size.

Or, if your parent object happens to be the window, you can use viewport units: vw and vh.

like image 63
mik01aj Avatar answered Oct 03 '22 20:10

mik01aj


If you're after an inset box-shadow you could use a radial-gradient background to mimic the behaviour. So instead of -

box-shadow: inset 0 0 100% #000; 

You would use -

background: radial-gradient(ellipse at center, rgba(0,0,0,0) 0,rgba(0,0,0,1) 100%); 

Support: FF16+, IE10+, Safari 5.1+

I can't find any definitive answer on when Chrome started supporting the property, but my current version (39.0.2171.65) definitely supports it.

ColorZilla is a pretty useful tool for generating radial (among other) gradients along with the necessary prefixes if you decide to go down this route.

like image 43
shakyjake Avatar answered Oct 03 '22 20:10

shakyjake