8

I need the text to have a black border.

I tried this,

  <div className="font-bold text-2xl text-white outline-4">
    Hello
  </div>

But it doesn't seem put a border to the text.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69

3 Answers3

13

I found a custom tailwind drop shadow yields the best result:

drop-shadow-[0_1.2px_1.2px_rgba(0,0,0,0.8)]

Just adjust the pixel values and intensity, and use as standard tailwind attribute.

This even works on svgs.

You can check out what an example looks like here

Philipp John
  • 141
  • 1
  • 4
3

Tailwind does not support it yet out of the box, you need to customize your CSS. Either use the experimental text-stroke property, which is not very well supported so I don't recommend it, maybe later, or:

Use text-shadow that works pretty well.

h1 {
  color: white;
  text-shadow:
   -1px -1px 0 #000,  
    1px -1px 0 #000,
    -1px 1px 0 #000,
     1px 1px 0 #000;
}
szabcsee
  • 373
  • 4
  • 12
3

I had this issue as well, but I did not feel that the existing answers gave me a solution that follows Tailwind's utility-first approach.

Instead of creating custom css for a specific element, instead we should create a new tailwind utility class for this purpose.

To do that we alter the main.css tailwind file that holds our configuration and use the @layer base selector.

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  .font-outline-2 {
    -webkit-text-stroke: 2px black;
  }
  .font-outline-4 {
    -webkit-text-stroke: 4px black;
  }
}

This allows us to reference our new class anywhere in our project alongside default tailwind classes, and it additionally works with Tailwind state selectors.

<h1 class="text-2xl font-bold font-outline-2 hover:font-outline-4">
  Works as expected
</h1>
Hawful
  • 41
  • 2