My Learning Blog

Border in tailwindCss - 11/14/2023

some details about border in tailwindCss mdx

Border , Outline and Ring

I am a little bit confused when I am learning border ,ring and outline,so what is the differences?

Border

Purpose: Borders are used to define the edges of an element. Usage: Applied using various utility classes to set border width, color, and style. Effect: Modifies the visual appearance of an element by adding borders. Layout Impact: Affects the layout of the document by taking up space.

test
 export default function Bolder() {
  return <div className="border-2  border-solid border-blue-500">test</div>;
}

embed code

<div class="border-2 border-solid border-blue-500">test</div>

direct rendering

test
<label class="block">
  <span class="block after:ml-0.5 after:text-red-500 after:content-['*'] ">
    Email
  </span>
  <input type="email" name="email" class="..." placeholder="you@example.com" />
</label>

Purpose: Outlines are typically used for non-visual purposes, such as indicating focus states for accessibility. Usage: Applied using the outline utility, with customization for color, style, width and offset. Effect: Adds a visible outline around an element, typically used for focus states. Layout Impact: Does not affect the layout of the document.

Using examples is quite standard in this scenario:

<button class="outline-none focus:outline-blue-500">Click me</button>

But this is also working :

<button
  class="outine-1 outline outline-pink-300 focus:outline-none focus:ring-4 focus:ring-violet-300"
>
  Click me
</button>

<button
  class="border border-pink-300 focus:border-none focus:ring-4 focus:ring-violet-300"
>
  Click me
</button>

Outlines are stacked outside border, and can sometimes be used as a “second border”, for effect.

Ring

(creating outline rings with box-shadows)

Purpose: Rings are specifically designed for focus states, providing a more customizable alternative to outlines. Usage: Applied using the ring utility with options for color, offset, opacity, and width. Effect: Adds a customizable focus ring around an element, primarily for focus states. Layout Impact: Does not affect the layout of the document.

<button class="ring ring-blue-500 focus:ring-opacity-50 focus:ring-offset-2">
  Click me
</button>

In summary border is a general-purpose utility for adding borders to elements, outline and ring are more specialized for focus states, with outline being a simpler option and ring offering more fine-grained control over the appearance of focus rings.

Divide

In Tailwind CSS, the divide-y utility is used to add a vertical (y-axis) border between sibling elements. When using divide-y, it is typically applied to a parent container, and each child element will automatically receive a border on its bottom edge.

<div class="divide-y divide-dashed divide-gray-500 hover:divide-solid">
  <p class="py-2">First element</p>
  <p class="py-2">Second element</p>
  <p class="py-2">Third element</p>
</div>

First element

Second element

Third element