How to create mega menu using HTML and CSS?

Mohammad Azad Verified
Learn how to create a mega menu (full-width dropdown menu in a navigation bar) using HTML and CSS.
@import url(https://fonts.googleapis.com/css?family=Open+Sans);
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Open Sans";
  border: 1px solid #f2f2f2;
}

.navbar {
  overflow: hidden;
  background-color: #fff;
  box-shadow: 0px 0px 12px rgb(0 0 0 / 15%);
  display: flex;
}

.navbar a {
  font-size: 16px;
  color: #000;
  text-align: center;
  padding: 20px 20px;
  text-decoration: none;
  position: relative;
}
.navbar a::after {
    content: "";
    position: absolute;
    width: 1px;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;  
  border: none;
  outline: none;
  color: #000;
  padding: 20px 20px;
  background-color: inherit;
  font: inherit;
  margin: 0;
  cursor: pointer;
}

.navbar a:hover, .dropdown:hover .dropbtn {
  background-color: #f2f2f2;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #fff;
  width: 100%;
  left: 0;
  box-shadow: 0px 8px 16px 0px rgb(0 0 0 / 5%);
  z-index: 1;
}

.dropdown-content .header {
    padding: 20px 25px;
    color: #e54d26;
    background: #f2f2f2;
}
.dropdown-content .header h2{
    margin: 0px;
    font-size: 16px;
    font-weight: 500;
}

.dropdown:hover .dropdown-content {
  display: block;
}

/* Create three equal columns that floats next to each other */
.row{
    display: flex;
    align-items: center;
}
.column {
  flex: 1;
  padding: 25px 12px;
}
.column:last-child {
  flex: 2;  
 }
.column img{
  width:100%;
  border-radius: 0px 10px 10px 0px;
}

.column h3 {
    margin: 0px 0px 15px;
}

.column a {
    float: none;
    color: black;
    padding: 10px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
    border: 1px solid #f2f2f2;
    border-radius: 4px;
    margin-top: 10px;
}

.column a:hover {
  background-color: #f2f2f2;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
    .row{
        display: block;
    }
    .column {
        width: 100%;
        height: auto;
    }
}
@media screen and (max-width: 1199px) {
    
    .column:last-child {
        /*display: none;*/
    }
}
<div class="navbar">
  <a href="#home">Home</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <div class="header">
        <h2>Mega Menu</h2>
      </div>   
      <div class="row">
        <div class="column">
          <h3>Category 1</h3>
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
        <div class="column">
          <h3>Category 2</h3>
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
        <div class="column">
          <h3>Category 3</h3>
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
        <div class="column">
          <img src="https://w3sniff.com/common/CoverIMG/html.jpg" alt="w3sniff">
        </div>
      </div>
    </div>
  </div> 
  
  <a href="#news">About</a>
</div>

<div style="padding:16px">
  <h3>Responsive Mega Menu (Full-width dropdown in navbar)</h3>
  <p>Hover over the "Dropdown" link to see the mega menu.</p>
  <p>Resize the browser window to see the responsive effect.</p>
</div>

Learn how to create a mega menu (full-width dropdown menu in a navigation bar) using HTML and CSS.

References and Credits

Comments

Leave a Comment