- Published on
How to Build A Responsive fixed top navigation With Tailwind CSS?

- What is Tailwind CSS?
- The description of Responsive fixed top navigation ui component
- Why use Tailwind CSS to create a Responsive fixed top navigation ui component?
- The preview of Responsive fixed top navigation ui component
- The source code of Responsive fixed top navigation ui component
- How to create a Responsive fixed top navigation with Tailwind CSS?
- Install tailwind css of verion 1.7.0
- All the unility class needed to create a Responsive fixed top navigation component
- 31 steps to create a Responsive fixed top navigation component with Tailwind CSS
- Conclusion
What is Tailwind CSS?
Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.
The description of Responsive fixed top navigation ui component
Responsive fixed top navigation with plain javascript. icon by heroicons
Why use Tailwind CSS to create a Responsive fixed top navigation ui component?
- It can make the building process of Responsive fixed top navigation ui component faster and more easily.
- Enables building complex responsive layouts and components freely.
- Minimum lines of CSS code in Responsive fixed top navigation component file.
The preview of Responsive fixed top navigation ui component
Free download of the Responsive fixed top navigation's source code
The source code of Responsive fixed top navigation ui component
<!-- I've set max-w-screen-md, you may need to change it -->
<nav class="bg-white shadow fixed max-w-screen-md z-10 mx-auto inset-x-0 top-0 flex justify-between items-center">
<a href="#" class="font-extrabold m-3 uppercase inline-flex hover:text-pink-700 transition-all duration-500">
<svg class="h-6 w-6 mr-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path fill="#fff" d="M12 14l9-5-9-5-9 5 9 5z" />
<path fill="#fff" d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222" />
</svg>
NEED JOB
</a>
<button id="mobileMenuButton" class="p-3 focus:outline-none md:hidden" title="Open side menu">
<!-- SVG For "x" button -->
<svg id="mobileMenuButtonClose" class="w-6 h-6 hidden" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" /></svg>
<!-- SVG For "Menu burger" button -->
<svg id="mobileMenuButtonOpen" class="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
</svg>
</button>
<!-- List of nav item -->
<div id="sideMenuHideOnMobile" class="bg-white font-semibold z-10 rounded-bl-md flex absolute top-0 right-0 transition-all duration-500 transform translate-x-0
w-1/2 md:w-auto
px-3 md:px-0
flex-col md:flex-row
-translate-y-full md:translate-y-0
md:mt-1 md:items-center md:mx-1 md:uppercase">
<a href="#" class="mx-0 sm:mx-2 my-2 border-b-2 border-transparent hover:border-pink-600 hover:text-pink-700 transition-all duration-500 py-1 sm:p-0">Project</a>
<a href="#" class="mx-0 sm:mx-2 my-2 border-b-2 border-transparent hover:border-pink-600 hover:text-pink-700 transition-all duration-500 py-1 sm:p-0">Resource</a>
<a href="#" class="mx-0 sm:mx-2 my-2 border-b-2 border-transparent hover:border-pink-600 hover:text-pink-700 transition-all duration-500 py-1 sm:p-0">About</a>
</div>
</nav>
<script>
var mobileMenuButton = document.getElementById("mobileMenuButton");
mobileMenuButton.onclick = function() {
document.getElementById("sideMenuHideOnMobile").classList.toggle("-translate-y-full");
document.getElementById("sideMenuHideOnMobile").classList.toggle("mt-12");
document.getElementById("sideMenuHideOnMobile").classList.toggle("shadow");
document.getElementById("mobileMenuButtonClose").classList.toggle("hidden");
document.getElementById("mobileMenuButtonOpen").classList.toggle("hidden");
}
// Hide element when click outside nav
var theElementContainer = document.getElementsByTagName("nav")[0];
document.addEventListener('click', function(event) {
if (!theElementContainer.contains(event.target)) {
document.getElementById("sideMenuHideOnMobile").classList.add("-translate-y-full");
document.getElementById("sideMenuHideOnMobile").classList.remove("mt-12");
document.getElementById("sideMenuHideOnMobile").classList.remove("shadow");
document.getElementById("mobileMenuButtonOpen").classList.remove("hidden");
document.getElementById("mobileMenuButtonClose").classList.add("hidden");
}
});
</script>
How to create a Responsive fixed top navigation with Tailwind CSS?
Install tailwind css of verion 1.7.0
Use the link
html tag to import the stylesheet of Tailwind CSS of the version 1.7.0
<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">
All the unility class needed to create a Responsive fixed top navigation component
bg-white
fixed
max-w-screen-md
z-10
mx-auto
top-0
flex
m-3
inline-flex
hover:text-pink-700
h-6
w-6
mr-2
p-3
md:hidden
hidden
absolute
right-0
w-1/2
px-3
flex-col
md:mt-1
md:mx-1
mx-0
sm:mx-2
my-2
border-b-2
border-transparent
hover:border-pink-600
py-1
sm:p-0
31 steps to create a Responsive fixed top navigation component with Tailwind CSS
Control the background color of an element to white using the
bg-white
utilities.Use
fixed
to position an element relative to the browser window.Set the maximum width/height of an element using the
max-w-screen-md
utilities.Control the stack order (or three-dimensional positioning) of an element to 10 in Tailwind, regardless of order it has been displayed, using the
z-10
utilities.Control the horizontal margin of an element to auto using the
mx-auto
utilities.Use the
top-0
utilities to set the top position of a positioned element to 0rem.Use
flex
to create a block-level flex container.Control the margin on all sides of an element to 0.75rem using the
m-3
utilities.Use
inline-flex
to create an inline flex container that flows with text.Control the text color of an element to pink-700 on hover using the
hover:text-pink-700
utilities.Use
h-6
to set an element to a fixed height(1.5rem).Use
w-6
to set an element to a fixed width(1.5rem).Control the margin on right side of an element to 0.5rem using the
mr-2
utilities.Control the padding on all sides of an element to 0.75rem using the
p-3
utilities.Use
hidden
to set an element to display: none and remove it from the page layout at only medium screen sizes.Use
hidden
to set an element to display: none and remove it from the page layout.Use
absolute
to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.Use the
right-0
utilities to set the right position of a positioned element to 0rem.Use
w-1/2
to set an element to a fixed width(1/2).Control the horizontal padding of an element to 0.75rem using the
px-3
utilities.Use
flex
to create a block-level flex container.Control the margin on top side of an element to 0.25rem at only medium screen sizes using the
md:mt-1
utilities.Control the horizontal margin of an element to 0.25rem at only medium screen sizes using the
md:mx-1
utilities.Control the horizontal margin of an element to 0rem using the
mx-0
utilities.Control the horizontal margin of an element to 0.5rem at only small screen sizes using the
sm:mx-2
utilities.Control the vertical margin of an element to 0.5rem using the
my-2
utilities.Control the border color of an element to b-2 using the
border-b-2
utilities.Control the border color of an element to transparent using the
border-transparent
utilities.Control the border color of an element to pink-600 using the
hover:border-pink-600
utilities on hover.Control the vertical padding of an element to 0.25rem using the
py-1
utilities.Control the padding on all sides of an element to 0rem at only small screen sizes using the
sm:p-0
utilities.
Conclusion
The above is a step-by-step tutorial on how to use Tailwind CSS to create a Responsive fixed top navigation components, learn and follow along to implement your own components.