In tailwind
<div class="border w-80 h-80 m-80"></div>
and
<div class="outline w-80 h-80 m-80"></div>
look totally different, at least after injecting preflight by using @tailwind base;
in my CSS.
I would like to make the outline case look as similar as possible to the border.
I have thought about adding custom extensions in my tailwind.config.js
, e.g.
module.exports = {
theme: {
extend: {
outlineColor: 'var(--tw-border-color)',
/* or */
outlineColor: {
primary: 'red',
}
}
}
}
But these unfortunately haven’t worked at all,
- I don’t think tailwind sets a
--tw-border-color
variable - using the second try
<div class="outline outline-primary w-80 h-80 m-80"></div>
doesn’t make a red outline.
I would prefer to not have to hardcode the color, the best solution would be that the lone outline
classname always produces an outline that looks the same as the border that the lone border
classname would produce.
I would like to use outline
instead of border so that I can solve the problem of double-borders in a grid in a more convenient way.
2
Answers
I've found a way that works, but it requires adding custom old school CSS.
Just manually copy all of Preflight's border styles onto outlines.
So, I've copypasted Preflight's section on borders into my stylesheet, and edited it to point at the analogous outline styles instead:
You must customize the outline utility in your Tailwind configuration. Unfortunately, by default, Tailwind CSS does not natively tie outline styles to the colour of the border. However, you can achieve this functionality with a combination of custom CSS variables and Tailwind’s configuration.
Here’s a solution to make the outline match the colour of the border:
Step 1: Utilize Custom CSS Variables
Add your own custom CSS variable for the
--tw-border-color
. You can assign this variable using Tailwind’s border utilities.Step 2: Extend the Tailwind Config
Update your
tailwind.config.js
with the following to generate a default outline colour which takes the same variable as the bordercolourr.Step 3 Use the Utility Classes
Now you can apply the outline utility, which will default to the same colour as the border.
Step 4: Optional – Match other border styles
If you also want to add it so that it copies the outline width, use the custom class:
Now, every time you use the outline class, it will inherit from the default border-color as well and will look even more like the border class, thus avoiding the double border problem in grids.