Search code examples
textcss

Text border using css (border around text)


Is there a way to integrate a border around text like the image below?

text border


Solution

  • Use multiple text shadows:

    text-shadow: 2px 0 #fff, -2px 0 #fff, 0 2px #fff, 0 -2px #fff,
                 1px 1px #fff, -1px -1px #fff, 1px -1px #fff, -1px 1px #fff;
    

    enter image description here

    body {
      font-family: sans-serif;
      background: #222;
      color: darkred;
    }
    
    h1 {
      text-shadow: 2px 0 #fff, -2px 0 #fff, 0 2px #fff, 0 -2px #fff,
                   1px 1px #fff, -1px -1px #fff, 1px -1px #fff, -1px 1px #fff;
    }
    <h1>test</h1>

    Alternatively, you could use -webkit-text-stroke, which produces a slightly different result because it modifies the stroke width instead of adding additional shadows around the text. Despite the webkit prefix, it works in most browsers (including Firefox) as of 2022:

    -webkit-text-stroke: 2px #fff;
    

    enter image description here

    body {
      font-family: sans-serif;
      background: #222;
      color: darkred;
    }
    
    h1 {
      -webkit-text-stroke: 2px #fff;
    }
    <h1>test</h1>

    Also, read more at CSS-Tricks.