Home >Web Front-end >CSS Tutorial >How to Add Drop Shadows to SVGs Using Only CSS?

How to Add Drop Shadows to SVGs Using Only CSS?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-24 03:19:18893browse

How to Add Drop Shadows to SVGs Using Only CSS?

How to Create Drop Shadows for SVGs Using CSS3

Is it feasible to create drop shadows for SVG elements using CSS3? Something like:

box-shadow: -5px -5px 5px #888;
-webkit-box-shadow: -5px -5px 5px #888;

Discussions on creating shadows using filter effects were encountered. Is there an example that only uses CSS? Below is a functional code where the cursor style is applied appropriately but no shadow effect is present. How can the shadow effect be achieved with minimal code?

svg .shadow {
  cursor:crosshair; 
  -moz-box-shadow: -5px -5px 5px #888;
  -webkit-box-shadow: -5px -5px 5px #888;
  box-shadow: -5px -5px 5px #888; 
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" baseProfile="full"  viewBox="0 0 120 70">  
    <rect class="shadow" x="10" y="10" width="100" height="50" fill="#c66" />
</svg>

Solution

Employ the CSS 'filter' attribute.

Compatible with webkit browsers, Firefox 34 , and Edge.

A polyfill is available to support FF < 34 and IE6 .

Implementation:

.shadow {
  -webkit-filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
  filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
}



  



  
    
    
    
    
  

The above is the detailed content of How to Add Drop Shadows to SVGs Using Only CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn