Using CSS, how do you create a text stroke outline

2019-08-05 06:41发布

问题:

Below is the code I was using to do a Text Stroke outline of 1px. But how do I get the outline thicker? If I just replace all "1px" with "5px", the result looks crazy.

HTML

<div class="element">
Hello!
</div>

CSS

.element {
color:white;

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

回答1:

You might use SVG as well, though it requires more code:

.element {
  font-size: 50px;
}

svg {
  width: 100%;
  height: 1.3em;
}

svg text {
  fill: pink;
  stroke-width: 8px;
  paint-order: stroke;
  stroke: violet;
}
<div class="element">
  <svg><text x="8px" y="75%">Hello kitty!</text></svg>
</div>



回答2:

You can consider text-stroke but you need to pay attention to browser support

.element {
  color: white;
  font-size:50px;
  -webkit-text-stroke: 5px #000;
}
<div class="element">
  Hello!
</div>