Create fixed rounded button by Tailwind

To create a fixed, rounded button using Tailwind CSS, you can combine Tailwind's fixed utility (for positioning) with the rounded classes (for rounding the button). This is useful if you want a button that stays in a fixed position on the screen, such as at the bottom-right corner.

Here’s how to do it:

Example of a Fixed Rounded Button

<button class="fixed bottom-4 right-4 bg-blue-500 text-white px-6 py-3 rounded-full shadow-lg">
  Fixed Button
</button>

Explanation:

  • fixed: Positions the button fixed relative to the viewport. It will stay in the same position even when the user scrolls.
  • bottom-4: Positions the button 1rem (16px) from the bottom of the screen.
  • right-4: Positions the button 1rem (16px) from the right side of the screen.
  • bg-blue-500: Sets the background color to blue.
  • text-white: Sets the text color to white.
  • px-6 py-3: Adds horizontal (px-6) and vertical (py-3) padding for sizing the button.
  • rounded-full: Makes the button fully rounded (pill shape).
  • shadow-lg: Adds a large shadow for a 3D effect.

Fixed Button with Hover Effect

You can also add a hover effect to make the button interactive:

<button class="fixed bottom-4 right-4 bg-green-500 text-white px-6 py-3 rounded-full shadow-lg hover:bg-green-700 transition duration-300">
  Fixed Button
</button>

Explanation of Hover Effect:

  • hover:bg-green-700: Changes the button's background color to a darker green when hovered over.
  • transition duration-300: Smoothens the hover transition over 300ms.

Fixed Floating Action Button Style

If you want the button to be smaller, like a floating action button (FAB), you can modify the size and use an icon instead of text:

<button class="fixed bottom-4 right-4 bg-red-500 text-white p-4 rounded-full shadow-lg">
  <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
  </svg>
</button>

Explanation:

  • p-4: Adjusts padding to make the button smaller and more compact.
  • The button contains an SVG icon, which makes it look like a floating action button (FAB), commonly used in mobile UIs.

Customizing Position:

You can adjust the bottom, right, top, or left classes to change the position of the button:

  • bottom-0 for sticking to the bottom.
  • top-0 for sticking to the top.
  • left-4 for placing the button on the left side.

This fixed, rounded button will remain in position regardless of scrolling and can be used for various actions like scrolling to the top, opening a modal, or performing a quick action.

댓글

이 블로그의 인기 게시물

Using the MinIO API via curl

How to split a list into chunks of 100 items in JavaScript, 자바스크립트 리스트 쪼개기

HTML Inline divisions at one row by Tailwind

Boilerplate for typescript server programing

가속도 & 속도

Gradle multi-module project

How to checkout branch of remote git, 깃 리모트 브랜치 체크아웃

CDPEvents in puppeteer

Sparse encoder

Reactjs datetime range picker