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

推荐订阅源

L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
TaoSecurity Blog
TaoSecurity Blog
博客园_首页
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
The Exploit Database - CXSecurity.com
量子位
Project Zero
Project Zero
A
Arctic Wolf
小众软件
小众软件
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
K
Kaspersky official blog
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
人人都是产品经理
人人都是产品经理
AI
AI
Cisco Talos Blog
Cisco Talos Blog
MyScale Blog
MyScale Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
S
Schneier on Security
P
Palo Alto Networks Blog

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks Single Element Loaders: The Bars | CSS-Tricks
How to create background pattern using CSS & conic-gradient
2022-07-03 · via CSS Articles by Temani Afif

How to create background pattern using CSS conic gradient

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

Having a good background pattern can make a difference in your website design. Thanks to CSS gradients, we can create fancy and cool patterns using a few lines of code. In this post, we will study the conic-gradient and use it to create different CSS patterns.

How does conic-gradient work?

From the specification

A conic gradient starts by specifying the center of a circle, similar to radial gradients, except that conic gradient color-stops are placed around the circumference of the circle, rather than on a line emerging from the center, causing the color to smoothly transition as you spin around the center, rather than as you progress outward from the center.

And here is a figure from the MDN page

color stops along the circumference of a conic gradient and the axis of a radial gradient.

Let’s take a basic example to see how it works:

background: conic-gradient(at 75% 25%, red, blue, yel

We place the center point at X=75% and Y=25% then we create a color transition between red, blue, and yellow. As simple as that!

In most cases, we don’t need a smooth transition but rather “hard stop” colors. To do this, we need to make the color start where the previous one ends:

background: conic-gradient(at 75% 25%, red 90deg, blue 90deg 225deg, yellow 225deg)

Note the use of the same angle between two colors (90deg and 225deg). We can still do better like below

background: conic-gradient(at 75% 25%, red 90deg, blue 0 225deg, yellow 0)

By specifying 0 (a value smaller than the previous one) the browser will automatically make it equal to the previous one. This will avoid us writing the same value twice.

We still have the same center but no transition between the colors. The red is filling 90deg. The blue starts from where the red ends and continues until 225deg so it’s filling 135deg. The yellow is filling the remaining space which is also 135deg (360deg - 225deg).

If we define a background-size, we create a pattern:

Not the best pattern but it illustrates the technique we are going to use in this article to build better ones!

50%

💰 SAVE 50% ON VERPEX YEARLY MANAGED HOSTING PLANS FOR WORDPRESS

with the discount code

SERVERS-SALE

Checkerboard/Chessboard pattern

Let’s start with a basic pattern:

Illustration of the chess pattern

Below is a figure to understand the logic of this pattern:

Illustration of the chess pattern

It’s clear that the center of our circle is placed at the center of the background area (the default position) so we don’t need to define it. Then we have 4 colors (2 whites and 2 blacks) each one filling 90deg (25% of the area). The code will be:

background: conic-gradient(#000 90deg, #fff 0 180deg, #000 0 270deg, #fff 0);

We have repetition so we can optimize the syntax using a repeating gradient and write the colors only once

background: repeating-conic-gradient(#000 0 90deg, #fff 0 180deg);

We can also use a percentage instead of a degree

background: repeating-conic-gradient(#000 0 25%, #fff 0 50%);

Then all we have to do is to update the background-size to control the pattern.

Zig-Zag pattern

Let’s move to a more fancy pattern where we will use two gradients this time

Illustration of the zig-zag pattern

Note that in the previous demos, I am using 90.5deg instead of 0deg to avoid jagged edges. Gradients are known to have such issues so we always update values that way to fix them.

Paper Graph pattern

Illustration of the graph pattern pattern

For this one, we are going to use two similar gradients but with different sizes this time. In addition to the background-position, we will see that playing with background-size can help us create patterns as well.

The first gradient is defined as follows:

And it gives us the following result

The second gradient is the same but instead of 2px 2px we use 1px 1px to have thinner lines and we divide the size by 5. This will give us the logical result of the small squares (a 5x5 grid) inside a bigger one.

background: 
 conic-gradient(from 90deg at 2px 2px,#0000 90deg,#366 0) 0 0/100px 100px,
 conic-gradient(from 90deg at 1px 1px,#0000 90deg,#366 0) 0 0/20px  20px;

And using CSS variables we can optimize like below:

--s: 100px; /* control the size */
  
--_g: #0000 90deg,#366 0; /* update the color here */
background: 
 conic-gradient(from 90deg at 2px 2px,var(--_g)) 0 0/var(--s) var(--s),
 conic-gradient(from 90deg at 1px 1px,var(--_g)) 0 0/calc(var(--s)/5) calc(var(--s)/5);

UP TO 70% OFF ALL VERPEX RESELLER HOSTING PLANS

with the discount code

AWESOME

Triangular pattern

After those three simple patterns, we can tackle more complex patterns using the same techniques. Let’s create some triangular patterns

Illustration of the triangular pattern

For this one, we get the following for the gradients

Illustration of the triangular pattern

Similar to the Zig-Zag pattern, we consider an offset for the second gradient using background-position to get the following code:

--s: 120px; /* control the size */
  
background:
  conic-gradient(from 150deg at 50% 33%,#FA6900 60deg,#0000 0) 
      calc(var(--s)/2) calc(var(--s)/1.4),
  conic-gradient(from -30deg at 50% 66%,#D95B43 60deg,#ECD078 0);
background-size: var(--s) calc(var(--s)/1.154);

Here are two more patterns with triangular shapes. For these, I will not give you a figure to illustrate each gradient. I let you dissect the code and try to understand the puzzle.

The code may look complex at first glance but if you follow simple steps you can easily understand it. First, try to isolate each gradient alone to understand the internal configuration (the colors stops and the center position) then place them above each other by disabling the repetition (set background-repeat: no-repeat) to see the pattern.

Stars pattern

Why not a pattern with stars!

Here is a step-by-step demo to illustrate the different gradients

As you can see, each gradient alone is pretty easy to understand and the combination of all gives us a good result. This is the power of gradients, we combine basic shapes to get a complex-looking pattern.

More patterns!

Before we end, I will leave you with a bunch of other examples to show you how powerful conic-gradient() is when it comes to creating fancy patterns!

50%

💰 EXTRA 50% OFF ALL VERPEX YEARLY MANAGED CLOUD SERVER PLANS

with the discount code

SERVERS-SALE

Conclusion

After the different examples we saw, you should be able to create cool patterns using conic-gradient(). You may find it difficult but with some practice, you will get comfortable with the different techniques and you can easily create your patterns.

I have another article that deals with radial-gradient() if you want a follow-up on this one: https://www.freecodecamp.org/news/css-radial-gradient/

Combining different gradients is another way to get even more patterns. I have made a lot of examples that you can find at css-pattern.com where I regularly publish new patterns. it’s also a Github Repository so you can Watch it and get notified when a new pattern is added.

Frequently Asked Questions

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif