DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Loader Animations Using Anime.js
  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • React Callback Refs: What They Are and How to Use Them
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Trending

  • DZone's Article Submission Guidelines
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  1. DZone
  2. Coding
  3. Languages
  4. Circle Text With CSS and JavaScript

Circle Text With CSS and JavaScript

See how to give your HTML some circular flair.

By 
John Negoita user avatar
John Negoita
·
Dec. 19, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
20.8K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I will share the CSS and JS code to generate circle text from an HTML text element. This technique makes a span element for each letter and rotates it slightly.  You can generate a circle text by adjusting the font size and circle radius.

Similar techniques have been implemented in online web applications like in the circle text generator in MockoFun.

So, let's see how to write text in a circle using CSS and JavaScript.

Circle Text Code

  • Codepen project: Curved Text CSS
  • GitHub Gist: Curved Text CSS Gist

Circle text

Let's break down the steps to creating curved text or circle text using a little JS and CSS.

Circle Text HTML Code

HTML
 




x


 
1
<div>
2
  <fieldset>
3
    <label>Text:</label>
4
    <input type="text" class="text" value="CIRCLE TEXT">
5
    <label>Radius:</label>
6
    <input class="radius" type="range" min="100" max="1000" value="500">
7
  </fieldset>
8
</div>
9
 
          
10
<div class="curved-text">CIRCLE TEXT</div>
11
 
          



The HTML code is pretty straightforward. The div.curved-text element is actually the only important one because that's where we will write our circle text.

The other div element simply contains the configuration options for the curved text:

  • input text for inputting the curved text content
  • radius slider for adjusting the radius of the circle text

Circle Text CSS Code

CSS
 




x


 
1
.curved-text{
2
  position:relative;
3
  display:inline-block;
4
  margin:0 auto;
5
  font-size:32px;
6
}
7
 
          
8
.curved-text span{
9
  min-width:0.5em;
10
  text-align:center;
11
  padding:30px;
12
  margin:0px;
13
}



For creating the circle text effect, we will only use two CSS classes:

  • .curved-text: CSS class used for the wrapper element where the curved circle text will reside
  • .curved-text span: CSS class used for each individual letters that make up the circle text

Circle Text JavaScript Code

JavaScript
 




xxxxxxxxxx
1
46


 
1
import jquery from "https://cdn.skypack.dev/jquery@~3.5.1";
2
function updateCurvedText($curvedText, radius) {
3
  $curvedText.css("min-width", "initial");
4
  $curvedText.css("min-height", "initial");
5
  var w = $curvedText.width(),
6
    h = $curvedText.height();
7
  $curvedText.css("min-width", w + "px");
8
  $curvedText.css("min-height", h + "px");
9
  var text = $curvedText.text();
10
  var html = "";
11
 
          
12
  Array.from(text).forEach(function (letter) {
13
    html += `<span>${letter}</span>`;
14
  });
15
  $curvedText.html(html);
16
 
          
17
  var $letters = $curvedText.find("span");
18
  $letters.css({
19
    position: "absolute",
20
    height:`${radius}px`,
21
    // backgroundColor:"orange",
22
    transformOrigin:"bottom center"
23
  });
24
  
25
  var circleLength = 2 * Math.PI * radius;
26
  var angleRad = w/(2*radius);
27
  var angle = 2 * angleRad * 180/Math.PI/text.length;
28
  
29
  $letters.each(function(idx,el){
30
    $(el).css({
31
        transform:`translate(${w/2}px,0px) rotate(${idx * angle - text.length*angle/2}deg)`
32
    })
33
  });
34
}
35
 
          
36
var $curvedText = $(".curved-text");
37
updateCurvedText($curvedText,500);
38
 
          
39
function settingsChanged(){
40
  $curvedText.text($(".text").val());
41
  updateCurvedText($curvedText,$(".radius").val());
42
}
43
 
          
44
$(".radius").on("input change",settingsChanged);
45
$(".text").on("input change",settingsChanged);
46
 
          



The JavaScript is where the actual magic of the circle text happens.

First, we import the jQuery library. You could probably get away without using jQuery, but I just find it convenient in this case for manipulating the HTML elements of the circle text setup.

