Create horizontal rule with text using TailwindCSS
Question:
How to create horizontal rule with text using TailwindCSS? Answer:
<div class="flex items-center">
<div class="grow border-b border-teal-600"></div>
<span class="shrink px-1 pb-1 text-teal-600">Content title</span>
<div class="grow border-b border-teal-600"></div>
</div>
Description:
If you just want to change the colour, line type or thickness of the hr
element, you can simply add the desired Tailwind classes.
For example, here is a code that creates a dashed blue horizontal rule:
<hr class="border-b border-dashed border-blue-600" />
And the result is:
Adding text to Horizontal Rule
To add the text to the hr
element, you need to add slightly more classes and pay more attention to the appropriate values.
<hr
class="border-t-[1px] border-dotted border-red-600 h-1 text-center overflow-visible
after:content-['Content_title'] after:relative after:top-[-14px] after:bg-white after:text-red-600 after:px-1"
/>
The code above will generate the following output:
What to look out for in this solution:
- The space character should not be used in the text, it should be replaced by an underscore
_
character. - The background colour should also be set to match the background colour of the page
A slightly more code-intensive but cleaner solution might be to imitate the hr
element like this:
<div class="flex items-center">
<div class="grow border-b border-teal-600"></div>
<span class="shrink px-1 pb-1 text-teal-600">Content title</span>
<div class="grow border-b border-teal-600"></div>
</div>
And the result is:
Reference:
The Horizontal Rule element reference
Share "How to create horizontal rule with text using TailwindCSS?"
Related snippets:
- Create blinking background with TailwindCSS
- Create a neon effect with TailwindCSS
- Rotate element with TailwindCSS
- Change SVG icon color with TailwindCSS
- Using :not in TailwindCSS
- Specify exact width value in TailwindCSS
- Set element width over 100% using Tailwind CSS
- Stretch children to fill horizontally
- Create a fixed navbar with TailwindCSS
- Use font from local files in TailwindCSS
- Make an element invisible in mobile size in Tailwind CSS
- Create a 20/80 grid with Tailwind CSS
- Select the nth child in TailwindCSS
- Add class to child when hovering on parent in TailwindCSS
- Fill the full height of the screen in TailwindCSS
- Create transition with TailwindCSS
- Create horizontal rule with text using TailwindCSS
- Set background image with TailwindCSS
- How to use CSS variables in TailwindCSS
- Center an absolute element in TailwindCSS
- Remove arrows from number input in TailwindCSS
- How to use !important in TailwindCSS
- Access child elements in TailwindCSS
- Select first child in TailwindCSS
- Create semi transparent background in TailwindCSS
- How to use calc() in TailwindCSS
- Add text shadow with Tailwind CSS
Tags:
create, style, hr, horizontal rule, css, tailwind Technical term:
Create horizontal rule with text using TailwindCSS