3 tricks for adding texture to your text with CSS and SVG

Default avatar.
December 18, 2014
3 tricks for adding texture to your text with CSS and SVG.
You’re probably familiar with using text to mask an image in Photoshop; it’s the best known way to add some texture, or even an image background, to your text. You can then use that text as an image on your website; however, wouldn’t it be great if you could apply the same effect using just HTML and CSS? The good news is, you can! CSS has introduced properties like background-clip and mask-image which you can use to create similar effects to those you’re creating in Photoshop. On top of that, you could also use SVG to clip an image with text. Today, we’ll look at the options, and even throw in some simple animation. If you’d like to follow along with the code, you can download the files here.

Browser support

Predictably, some of the properties we’ll be using aren’t universally supported, meaning they will fail in browsers like IE and Firefox. The good news is that these properties will fail silently, meaning that these techniques are a progressive enhancement, and fine to use in sites.

Clipping text using background-clip

The first option we’ll look at is the background-clip property. This property will define whether the background will be extended into the border or not. It allows the text of a defined element to clip an image.

The HTML

Our markup is simply an h1 with the class bgClip:

Clip Text

Now, let’s add the magic with CSS…

The CSS

We’ll add texture to our text with an image of the night sky. We’ll also make sure that the text is rendered smoothly using font-smoothing. Note that for this to work the text color must be transparent, so we’ll also use text-fill-color:transparent.
.bgClip {
 background:url('../images/clouds.jpg');
 background-repeat:repeat-x;
 background-position:0 0;
 font-size:200px;
 text-transform:uppercase;
 text-align:center;
 font-family:'Luckiest Guy';
 color:transparent;
 -webkit-font-smoothing:antialiased;
 -webkit-background-clip:text;
 -moz-background-clip:text;
 background-clip:text;
 -webkit-text-fill-color:transparent;
 margin:0;
Now we just want to add a little animation to make the background more enticing:
 -webkit-animation:BackgroundAnimated 15s linear infinite;
 -moz-animation:BackgroundAnimated 15s linear infinite;
 -ms-animation:BackgroundAnimated 15s linear infinite;
 -o-animation:BackgroundAnimated 15s linear infinite;
 animation:BackgroundAnimated 15s linear infinite;
}
 @keyframes BackgroundAnimated {
 from {
 background-position:0 0
 }
 to {
 background-position:100% 0
 }
}
 @-webkit-keyframes BackgroundAnimated {
 from {
 background-position:0 0
 }
 to {
 background-position:100% 0
 }
}
 @-ms-keyframes BackgroundAnimated {<
 from {
 background-position:0 0
 }
 to {
 background-position:100% 0
 }
}
 @-moz-keyframes BackgroundAnimated {
 from {
 background-position:0 0
 }
 to {
 background-position:100% 0
 }
}
Here’s the result:

Clipping text using SVG

The next technique we’ll look at is SVG clipping. Similar to the CSS method above, SVG also allows you to clip text with images using the clipPath property. Usually the clipPath property contains shape attributes such as a circle or square, but you can also use text.

The SVG

You’ll see that I use the HTML image element to clip the image.
 <?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>


 
 
  SVG Text 
 
 






Although I’m using a .jpg here, you can use other image formats, or even video.

The CSS

Now, all we have to do is use the clip-path property to apply the SVG as the image’s clip path:
.svgClipped {
 -webkit-clip-path: url(#svgPath);
 clip-path: url(#svgPath);
 margin:0 auto;
}
Here’s the result:

Masking text using mask-image

The last technique we’re going to look at is texturizing text with mask-image. The basic functionality of this property is that it will clip the area of text that is visible based on opacity.

The HTML

All we need is an h1 element wrapped in a div:

Mask Text

The CSS

To mask the image with the text we’ll use the -webkit-mask-image to specify the image and we’ll also add a nice text-shadow for good measure. Finally, I want to ass some smooth hover effects to reveal the whole text on mouse-over (just because we can):
#maskText h1 {
 font-size: 200px;
 font-family: 'Lilita One', sans-serif;
 color: #ffe400;
 text-shadow: 7px 7px 0px #34495e;
 text-transform: uppercase;
 text-align: center;
 margin: 0;
 display: block;
 -webkit-mask-image: url('../images/texture.png');
 -webkit-transition:all 2s ease;
 -moz-transition:all 2s ease;
 -o-transition:all 2s ease;
 transition:all 2s ease;
}
 #maskText h1:hover{
 -webkit-mask-image: url('../images/texture-hover.png');
 cursor: pointer;
 color: #ffe400;
}
Here’s the result:

Conclusion

Great! You just learned how to clip text on different ways. These properties will save you time and keep you out of Photoshop. More importantly, they’re dynamic, unlike a jpg. CSS and SVG have come a long way, and techniques like these will become standard over the next few years as browsers catch up.

Samuel Norton

Sam Norton is a web designer and a front web developer with over 2 years of experience in the industry. Have a passion for designing detailed, creative and modern websites & graphics. He spend most of his time practically every day, experimenting with HTML, CSS and Wordpress.

Read Next

3 Essential Design Trends, November 2024

Touchable texture, distinct grids, and two-column designs are some of the most trending website design elements of…

20 Best New Websites, October 2024

Something we’re seeing more and more of is the ‘customizable’ site. Most often, this means a button to swap between…

Exciting New Tools for Designers, October 2024

We’ve got goodies for designers, developers, SEO-ers, content managers, and those of you who wear multiple hats. And,…

15 Best New Fonts, September 2024

Welcome to our roundup of the best new fonts we’ve found on the web in the previous four weeks. In this month’s edition…

3 Essential Design Trends, October 2024

This article is brought to you by Constantino, a renowned company offering premium and affordable website design You…

A Beginner’s Guide to Using BlueSky for Business Success

In today’s fast-paced digital world, businesses are always on the lookout for new ways to connect with their audience.…

The Importance of Title Tags: Tips and Tricks to Optimize for SEO

When it comes to on-page SEO, there’s one element that plays a pivotal role in both search engine rankings and user…

20 Best New Websites, September 2024

We have a mixed bag for you with both minimalist and maximalist designs, and single pagers alongside much bigger, but…

Exciting New Tools for Designers, September 2024

This time around we are aiming to simplify life, with some light and fast analytics, an all-in-one productivity…

3 Essential Design Trends, September 2024

September's web design trends have a fun, fall feeling ... and we love it. See what's trending in website design this…

Crafting Personalized Experiences with AI

Picture this: You open Netflix, and it’s like the platform just knows what you’re in the mood for. Or maybe you’re…

15 Best New Fonts, August 2024

Welcome to August’s roundup of the best fonts we’ve found over the last few weeks. 2024’s trend for flowing curves and…