The updateCurvedText() function takes in two parameters:

  • $curvedText, which is a reference to the curved text wrapper element
  • radius: the radius for our curved/circle text

Let's see how this function works and how it creates our circle text:

JavaScript
 




xxxxxxxxxx
1


 
1
...
2
  $curvedText.css("min-width", "initial");
3
  $curvedText.css("min-height", "initial");
4
  var w = $curvedText.width(),
5
    h = $curvedText.height();
6
  $curvedText.css("min-width", w + "px");
7
  $curvedText.css("min-height", h + "px");
8
  var text = $curvedText.text();
9
...



First, we reset the min-width and min-height properties of our wrapper. This will allow us to actually measure the dimensions of our text in its normal straight form.

We do this because when we curve the text, we will end up with an element that we don't want to be smaller than the original text. Otherwise, the element will jump all over the place when adjusting its radius.

We store the width (w), height (h) and also the text for later use.

JavaScript
 




xxxxxxxxxx
1


 
1
...
2
  var html = "";
3
 
          
4
  Array.from(text).forEach(function (letter) {
5
    html += `<span>${letter}</span>`;
6
  });
7
  $curvedText.html(html);
8
...



Next, we need to wrap each letter of the text we want to curve into span elements. We simply go over each character of the text and add a span element around it. Then, we replace the content of our wrapper with the new HTML code.

Please notice that the span elements will use the CSS code we defined earlier.

Just a few more steps and we are done!

JavaScript
 




x


 
1
...
2
  var $letters = $curvedText.find("span");
3
  $letters.css({
4
    position: "absolute",
5
    height:`${radius}px`,
6
    transformOrigin:"bottom center"
7
  });
8
...


Curved text elements


Imagine that each letter of our curved text becomes a rectangle element with the height set to the radius value of our desired circle text. We also need to set the transformOrigin to the bottom center of the rectangle, because we will want to rotate each letter by a certain angle to distribute the letters on a circle.

The image above is just to give you an idea of how the elements are transformed. We add the position:absolute CSS style, because we want all the letters to initially overlap. We want this because we want to rotate those rectangles around the same origin point.

And now, the last step — the actual rotation for writing text in a circle:

JavaScript
 




xxxxxxxxxx
1
10


 
1
...
2
  var circleLength = 2 * Math.PI * radius;
3
  var angleRad = w/(2*radius);
4
  var angle = 2 * angleRad * 180/Math.PI/text.length;
5
  
6
  $letters.each(function(idx,el){
7
    $(el).css({
8
        transform:`translate(${w/2}px,0px) rotate(${idx * angle - text.length*angle/2}deg)`
9
    })
10
  });
11
...



Let me explain the code:

The circleLength variable is not actually used. It's just a reminder of how to calculate the length of the circle.

This helps us determine the angle of a circle slice when we have the length of the slice edge. We simply divide this length by two times the radius of the whole circle.

The length of the circle slice, in our case, is the width of the original text. The idea here is that no matter how you bend text, the letters' combined width will always be the same.

Using the formula mentioned angleRad = w/(2*radius) gives us an angle in radians. CSS likes working with degrees though. 

So, we simply need to transform the radians to degrees. We then divide this by the number of letters in the text. This will give us the angle step we need to rotate each letter.

With the angle calculated, we apply the CSS to translate each letter to the center of the word, and then rotate each letter with the same angle multiplied by the letter's index in the word. 

When we rotate each letter, because we start at the angle value 0, we will end up with a curved text that starts on top. 

That's fine, but it looks a lot better if the text in a circle is centered and balanced to the left and right.

So, we simply need to subtract text.length*angle/2 each time we rotate a letter.

In Conclusion

Now you know how to make curved text or circle text in CSS and JS. 

If you don't want to start coding this yourself, you can use the Codepen snippet, or even better, you can use the MockoFun circle text generator. It's FREE and it gives you lots of flexibility, over 1,500 different fonts, changing colors and sizes with just a few clicks. 

CSS JavaScript Element

Opinions expressed by DZone contributors are their own.

Related

  • Loader Animations Using Anime.js
  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • React Callback Refs: What They Are and How to Use Them
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!