DEV Community

Devin Rasmussen
Devin Rasmussen

Posted on

Create a navbar with gradient

TL;DR codepen

I was hanging out at fly.io and noticed this sweet top navigation bar
fly io
so I had to machete through all the tailwind to replicate.

First we pick three colors: lightskyblue rebeccapurple sandybrown

Let's introduce a nav with a height

<nav>
</nav>
Enter fullscreen mode Exit fullscreen mode
nav {
  height: 80px;
Enter fullscreen mode Exit fullscreen mode

Now let's introduce the gradient from top left to bottom right

nav {
  background: 
    linear-gradient(to bottom right 
      lightskyblue rebeccapurple sandybrown
    );
}
Enter fullscreen mode Exit fullscreen mode

linear gradient with no opacity

We need to reduce the opacity on lightskyblue and sandybrown so let's get their HSL using shift click like so

transform

With reduced opacity (0.25), our code may look something like

nav {
  background: linear-gradient(
    to bottom right,
    hsla(203, 92%, 75.5%, 0.25),
    rebeccapurple,
    hsla(27.6, 87.1%, 66.7%, 0.25)
  );
}
Enter fullscreen mode Exit fullscreen mode

header without background

Now let's add a background to the whole thing

nav {
  background-color: rebeccapurple;
}
Enter fullscreen mode Exit fullscreen mode

header with background gradient

I didn't love this so I went ahead and abstracted the rebeccapurple to HSL so I could darken it significantly (lightness in HSL from 40 to 15). It's in a CSS variable because we'll use it twice.

nav {
/* --rebecca-purple: hsl(270, 50%, 40%); */
  --rebecca-purple: hsl(270, 60%, 15%);
Enter fullscreen mode Exit fullscreen mode

darker rebecca-purple

Almost there. A good gradient doesn't look like a gradient. So let's bring the lightness of the first and last colors down to 35% for the final result.

final nav with gradient

nav {
  --rebecca-purple: hsl(270, 60%, 15%);
  background-image: linear-gradient(
    to bottom right, 
    hsla(203, 92%, 35.5%, 0.25), 
    var(--rebecca-purple), 
    hsla(27.6, 87.1%, 35.7%, 0.25)
  );
  background-color: var(--rebecca-purple);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)