Stroke Text With Css: Easy And Effective Guide

can you put a stroke on text css

Stroked text, also known as outlined text, is a design feature that can be used to improve readability and make text stand out from its background. In graphic and web design, the outside of outlined text is referred to as the stroke, and the inside as the fill. The stroke can be a different colour to the fill and can vary in width or thickness. In CSS, the stroke feature is added to text using the text-stroke property, which is a shorthand for the following properties: text-stroke-width and text-stroke-colour. The text-stroke property is only used with a -webkit- vendor prefix and is not a standard feature. It is supported by approximately 98% of modern browsers but may not be suitable for all projects.

Characteristics Values
Shorthand for text-stroke-width, text-stroke-color
Shorthand for text-fill-color, color
Stroke width 0, thin, medium, thick, length
Stroke color hex, rgb/rgba, hsl/hsla, color names

medshun

Using the -webkit-text-stroke property

The -webkit-text-stroke property is an experimental CSS property that allows users to add a stroke to text. It is not currently included in any W3C or WHATWG specification and is only implemented behind a -webkit vendor prefix. However, future versions of Firefox and Internet Explorer may support the property, likely under their own prefixes.

The -webkit-text-stroke property is a shorthand for two other properties: text-stroke-width and text-stroke-color. The text-stroke-width property takes a unit value (e.g. 1px, 0.125em, 4in) and describes the thickness of the stroke effect, while the text-stroke-color property takes a color value (e.g. hex, RGB/RGBA, HSL/HSLA).

Css

H1 {

  • Webkit-text-stroke-width: 1px;
  • Webkit-text-stroke-color: black;

}

Alternatively, you can use the shorthand -webkit-text-stroke property to set both the width and color in a single declaration:

Css

H1 {

Webkit-text-stroke: 1px black;

}

It is important to note that the -webkit-text-stroke property only supports animating the stroke color and not the stroke width. Additionally, the stroke drawn by -webkit-text-stroke is aligned to the center of the text shape, and there is currently no option to set the alignment to the inside or outside of the shape. This limitation can make it less usable, as the stroke can interfere with the shape of the letters, distorting the original design.

While the -webkit-text-stroke property offers a direct way to add a stroke to text, it has limited browser support. As an alternative, you can use the text-shadow property to simulate a stroke effect. This approach is more widely supported and can be used in browsers like Firefox, Opera, and IE 10. Here is an example of using text-shadow to create a 1px black stroke:

Css

H1 {

Color: white;

Text-shadow: -1px -1px 0 #000, 1px -1px 0 #000,

1px 1px 0 #000, 1px 1px 0 #000;

}

By using multiple text-shadow declarations with different offset values, you can create the effect of a stroke around the text. Keep in mind that this technique may not produce perfect results for thicker strokes, and you might see gaps or issues with the appearance.

In conclusion, the -webkit-text-stroke property provides a way to add a stroke to text in CSS, but it has limited browser support. As a more compatible alternative, you can use the text-shadow property to achieve a similar effect. When using either approach, consider the limitations and test across different browsers to ensure the desired appearance.

medshun

Using the text-shadow property

The `text-shadow` property in CSS is used to add shadows to text. It accepts a comma-separated list of shadows to be applied to the text and any of its decorations.

The `text-shadow` property is specified as a list of shadows. Each shadow is specified as two or three `` values, followed by a `` value. The first two `` values are the `` and `` values, which specify the shadow's horizontal and vertical distance from the text. The third, optional `` value is the ``, which determines the blur of the shadow.

Css

H1 {

Text-shadow: 2px 2px 5px red;

}

In this example, the `h1` heading will have a shadow that is offset by 2px on the x-axis, 2px on the y-axis, and has a blur radius of 5px. The colour of the shadow is red.

You can also specify multiple shadows, separated by commas. Here is an example:

Css

H1 {

Text-shadow: 1px 1px 2px black, 0 0 1em blue, 0 0 0.2em blue;

}

In this example, the `h1` heading will have three shadows applied to it. The first shadow is black and offset by 1px on the x-axis and 1px on the y-axis, with a blur radius of 2px. The second and third shadows are both blue, but have different offsets and blur radii.

The `text-shadow` property can be used to create a variety of text effects, such as neon glows or outlined text. It is a useful tool for adding depth and style to your text elements.

medshun

Using SVG filters

SVG filters are quite different from CSS filters and arguably more powerful, as there are more of them and you can do things like bevel/emboss or stroke which you can't do in CSS filters.

SVG filters can be applied to images (both SVGs and other formats), text, and every other HTML element. CSS filters are mostly limited to images and are fairly easy to use.

To use SVG filters, you can create a filter element with a unique ID and then reference it in the CSS using the filter property. The filter element will hold filter primitives as children, which are elements that take some sort of image or graphic as input and then output it with an effect applied.

Html

Fill="#4691f6"

