Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • A Responsive Menu With Hover Effect

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 372
    Comment on it

    Hello, readers . In today's blog, I have created A Responsive Menu which when we hover over the menu items it covers the area of the hovered item with a different color.

     

    For creating the navigation menu , I need to create an unordered list with the class name as a menu containing the nested <li> in it.

     

    Below is the code for the above example :-

    <html>
    <head>
        <title>Responsive Menu With Hover Effect</title>
        <style>
        @import url(http://fonts.googleapis.com/css?family=PT+Sans);
    * {
      box-sizing: border-box;
    }
    html,
    body {
      margin: 0;
      padding: 0;
    }
    body {
      font-family: 'PT Sans', Arial, Verdana;
      background-color: #eee;
    }
    h1 {
      text-align: center;
      font-size: 48px;
      text-transform: uppercase;
      letter-spacing: 3px;
      color: #222;
    }
    .menu {
      list-style: none;
      margin: 0;
      padding: 0;
      width: 100%;
      height: 120px;
      margin: auto;
      position: relative;
      background-color: #2c3e50;
      z-index: 7;
    }
    .menu li {
      float: left;
      width: 25%;
      height: 100%;
      margin: 0;
      padding: 0;
    }
    .menu a {
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      width: 100%;
      height: 100%;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
          -ms-flex-pack: center;
              justify-content: center;
      -webkit-box-align: center;
      -webkit-align-items: center;
          -ms-flex-align: center;
              align-items: center;
      color: #fff;
      text-decoration: none;
      position: relative;
      font-size: 18px;
      z-index: 9;
    }
    a.active {
      background-color: #e74c3c;
      pointer-events: none;
    }
    li.slider {
      width: 25%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      background-color: #e74c3c;
      z-index: 8;
      -webkit-transition: left 0.4s, background-color 0.4s;
      transition: left 0.4s, background-color 0.4s;
    }
    .menu li:nth-child(1):hover ~ .slider,
    .menu li:nth-child(1):focus ~ .slider,
    .menu li:nth-child(1):active ~ .slider {
      left: 0;
      background-color: #3498db;
    }
    .menu li:nth-child(2):hover ~ .slider,
    .menu li:nth-child(2):focus ~ .slider,
    .menu li:nth-child(2):active ~ .slider {
      left: 25%;
      background-color: #9b59b6;
    }
    .menu li:nth-child(3):hover ~ .slider,
    .menu li:nth-child(3):focus ~ .slider,
    .menu li:nth-child(3):active ~ .slider {
      left: 50%;
      background-color: #e67e22;
    }
    .menu li:nth-child(4):hover ~ .slider,
    .menu li:nth-child(4):focus ~ .slider,
    .menu li:nth-child(4):active ~ .slider {
      left: 75%;
      background-color: #16a085;
    }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Responsive Menu Effect on Hover!</h1>
                <ul class="menu">
                  <li><a href="#" class="active">Home</a></li>
                  <li><a href="#">About</a></li>
                  <li><a href="#">Portfolio</a></li>
                  <li><a href="#">Contact</a></li>
                  <li class="slider"></li>
                </ul>
        </div>
    </body>
    </html>

    In the CSS code , I have set the height and width , margin and its position to relative to the menu class .

     

    For the menu <li> I have to float it left with 25%. Now to the anchor tag nested in the menu class I have set its height and width to 100% with the position to relative.

     

    To the first <li> of the menu class with the class name as active I have sets its background color.

     

    Now to provide the transition effect to the slider class I had made its position to absolute and adjusted its height and width.

     

    To this, I have set the transition to left for 0.4s with a different background-color.

     

    Instead of applying the media query I have taken pseudo- elements i.e. the (nth-child) .

     

    As in the example, I have taken four <li> so to each <li> (nth-child) whenever it has hovered, focused and active of the slider class , the background color changes to the left at different percentages with a particular order.

     

    Conclusion:-

    Hence, A responsive menu with the hover effect has been created.

     

    Note:- The above code will run over all modern browsers such as on Firefox 7.0.1, Chrome 15.0, Internet Explorer 9.0 , Safari 5.1.1.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: