Wednesday, 29 April 2015

Simple responsive menu using css3

 Hello readers !
This blog is about how to make a simple navigation menu .
First of all you need an unorder listing(ul-li) in html page as I have taken in example below.
Now remember you also need an anchor tag after list.
The anchor tag will not show in full width ,it works only when you decrease your view screen size.

After this we come on css.
Here we define all classes with their attributes. Also you can manage media screen width as per your needs.

Now the last and important step you need add javascript to hide and show last anchor tag.
Hope It will help you.
Note:- You have to include jquery library aslo.
<-- Html -->
  1. <nav class="clearfix">
  2. <ul class="clearfix">
  3. <li><a href="#">Nav Tital</a></li>
  4. <li><a href="#">Nav Tital</a></li>
  5. <li><a href="#">Nav Tital</a></li>
  6. <li><a href="#">Nav Tital</a></li>
  7. <li><a href="#">Nav Tital</a></li>
  8. <li><a href="#">Nav Tital</a></li>
  9. </ul>
  10. <a href="#" id="gear">Navigation</a>
  11. </nav>
<--css-->
  1. * {
  2. margin: 0px;
  3. text-decoration: none;
  4. padding: 0px
  5. }
  6. .clearfix:before, .clearfix:after {
  7. content: " ";
  8. display: table;
  9. }
  10. .clearfix:after {
  11. clear: both;
  12. }
  13. .clearfix {
  14. *zoom: 1;
  15. }
  16. body {
  17. background: #e5e5e5;
  18. font-family: Arial, sans-serif;
  19. }
  20. nav {
  21. height: 40px;
  22. background: #999;
  23. font-size: 13px;
  24. position: relative;
  25. font-weight: bold;
  26. }
  27. nav ul {
  28. margin: 0 auto;
  29. width: 600px;
  30. }
  31. nav li {
  32. display: inline;
  33. float: left;
  34. }
  35. nav a {
  36. color: #fff;
  37. display: inline-block;
  38. width: 100px;
  39. text-align: center;
  40. line-height: 40px;
  41. }
  42. nav li a {
  43. border-right: 1px solid #333;
  44. box-sizing: border-box;
  45. -moz-box-sizing: border-box;
  46. -webkit-box-sizing: border-box;
  47. }
  48. nav li:last-child a {
  49. border-right: 0;
  50. }
  51. nav a:hover, nav a:active {
  52. background: #666666;
  53. }
  54. nav a#gear {
  55. display: none;
  56. }
  57. @media screen and (max-width:599px) {
  58. nav {
  59. height: auto;
  60. }
  61. nav ul {
  62. width: 100%;
  63. display: block;
  64. height: auto;
  65. }
  66. nav li {
  67. width: 50%;
  68. float: left;
  69. position: relative;
  70. }
  71. nav li a {
  72. border-bottom: 1px solid #333333;
  73. border-right: 1px solid #333333;
  74. }
  75. nav a {
  76. text-align: left;
  77. width: 100%;
  78. text-indent: 25px;
  79. }
  80. }
  81. @media only screen and (max-width : 479px) {
  82. nav {
  83. border-bottom: 0;
  84. }
  85. nav ul {
  86. display: none;
  87. height: auto;
  88. }
  89. nav a#gear {
  90. display: block;
  91. background-color: #222;
  92. width: 100%;
  93. position: relative;
  94. }
  95. nav a#gear:after {
  96. content: "";
  97. background: url('navigationICON.png') no-repeat; /* This is the path of image */
  98. width: 30px;
  99. height: 30px;
  100. display: inline-block;
  101. position: absolute;
  102. right: 15px;
  103. top: 10px;
  104. }
  105. }
  106. @media only screen and (max-width : 320px) {
  107. nav li {
  108. display: block;
  109. float: none;
  110. width: 100%;
  111. }
  112. nav li a {
  113. border-bottom: 1px solid #333;
  114. }
  115. }
<--Script-->
  1. <script>
  2. $(function() {
  3. var gear = $('#gear');
  4. menu = $('nav ul');
  5. menuHeight = menu.height();
  6. $(gear).on('click', function(e) {
  7. e.preventDefault();
  8. menu.slideToggle();
  9. });
  10. });
  11. </script>
You have to include this :-

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

No comments:

Post a Comment