惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
博客园_首页
Vercel News
Vercel News
WordPress大学
WordPress大学
美团技术团队
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Y
Y Combinator Blog
博客园 - 【当耐特】
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
腾讯CDC
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Recent Announcements
Recent Announcements
雷峰网
雷峰网
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
Jina AI
Jina AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
P
Privacy & Cybersecurity Law Blog
Recorded Future
Recorded Future
Help Net Security
Help Net Security
N
News and Events Feed by Topic
博客园 - Franky
P
Proofpoint News Feed
L
LINUX DO - 热门话题
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
D
Docker
Google DeepMind News
Google DeepMind News
有赞技术团队
有赞技术团队
IT之家
IT之家
Security Latest
Security Latest
L
LangChain Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks

1A23 Blog

Progress Bar Labels with CSS Anchor Positioning Shift-JIS / UTF-8 文字化け解読:実はもうちょっと読めるかも DAM Karaoke in Round1 Tukwila from the View of a Vocaloid Enjoyer ひとりのボカロファンから見る Round1 Tukwila の DAM カラオケ Reverse engineering an IL2CPP NSO binary: Case study of Mojipittan Encore Use WebVTT without actually using WebVTT: Another way to monitor playback progress of HTML Media Elements Flexible and dynamic flow control of Azure DevOps YAML Pipelines using variables Content-aware Infinite Scroll Loop using JavaScript 『初音ミクの消失合作』セリフ文字起こし
Rotated Background Patterns in CSS with SVG
Eana Hufwe · 2025-03-07 · via 1A23 Blog

While working on the WordPress theme Tìtsyul Amip Twenty Twenty-Five, a need arose for a more flexible way to implement rotated background patterns in CSS. Existing methods often involve trade-offs, and this exploration led to a technique leveraging the power of SVG.

Existing Approaches and Their Limitations

Traditionally, achieving rotated background images in CSS has involved several workarounds, each with its own set of challenges:

  • CSS Transforms: Using the transform: rotate() property. This rotates the entire element, including its content, which is often not the desired outcome.
  • Pseudo-elements: Employing ::before or ::after pseudo-elements to create a rotated background. While this allows rotating the background independently of the content, it can be complex to manage sizing and positioning. Additionally, if the html or body elements have a background color, pseudo-elements with a negative z-index may fall behind them.
  • Pre-rotated Images: Rotating the image using image editing software and then using the rotated image as the background. This is impractical for seamless patterns in most rotation angles or when dynamic canvas size is required.

The SVG Pattern Technique

Building on these insights, let’s now explore a method that leverages SVG to achieve rotated background patterns—a solution that delivers both flexibility and precise control.

At the core of this approach is the SVG <pattern> feature, a versatile tool that enables the creation of seamless, repeating background patterns. The <pattern> element defines a tileable area that can be used as a fill for other SVG shapes, allowing for intricate and scalable designs. By leveraging attributes such as patternTransform, you can easily manipulate these patterns with transformations like rotation, scaling, and translation, making it an ideal choice for dynamic and responsive backgrounds.

Draw the Pattern Using SVG Begin by creating the desired pattern using SVG elements. This can include vector graphics or embedded bitmaps.

<polygon points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2" />

Wrap in a <pattern> Definition Encapsulate the pattern within a <pattern> element inside the <defs> section of the SVG, specify the dimensions for the pattern, and use the patternTransform attribute to apply transformations, such as rotation.

<defs>

<pattern id="star" width="10" height="10" patternUnits="userSpaceOnUse" patternTransform="rotate(20)">

<polygon points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2" />

</pattern>

</defs>

Create the Main SVG Create a new SVG element without a viewBox attribute. Set the width and height to 100% to ensure it covers the entire area of the element. Then add a <rect> element to cover the entire SVG area, and set its fill attribute to reference the pattern defined in step 2.

<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">

<defs>

<pattern id="star" width="10" height="10" patternUnits="userSpaceOnUse" patternTransform="rotate(20)">

<polygon points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2" />

</pattern>

</defs>

<rect width="100%" height="100%" fill="url(#star)" />

</svg>

Encode and Use in CSS Encode the SVG as a data URL. Use the data URL as the background-image in your CSS.

div {

background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25'%3E%3Cdefs%3E%3Cpattern id='star' width='10' height='10' patternUnits='userSpaceOnUse' patternTransform='rotate(20)'%3E%3Cpolygon points='0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2' /%3E%3C/pattern%3E%3C/defs%3E%3Crect width='100%25' height='100%25' fill='url(%23star)' /%3E%3C/svg%3E");

}

Using this technique, the pattern background can be transformed on its own, while still behave like any other CSS background to enjoy other features like background stacking and blend mode settings.

This is an example of how a background using this technique could look like.

SVG Rotated Background Generator

You can also try it out using the generator below to create custom rotated background patterns with SVG. You can also adjust parameters such as pattern size, scaling, and rotation angle.