The Wayback Machine - https://web.archive.org/web/20240717024547/https://www.geeksforgeeks.org/how-to-add-stroke-using-css/
Open In App

How to Add Stroke using CSS ?

Last Updated : 21 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

CSS “stroke” typically refers to the visual effect created by the border property, outlining elements with color and style. This can be useful for emphasizing or highlighting certain elements on a webpage. Adding a stroke to an element can be achieved using various approaches and CSS properties.

There are several methods that can be used to add strokes using CSS

  • Using text-stroke Property
  • Using border Property

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using text-stroke Property

In this approach we are using the text-stroke property, to create a text outline with CSS. It adds an outline or stroke to text, specifying its width and color

Syntax:

selector {
-webkit-text-stroke: <width> <color>;
text-stroke: <width> <color>;
}
  • selector refers to the HTML element or class to which the stroke needs to be applied.
  • <width> specifies the width of the stroke in pixels.
  • <color> defines the color of the stroke.

Note: The text-stroke property is currently only supported in WebKit browsers (such as Chrome and Safari). For other browsers, the text-stroke property can be achieved using the -webkit-text-stroke property.

Example: In this example, we are using the above-explained approach.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        h1 {
            -webkit-text-stroke: 2px green;
            text-stroke: 2px black;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
</body>
  
</html>


Output:

Screenshot-2023-07-17-134757
Approach 2: Using border Property

In this approach, we are using the border property to create a stroke-like effect around an element. The border property in CSS allows the styling of an element’s border. It defines its width, style, and color. Used to create borders around elements or as a decorative element.

Syntax:

selector {
border: <width> solid <color>;
}
  • selector refers to the HTML element or class to which the stroke needs to be applied.
  • <width> specifies the width of the stroke in pixels.
  • <color> defines the color of the stroke.

Example: In this example, we are using the above explain approach.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        .box {
            border: 3px solid green;
            width: 200px;
            height: 100px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <div class="box"></div>
</body>
  
</html>


Output:

Screenshot-2023-07-17-135809



Similar Reads

How to add stroke width of text canvas using Fabric.js ?
In this article, we are going to see how to add stroke width to the text canvas using FabricJS. The canvas means text written is movable, rotatable, resizable, and can be stretched. But in this article, we will add a stroke width. Further, the text itself cannot be edited like a textbox.Approach: To make it possible, we are going to use a JavaScrip
2 min read
Tailwind CSS Stroke Width
This class accepts lots of value in tailwind CSS in which all the properties are covered in form. This class is used to set the width of a border in an SVG shape. This property can only be applied to elements that have a shape or text content elements. In CSS, we did that by using the CSS Stroke-width property. Stroke Width classes: stroke-0: This
1 min read
CSS | stroke-width Property
The stroke-width property is used to set the width of a border in a SVG shape. This property can only be applied to elements that have a shape or are text content elements. Syntax: stroke-width: &lt;length&gt; | &lt;percentage&gt; Property Values: length: It is used to set the stroke width in measurement units. It can take values in whole numbers o
2 min read
CSS | stroke-linecap Property
The stroke-linecap property is used to define the shape that is used at the end of open subpaths. Syntax: stroke-linecap: butt | round | square | initial | inherit Property Values: butt: It is used to indicate that the stroke will not extend beyond the endpoints of the stroke. It makes the stroke appear to end at a sharp right angle. Example: &lt;!
3 min read
CSS | stroke-opacity Property
The stroke-opacity property is used to set the opacity of the color or pattern that is applied to the stroke of a shape. A decimal or percentage value could be used to make the stroke visible, invisible or semi-transparent. Syntax: stroke-opacity: [0-1] | &lt;percentage&gt; Parameters: This property accepts two types of parameters as mentioned abov
3 min read
CSS stroke-dashoffset Property
The stroke-dashoffset property is used to set the location along an SVG path where the dash pattern of a stroke will begin. A higher value means that the dash will start at a further location. Syntax: stroke-dasharray: &lt;length&gt; | &lt;percentage&gt; Property Values: length: It is used to set the offset in length units. The values are resolved
2 min read
CSS stroke-dasharray Property
The stroke-dasharray property is used to set the pattern of dashes and gaps used in the stroke of SVG shapes. A larger value indicates a larger number of dashes. Different values can be specified in the array parameter to change the pattern. Syntax: stroke-dasharray: &lt;dasharray&gt; | none Property Values: dasharray: It is used to set the pattern
2 min read
CSS | stroke-linejoin Property
The stroke-linejoin property is an inbuilt property used to define the shape that is used to end an open sub-path of a stroke. Syntax: stroke-linejoin: miter | miter-clip | round | bevel | arcs | initial | inherit Property Values: miter: It is used to indicate that a sharp corner would be used to join the two ends. The outer edges of the stroke are
4 min read
CSS | stroke-miterlimit Property
The stroke-miterlimit property is used to represent the limit on the ratio of the miter length to the stroke-width, that is used to draw a miter join. This property is used in situations when the miter extends beyond the thickness of the line. If this limit specified is exceeded, the 'miter' type of the join is converted to 'bevel'. This will crop
2 min read
CSS stroke-color property
CSS stroke-color property sets the color of stroke as of border. Note: Since no browser supports stroke-color property, therefore, we use stroke as a shorthand property, to define the color of the stroke. Syntax: stroke-color: currentColor /* Or */ stroke-color: color-name /* Or */ stroke-color: rgb value /* Or */ stroke-color: hexadecimal value Pr
2 min read
three90RightbarBannerImg