D="M18.258,3.266c-0.693,0.405-1.46,0.698-2.277,0.857c-0.653-0.686-1.586-1.115-2.618-1.115c-1.98,0-3.586,1.581-3.586,3.53c0,0.276,0.031,0.545,0.092,0.805C6.888,7.195,4.245,5.79,2.476,3.654C2.167,4.176,1.99,4.781,1.99,5.429c0,1.224,0.633,2.305,1.596,2.938C2.999,8.349,2.445,8.19,1.961,7.925C1.96,7.94,1.96,7.954,1.96,7.97c0,1.71,1.237,3.138,2.877,3.462c-0.301,0.08-0.617,0.123-0.945,0.123c0.456,1.402,1.781,2.422,3.35,2.451c-1.228,0.947-2.773,1.512-4.454,1.512c-0.291,0-0.575-0.016-0.855-0.049c1.588,1,3.473,1.586,5.498,1.586c6.598,0,10.205-5.379,10.205-10.045c0-0.153-0.003-0.305-0.01-0.456c0.7-0.499,1.308-1.12,1.789-1.827c-0.644,0.28-1.334,0.469-2.06,0.555C17.422,4.782,17.99,4.091,18.258,3.266" >

In this example, the `` element is defined with the ID "shadow" and contains the `` primitive, which applies the drop shadow effect. The `` primitive has several attributes that can be adjusted to customize the appearance of the drop shadow, such as `dx, `dy`, and `stdDeviation`.

The SVG icon is then styled with the class "svg-icon" and the filter is applied to it using the `filter` property and referencing the ID of the filter element.

You can also apply SVG filters directly within the SVG syntax using the `filter` attribute:

Html

D="..."

Filter="url(#myfilters)"

Html

upgrade yourself

In this example, the `` filter primitive is used to expand the text by dilating its alpha channel. This allows us to add an outline to the text without reducing the thickness of the original text.

SVG filters offer a wide range of creative possibilities and are a powerful tool for enhancing the visual aspects of a website. They provide more flexibility and control compared to CSS filters and are worth exploring for web developers looking to create unique and engaging designs.

medshun

Using SVG text and CSS

To add a stroke to SVG text using CSS, you can use the `text-stroke` property, which is a shorthand for the `text-stroke-width` and `text-stroke-color` properties. The `text-stroke-width` property defines the thickness of the stroke, while the `text-stroke-color` property specifies the color of the stroke. Here's an example:

Css

H1 {

  • Webkit-text-stroke-width: 1px;
  • Webkit-text-stroke-color: black;

}

Alternatively, you can use the `text-shadow` property to create a similar effect. This property allows you to add multiple shadows around the text element, creating the appearance of a stroke. Here's an example:

Css

H1 {

Color: white;

Text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;

}

When using SVG text, you can also control the stroke width, opacity, line caps, line joins, and dash patterns using attributes such as `stroke-width, `stroke-opacity, `stroke-linecap, `stroke-linejoin, and `stroke-dasharray. These attributes allow you to customize the appearance of the stroke to fit your specific needs.

Here's an example of using SVG to create a text stroke:

Html

Svg>

Text y="50" fill="none" stroke="black" stroke-width="0.5px">

Scalable Title

/text

/svg

By utilizing CSS and SVG text, you can add strokes to your web text, creating visually appealing and customizable designs.

medshun

Using HTML5 Canvas

The HTML5 Canvas element can be used to add a stroke to text. The strokeText() method is used to draw text on the canvas. Here is an example of how to use it:

Javascript

Const canvas = document.getElementById("myCanvas");

Const ctx = canvas.getContext("2d");

Ctx.font = "20px Georgia";

Ctx.strokeText("Hello World!", 10, 50);

In the above code, we first get a reference to the canvas element using `getElementById()`. We then use the `getContext()` method to get the 2D rendering context of the canvas. Next, we set the font and font size using the font property. Finally, we call the `strokeText()` method to draw the text on the canvas. The first two parameters of the `strokeText()` method are the x and y coordinates of the starting position of the text.

To set the stroke color of the text, we can use the strokeStyle property of the canvas context. For example:

Javascript

Ctx.strokeStyle = "black";

The default value of `strokeStyle` is `#000000` (solid black).

It is also possible to create a gradient for the stroke using the createLinearGradient() method:

Javascript

Const gradient = ctx.createLinearGradient(0, 0, c.width, 0);

Gradient.addColorStop("0", "magenta");

Gradient.addColorStop("0.5", "blue");

Gradient.addColorStop("1.0", "red");

Ctx.strokeStyle = gradient;

In the above code, we create a linear gradient that starts at the top-left corner of the canvas (`0, 0`) and ends at the top-right corner (`c.width, 0`). We then add three color stops to the gradient using the `addColorStop()` method. The first parameter of `addColorStop()` is the position of the color stop as a percentage, and the second parameter is the color value. Finally, we set the `strokeStyle` property to the gradient.

To set both the fill and stroke for HTML5 canvas text, you must use both the `fillText()` and `strokeText()` methods together. It is good practice to use the `fillText()` method before the `strokeText()` method to ensure that the stroke thickness is rendered correctly.

Stroke and Leukemia: Is There a Link?

You may want to see also

Frequently asked questions

The CSS code to add a stroke to text is as follows:

```css

-webkit-text-stroke: 1px black;

```

This will add a 1px thick black stroke to your text. You can adjust the thickness and colour as desired.

You can use the `text-shadow` property to add a stroke effect to text in CSS without using Webkit. Here is an example:

```css

text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;

```

This will create a 1px thick black stroke around your text. You can adjust the thickness and colour as needed.

Yes, you can add a stroke to text in CSS using SVG. Here is an example:

```html

svg>

text x="50%" y="50%" dominant-baseline="central" text-anchor="middle">

Text

/text

/svg

```

```css

text {

stroke-linejoin: round;

stroke: white;

stroke-width: 12px;

paint-order: stroke;

}

```

This will add a 12px thick white stroke to your text. You can adjust the thickness and colour as needed.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment