You want to set some text inside the shape of a circle with HTML and CSS? That’s crazy talk, right?
Not really! Thanks to shape-outside
and some pure CSS trickery it is possible to do exactly that.
However, this can be a fiddly layout option. We have to take lots of different things into consideration, like character count, word count, typeface variations, font sizing, font formatting, and responsive requirements to name a few. One size, does not fit all here. But hey, let’s do it anyway.
Here’s the goal: we want to display a <blockquote>
and author attribution (name) inside a circle shape. We also want to make the layout as flexible as we can. This layout won’t require any additional files and keeps the HTML markup squeaky clean.
This is what we’re striving for:
The shape-outside
feature is not supported in Internet Explorer or Microsoft Edge 18 and below at the time of this writing.
First up, the HTML
We’re going to end up needing a wrapper element to pull this off, so let’s use the semantic <blockquote>
as the inner element. The outside wrapper can be a div:
<div class="quote-wrapper">
<blockquote class="text" cite="http://www.inspireux.com/category/quotes/jesse-james-garrett/">
<p>Experience design is the design of anything, independent of medium, or across media, with human experience as an explicit outcome, and human engagement as an explicit goal.</p>
<footer>– Jesse James Garrett</footer>
</blockquote>
</div>
If you’re interested in a deep-dive on the HTML of quotes, you’re in luck. We’re going to set the quote itself in a <p>
and the name of the author inside a <footer>
. We’ve got class names for the CSS styling hooks we’ll need.
Next, some baseline CSS
Let’s start with the div wrapper. First, we’ll set the minimum (responsive) square size at 300px so it fits on smaller screens. then, we’ll add relative positioning (because we will need it later).
.quote-wrapper {
height: 300px;
position: relative;
width: 300px;
}
Now we’ll make the blockquote fill the whole wrapper and fake a circle shape with a radial gradient background. (That’s right, we are not using border-radius
in this example).
.text {
background: radial-gradient(
ellipse at center,
rgba(0, 128, 172, 1) 0%,
rgba(0, 128, 172, 1) 70%,
rgba(0, 128, 172, 0) 70.3%
);
height: 100%;
width: 100%;
}
One thing to note is that 70% displays a much rougher edge. I manually added very small percentage increments and found that 70.3% looks the smoothest.
Now we have our base circle in place. Add these additional style rules to .text
.
.text {
color: white;
position: relative;
margin: 0;
}
Here’s what we have so far:
Giving text the CSS treatment
Let’s style the paragraph first:
.text p {
font-size: 21px;
font-style: italic;
height: 100%;
line-height: 1.25;
padding: 0;
text-align: center;
text-shadow: 0.5px 0.5px 1px rgba(0, 0, 0, 0.3);
}
Since posting this article, others have commented below about an alternate technique using radial-gradient
as the shape-outside
. This yields a smoother semicircular curve over polygons.
Let’s use the blockquote’s ::before
pseudo-element to create our shaping. This is where the shape-outside
property comes into play. We plot out the polygon()
coordinates and float it to the left so the text wraps inside the shape.
.text::before {
content: "";
float: left;
height: 100%;
width: 50%;
shape-outside: polygon(
0 0,
98% 0,
50% 6%,
23.4% 17.3%,
6% 32.6%,
0 50%,
6% 65.6%,
23.4% 82.7%,
50% 94%,
98% 100%,
0 100%
);
shape-margin: 7%;
}
Let’s change the radial background color to red. The path editor polygon points and connecting lines are also blue. We are changing this color temporarily for greater contrast with the editor tool.
background: radial-gradient(
ellipse at center,
rgba(210, 20, 20, 1) 0%,
rgba(210, 20, 20, 1) 70%,
rgba(210, 20, 20, 0) 70.3%
);
I like Firefox’s developer tools because it has super handy features like a shape-outside
path editor. Click on the polygon shape in the inspector to see the active shape in the browser window. Big thumbs up to the Mozilla dev team for creating a very cool interface!
The Firefox shape editor tool also works for clip-path
and <basic-shape>
values.
Here’s what we have at this point:
We can do the same sort of thing for the paragraph’s ::before
pseudo-element. We use the shape-outside
to make the same polygon, in reverse, then float it to the right.
.text p::before {
content: "";
float: right;
height: 100%;
width: 50%;
shape-outside: polygon(
2% 0%,
100% 0%,
100% 100%,
2% 100%,
50% 94%,
76.6% 82.7%,
94% 65.6%,
100% 50%,
94% 32.6%,
76.6% 17.3%,
50% 6%
);
shape-margin: 7%;
}
Looking good, but where did the footer go? It overflowed the <blockquote>
(where the circular colored background is), so we’re unable to see that white text on a white background.
Styling the footer
Now we can style the <footer>
and give it an absolute position to bring it back on top of the circle.
.quote-wrapper blockquote footer {
bottom: 25px;
font-size: 17px;
font-style: italic;
position: absolute;
text-align: center;
text-shadow: 0.5px 0.5px 1px rgba(0, 0, 0, 0.3);
width: 100%;
}
Again, feel free to change the background color to suit your needs.
This is where the fiddly part comes in. The text itself needs to be styled in such a way that the number of words and characters work inside the shape. I used these CSS rules to help make it fit nicely:
font-size
shape-margin
(we have two exclusion areas to adjust)line-height
letter-spacing
font-weight
font-style
min-width
andmin-height
(to size of the.quote-wrapper
container)
Adding the quote mark for some flourish
Did you see the giant quotation mark in the original demo? That’s what we want to make next.
We’ll take advantage of the ::before
pseudo-element for .quote-wrapper
. Yet again, this will take a fair amount of fiddling to make it look right. I found line-height
has a huge effect on the mark’s vertical position.
.quote-wrapper::before {
content: "\201C";
color: #ccc;
font-family: sans-serif, serif;
font-size: 270px;
height: 82px;
line-height: 1;
opacity: .9;
position: absolute;
top: -48px;
left: 0;
z-index: 1;
}
There’s actually a difference between curly (“smart”) quote marks and straight (dumb) ones. I’d suggest using curly quote marks for dialogue and straight quote marks for coding.
Handling responsive styles
We should probably make our quote bigger on larger screens. I’m setting a breakpoint at 850px, but you may want to use something different.
@media (min-width: 850px) {
.quote-wrapper {
height: 370px;
width: 370px;
}
.quote-wrapper::before {
font-size: 300px;
}
.text p {
font-size: 26px;
}
.quote-wrapper blockquote footer {
bottom: 32px;
}
}
There we have it!
We set HTML text inside a circular shape using a combination of old and new CSS techniques to make an appealing <blockquote>
that commands attention. And we achieved our display goal without any additional dependencies, while still keeping the HTML markup clean and semantic.
I hope this article encourages you to explore new layout possibilities with shape-outside
. Stay tuned for shape-inside
.
I’ve used the approach in the article, but matching the size of the circle to the amount of text is very annoying. My current solution involves a —-(var) that can be set by content authors.
Does anyone know a way to detect overflow of the shapes? We could use that to set the size of the circle dynamically.
Yeah, the layout can be annoying to get right. Please see the comment below for an alternate technique using radial-gradient as the shape-outside. I started messing around with the design:
I did a real quick search of the overflow issue. This might help:
https://stackoverflow.com/questions/9333379/check-if-an-elements-content-is-overflowing/34299947
Here’s another approach using radial-gradient as the shape-outside value: https://codepen.io/dannievinther/pen/LaJoaW
Maybe a little simpler..?
The technique comes from Ana Tudor.
Yeah, that’s a great idea (that I didn’t think of) and yields a smoother circular curve.
On the flip side, hopefully the article will introduce a few people to using the Firefox polygon path editor tool.
Would you have the url to the original post/pen by Ana Tudor?
Thanks Dannie.
@Kerry
I definitely agree with the point made on the polygon path editor in Firefox :)
I was able to find the original pen here: https://codepen.io/thebabydino/pen/vPddeq
Unfortunately, at keyword is not supported on iOS.
Should it not be a
cite
tag as opposed to afooter
tag? I think that would make more sense.Also, can we also swap out the
p
for aq
tag? Perhaps these would be more semantic.This article is a deep dive into all that. I think the way it’s being done here is correct.
The markup above is based on the MDN
blockquote
example.Originally I was incorrectly using
cite
for the author attribution. This element should represent the title of a work (like a book).I believe the usage of
p
within blockquote is right. Theq
tag is for short inline quotes inside larger paragraphs of text.This is a really cool technique and something I’m excited to try out soon. The one thing I’m getting really stuck on is vertically centering the content. Anyone have any bright ideas? Pretty much any technique I’ve tried so far (grid, flexbox, table layout, absolute positioning, magic number margins) breaks the layout in one way or another. I’m hoping to roll this out as a WP block editor block, so I need a technique that can handle arbitrary text length (though I’ll let editors deal with keeping it short enough to remain contained by the circle).
I also tried in vain to centre the content vertically. Hopefully, someone else has a simple solution.
Making a generic WP editable block would be a real challenge. I guess one advantage to start with is having a square shape as the base for the content (predictable aspect ratio)?
I’m thinking:
– Check out the top two comments for technique variations that might be more flexible.
– Possibly use shape-margin to increase the exclusion areas? Forcing the text towards the middle.
– Would it be possible to base the size of the circle off the text element (size, character/word count) somehow?
Be great to see what you come up with Mark. Please leave a url to your project/example so we can check it out.
With CSS grid, overlapping grid areas, and side-by-side shape-outside boxes, you can build a Venn diagram:
The information in this article can be the first step in building novel layouts across shapes.
Yeah, that’s cool. I forked and tweaked the circle background colours. What do you think Adrian?
Kerry, I think that looks pretty swell.
and what about center the content vertically ? does anyone have any idea?