Explain Codes LogoExplain Codes Logo

Svg changes color when rotated in Safari 10

web-development
responsive-design
performance
best-practices
Anton ShumikhinbyAnton Shumikhin·Jan 27, 2025
TLDR

To bypass the SVG color shift in Safari 10, apply CSS transform using the rotate3d() function without resorting to color filters. Ensure a consistent color by setting the SVG fill property explicitly:

/* Apply 3D rotation, helps when dealing with the 'Safari Effect' */ .svg-fix { transform: rotate3d(0,0,1,30deg); }
<!-- Define fill color directly on SVG to prevent 'rotational color enlightenment'*/ <svg class="svg-fix" fill="#FF0000" ... > <!-- SVG content --> </svg>

Implement this to keep your SVG's color intact when rotation in Safari 10.

Rotation: Mastering the Third Dimension

Let's Rotate this in 3D

Complex rotations can benefit from the rotate3d(0,0,1,deg) function as it provides better rotational control and prevents the unpredictable color-changing phenomenon when rendering in Safari 10:

/* Complex rotations in Safari? No problem! */ .complex-fix { transform: rotate3d(0,0,1,45deg); }

Cross-Browser Testing: A Necessary Evil

SVG rendering differs between browser versions, hence always test your SVGs in as many different browsers as possible, including different versions of Safari. Keep an eye out for unexpected behaviors introduced in Safari 10.

Animation: Keeping Colors in Check

When using animation with SVGs, state the fill color at each keyframe, or restrict animations to transformations to ensure stable colors across the animation timeline:

/* Dancing SVG with stable fill color */ @keyframes rotation-dance { from { transform: rotate3d(0,0,1,0deg); } to { transform: rotate3d(0,0,1,360deg); } }

Rotate, Replace, Repeat

After rotating, Safari 10 might play with your SVG colors, giving you an unnecessary run in the rainbow realm!

Lo and Behold: The Solution

Post-fix, the color of SVG holds, even while rotating at Safari 10 SPD (Super Passable Degree).

Ride the rotate3d Wave

Precision: Taking the Bull's Eye

rotate3d() offers more tight control over rotations and thus assists in keeping the SVG graphics free from unexpected color shifts in Safari 10.

Issues with Subsampling

Color shifts could be related to sub-pixel rendering and rotate3d() appears to handle these issues better, rendering a smoother visual result.

Performance: Rev Up the Engine

Activate hardware acceleration via 3D transformations, which can potentially enhance performance while also working around Safari 10's rendering quirks.

Diving Deeper into CSS

Consistency: The Key to Success

Use SVG presentation attributes inside your markup for consistent rendering across multiple browsers, Safari included.

<circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="blue"/>

Note on SVG Properties

SVG properties like fill-rule and clip-rule play crucial roles in how shapes and colors are rendered, keep an eye on them!