Text border using css (border around text)

前端 未结 5 1314
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 07:34

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

\"text

相关标签:
5条回答
  • 2020-12-04 07:53

    I don't like that much solutions based on multiplying text-shadows, it's not really flexible, it may work for a 2 pixels stroke where directions to add are 8, but with just 3 pixels stroke directions became 16, and so on... Not really confortable to manage.

    The right tool exists, it's SVG <text> The browsers' support problem worth nothing in this case, 'cause the usage of text-shadow has its own support problem too, filter: progid:DXImageTransform can be used or IE < 10 but often doesn't work as expected.

    To me the best solution remains SVG with a fallback in not-stroked text for older browser:

    This kind of approuch works on pratically all versions of Chrome and Firefox, Safari since version 3.04, Opera 8, IE 9

    Compared to text-shadow whose supports are: Chrome 4.0, FF 3.5, IE 10, Safari 4.0, Opera 9, it results even more compatible.

    .stroke {
      margin: 0;
      font-family: arial;
      font-size:70px;
      font-weight: bold;
      }
      
      svg {
        display: block;
      }
      
      text {
        fill: black;
        stroke: red;
        stroke-width: 3;
      }
    <p class="stroke">
      <svg xmlns="http://www.w3.org/2000/svg" width="700" height="72" viewBox="0 0 700 72">
        <text x="0" y="70">Stroked text</text>
      </svg>
    </p>

    0 讨论(0)
  • 2020-12-04 07:59

    The following will cover all browsers worth covering:

    text-shadow: 0 0 2px #fff; /* Firefox 3.5+, Opera 9+, Safari 1+, Chrome, IE10 */
    filter: progid:DXImageTransform.Microsoft.Glow(Color=#ffffff,Strength=1); /* IE<10 */
    
    0 讨论(0)
  • 2020-12-04 08:00

    Sure. You could use CSS3 text-shadow :

    text-shadow: 0 0 2px #fff;
    

    However it wont show in all browsers right away. Using a script library like Modernizr will help getting it right in most browsers though.

    0 讨论(0)
  • 2020-12-04 08:05
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
    
    0 讨论(0)
  • 2020-12-04 08:09

    Use multiple text shadows:

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

    enter image description here

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

    Alternatively, you could use text stroke, which only works in webkit:

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

    enter image description here

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

    Also read more as CSS-Tricks.

    0 讨论(0)
提交回复
热议问题