Back to all posts

CSS Nav Bar - Build a navbar using <details> and <summary> tag

by Subin Sudhakaran / 24th June, 2020

Portfolio: subinsamrat.netlify.com

Okay, So far we have learned basic CSS Concepts, now we will try to apply those principles in our project by building responsive navigation menu using HTML & CSS. Also we are going to learn 2 new HTML tags which you guys would have not heard of.

Before heading into the tutorial part, this is what we are going to build:


captured19ec6d0a1d6f48a1.gif

Let's get started.

Step 1: Semantic HTML elements


<details>
  <summary></summary>
  <nav class="menu">
    <a href="#">Home</a>
    <a href="#">Work</a>
    <a href="#">Links</a>
    <a href="#">Contact</a>
    <a href="#">About</a>
  </nav>
</details>

:root {
  --primColor: #dcdcdc;
  --secoColor: #555555;
  --cornerRad: 4px;
}

body {
  background-color: var(--primColor);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
}

Just decorating the body section.

Step 2: What details and summary tags will do ?


Just like accordians. The child elements of <details> tag will be considered as the content and the summary tag will be the accordian header. Without using any CSS/JavaScript we can create an accordian in HTML itself. Glad I learned this.

So far:

captured-1a06986fb6691e7d8.gif

Step 3: Decorate the icon


summary {

  text-align: center;
  padding: 12px 10px;
  width: 23px;
  height: 17px;
  background-color: var(--primColor);
  border: 2px solid var(--secoColor);
  border-radius: var(--cornerRad);
  color: var(--secoColor);
  cursor: pointer;
  user-select: none;
  outline: none;
  transition: transform 200ms ease-in-out 0s;
}

summary::before,
summary::after {
  position: static;
  top: 0;
  left: 0;
}
summary::before {
  content: "";
}
summary::after {
  content: "III";
  letter-spacing: -1px;
}

summary:hover {
  transform: scale(1.1);
}

{/* to hide the arrow icon */}

summary::-webkit-details-marker {
  display: none;
}

{/* Add this property to summary section to transform the "III" to horizontal position */}

summary {
  writing-mode: vertical-lr;
}

So far:

ham-menu-styling


Step 4: Transform ham menu to cross symbol


{/* add the X on open */}
details[open] summary::before {
  content: "X";
}
{/* remove the ham menu that we added in summary::after */}
details[open] summary::after {
  content: "";
}

So far:

captured-2f8946771b5b648a8.gif


Step 5: Styling the nav and links


.menu {
  width: fit-content;
  border-radius: var(--cornerRad);
  background-color: var(--primColor);
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.2);
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
}

.menu a {
  padding: 12px 24px;
  margin: 0 16px;
  color: var(--secoColor);
  border-bottom: 2px solid rgba(0, 0, 0, 0.1);
  text-decoration: none;
  text-align: center;
  transition: filter 200ms linear 0s;
}

{/* padding top for 1st link */}

.menu a:nth-of-type(1) {
  padding-top: 24px;
}

{/* link hover */}

.menu a:hover {
  filter: brightness(200%);
}

So far:

captured-3a8b2bb8563a44768.gif


Step 6: Add helper content and hide on menu open


details::before {
  content: "← Click This Button";
  color: var(--secoColor);
  position: absolute;
  margin-left: 80px;
  padding: 10px 10px;
  opacity: 0.4;
}

details[open]::before {
  animation: fadeMe 300ms linear forwards;
}

@keyframes fadeMe {
  0% {
    opacity: 0.4;
  }
  100% {
    opacity: 0;
  }
}

captured19ec6d0a1d6f48a1.gif

Post your doubts in subinsamrat96@gmail.com. I will be there for you guys, always. Thank you.