Add stroke around text - on the outside - with css?

后端 未结 9 1427
走了就别回头了
走了就别回头了 2021-01-03 21:48

I have the following HTML and CSS:

相关标签:
9条回答
  • 2021-01-03 22:25

    One option is to use text-shadow to simulate a stroke. Example:

    text-shadow:
        -1px -1px 0 #000,  
         1px -1px 0 #000,
        -1px  1px 0 #000,
         1px  1px 0 #000;
    
    0 讨论(0)
  • 2021-01-03 22:27

    i know this is an old question but have a look at this:

    https://jsfiddle.net/1dbta9cq/

    you can simply place another text on top of the stroked text with absolute positioning, it's not so tidy but it solves the problem

    <div class="container">
      <h1 class="stroke">Stroke</h1>
      <h1 class="no-stroke">Stroke</h1>
    </div>
    
    .container{
      width:300px;
    }
    
    .stroke{
      position:absolute;
      -webkit-text-stroke: 10px black;
    }
    
    .no-stroke{
      color:white;
      position:absolute
    }
    
    0 讨论(0)
  • 2021-01-03 22:28

    Here's a SASS mixin I created to make it easy to create a text outline using the prefixed properties (-webkit, -moz) when supported, but falling back to just a color with text shadow. Note that the fallback doesn't work well with opaque fill colors, but other than that I think it's a pretty good solution, until we can get a standard method that has better browser support.

    Fiddle

    Mixin

    @mixin text-stroke($fill-color, $stroke-color, $stroke-width) {
      color: $fill-color;
      text-shadow: -$stroke-width -$stroke-width 0 $stroke-color,
                    $stroke-width -$stroke-width 0 $stroke-color,
                   -$stroke-width  $stroke-width 0 $stroke-color,
                    $stroke-width  $stroke-width 0 $stroke-color;
    
      @supports ((-webkit-text-stroke-color: $stroke-color) and (-webkit-text-fill-color: white))
                or ((-moz-text-stroke-color: $stroke-color) and (-moz-text-fill-color: white)) {
                            color: unset;
                      text-shadow: unset;
             -moz-text-fill-color: $fill-color;
          -webkit-text-fill-color: $fill-color;
           -moz-text-stroke-color: $stroke-color;
        -webkit-text-stroke-color: $stroke-color;
           -moz-text-stroke-width: $stroke-width;
        -webkit-text-stroke-width: $stroke-width;
      }
    }
    

    Example Usage

    (Makes text semi-transparent black with a white outline)

    .heading-outline {
      @include text-stroke(rgba(#000,.5), #fff, 1px);
    }
    
    0 讨论(0)
提交回复
热议问题