How To Create A Responsive Navigation

One of the trickiest parts to be responsified on a website is “the Navigation”, this part is really important for the website accessibility, as this is one of the ways visitors jump over the web pages.

There are actually many ways to create responsive web site navigation and even some jQuery plugins are available to do it in a second.

However, rather than applying an instant solution, in this post, we are going to walk you through onhow to build a simple navigation from the ground and using the CSS3 media queries and a littlejQuery to display it in a small screen size like the smartphones properly.

  • DEMO
  • DOWNLOAD SOURCE

So, let’s just get started.

HTML

First of all, let’s add the meta viewport inside the head tag. This meta viewport tag is required for our page to scale properly inside any screen size, particularly in the mobile viewport.

view plaincopy to clipboardprint?
  1. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">  

…and then add the following snippet as the navigation markup inside the body tag.

view plaincopy to clipboardprint?
  1. <nav class="clearfix">  
  2.     <ul class="clearfix">  
  3.         <li><a href="#">Home</a></li>  
  4.         <li><a href="#">How-to</a></li>  
  5.         <li><a href="#">Icons</a></li>  
  6.         <li><a href="#">Design</a></li>  
  7.         <li><a href="#">Web 2.0</a></li>  
  8.         <li><a href="#">Tools</a></li>    
  9.     </ul>  
  10.     <a href="#" id="pull">Menu</a>  
  11. </nav>  

As you can see above, we have six primary menu links and added one more link after them. This extra link will be used to pull the menu navigation when it is hidden in a small screen.

Further reading: Don’t forget the viewport meta tag.

Styles

In this section, we start styling the navigation. The style here is only decorative, you can pick any colors as you desire. But in this case, we (I personally) want the body to have a soft and creamy color.

view plaincopy to clipboardprint?
  1. body {  
  2.     background-color#ece8e5;  
  3. }  

The nav tag that define the navigation will have 100% full width of the browser window, while the ulwhere it contains our primary menu links will have 600px for the width.

view plaincopy to clipboardprint?
  1. nav {  
  2.     height40px;  
  3.     width: 100%;  
  4.     background#455868;  
  5.     font-size11pt;  
  6.     font-family'PT Sans'Arialsans-serif;  
  7.     font-weightbold;  
  8.     positionrelative;  
  9.     border-bottom2px solid #283744;  
  10. }  
  11. nav ul {  
  12.     padding: 0;  
  13.     margin: 0 auto;  
  14.     width600px;  
  15.     height40px;  
  16. }  

Then, we will float the menu links to the left, so they will be displayed horizontally side by side, but floating an element will also cause their parent collapse.

view plaincopy to clipboardprint?
  1. nav li {  
  2.     displayinline;  
  3.     floatleft;  
  4. }  

If you notice from the HTML markup above, we’ve already added clearfix in the class attribute for both the nav and ul to clear things around when we float the elements inside it using this CSS clearfix hack. So, let’s add the following style rules in the style sheet.

view plaincopy to clipboardprint?
  1. .clearfix:before,  
  2. .clearfix:after {  
  3.     content" ";  
  4.     display: table;  
  5. }  
  6. .clearfix:after {  
  7.     clearboth;  
  8. }  
  9. .clearfix {  
  10.     *zoom: 1;  
  11. }  

Since we have six menu links and we also have specified the container for 600px, each menu links will have 100px for the width.

view plaincopy to clipboardprint?
  1. nav a {  
  2.     color#fff;  
  3.     displayinline-block;  
  4.     width100px;  
  5.     text-aligncenter;  
  6.     text-decorationnone;  
  7.     line-height40px;  
  8.     text-shadow1px 1px 0px #283744;  
  9. }  

The menu links will be separated with 1px right border, except for the last one. Remember our previous post on box model, the menu’s width will be expanded for 1px making it 101px as the border addition, so here we change the box-sizing model to border-box in order to keep the menu remains 100px.

view plaincopy to clipboardprint?
  1. nav li a {  
  2.     border-right1px solid #576979;  
  3.     box-sizing:border-box;  
  4.     -moz-box-sizing:border-box;  
  5.     -webkit-box-sizing:border-box;  
  6. }  
  7. nav li:last-child a {  
  8.     border-right: 0;  
  9. }  

Next, the menu will have brighter color when it is in :hover or :active state.

view plaincopy to clipboardprint?
  1. nav a:hover, nav a:active {  
  2.     background-color#8c99a4;  
  3. }  

…and lastly, the extra link will be hidden (for the desktop screen).

view plaincopy to clipboardprint?
  1. nav a#pull {  
  2.     displaynone;  
  3. }         

At this point, we only styling the navigation and nothing will happen when we resize the browser window. So, let’s jump to the next step.

Further reading: CSS3 Box-sizing (Hongkiat.com)

Media Queries

The CSS3 media queries is used to define how the styles will shift in some certain breakpoints or specifically the device screen sizes.

Since our navigation is initially 600px fix-width, we will first define the styles when it is viewed in600px or lower screen size, so practically speaking, this is our first breakpoint.

In this screen size, each of two menu links will be displayed side by side, so the ul‘s width here will be 100% of the browser window while the menu links will have 50% for the width.

Comments

  1. Genie

    September 16, 2013

    Everything is very open with a really clear clarification of the challenges.
    It was definitely informative. Your site is very useful.
    Thanks for sharing!

  2. webdesign-resources.org

    October 05, 2013

    A substantial portion of these mails go to international destinations where other businesses and individuals
    are provided with the companies product or services information.
    And you can do it with a simple and easy online application.
    You may have to look at the overall design and layout of your online site.
    As much the co-founders go, they both have exceptional track records in the profession,
    especially in the online marketing arena.

  3. android hack games download

    October 06, 2013

    After checking out a number of the articles on your blog, I honestly like
    your way of blogging. I added it to my bookmark website list and will
    be checking back in the near future. Please check out
    my web site too and let me know what you think.

  4. Tyrone

    October 17, 2013

    Lots of people are coming forward to joining online businesses
    so that they can earn money within a short period of time.
    Beginners should start with little money and increase their
    gambling amount gradually to avoid huge losses. In doing so, it is good to
    make an online portfolio for you to show your samples.
    Hire good attorneys, tax accountants, mentors and coaches.

  5. free shemale chat room

    November 10, 2013

    The tourism industry in Thailand has the very local characteristics.

    You can ask what is the distinction between this site and
    also the other Shemale Yum Sites. com featuring over 300 horny she male webcam models live show and video chat.

  6. shemalecams

    November 29, 2013

    Oftentimes, what leads to long-distance couples not making a true success of things would be lack of communication and honesty.
    Examination of petrified fear describes the enlightened expression on
    the lady cop’s face, as she calmly approached me without an aggressive posture.
    There are Asian ladyboys if you prefer exotic and unique beauties.

  7. hosting quickbooks

    February 10, 2014

    Paragraph writing is also a fun, if you be acquainted with then you can
    write or else it is complex to write.

  8. hosting quickbooks

    February 10, 2014

    Paragraph writing is also a fun, if you be acquainted with then you can write or
    else it is complex to write.

  9. milf sex cam

    March 11, 2014

    MILF Cam Advantages - There are several clear reasons to be
    enthusiastic about MILF cam chats. Condoms, of course, are
    always an absolute necessity. How long does it take to get your free breast implants.

  10. xbox 360 emulator for pc deer hunter 2014 hack no

  11. låna pengar 10 000

    March 19, 2014

    Hi there! Do you know if they make any plugins
    to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not
    seeing very good results. If you know of any please share.
    Many thanks!

  12. Edwardo

    March 20, 2014

    How mild or wild you are willing to be is your call, but the option
    is there for whatever you want to experience. The best thing of course is to explore your
    options. Adult dating software is an effective high-end dating
    script.

  13. Edwardo

    March 20, 2014

    How mild or wild you are willing to be is your call, but the option is there
    for whatever you want to experience. The best thing of course is to explore your options.
    Adult dating software is an effective high-end dating script.

  14. tinyurl.com

    March 25, 2014

    These shows include the use of live nude cams which offer a stripshow.
    We won’t discuss whether or not you believe in ghosts,
    but apparently having a USB radar that shows you the location of the ghosts will not save
    you from them. Some of exactly people points which you want to do in
    buy to attain that accomplishment.

  15. Kudos. I appreciate this!

  16. granny cams

    March 31, 2014

    MILF Cam Advantages - There are several clear reasons to be enthusiastic about MILF cam chats.
    Condoms, of course, are always an absolute necessity. You want to start it all
    afresh though a bit baffled, mainly because you don’t know where to go and how to start with it again.

  17. granny cams

    March 31, 2014

    MILF Cam Advantages - There are several clear reasons to be enthusiastic
    about MILF cam chats. Condoms, of course,
    are always an absolute necessity. You want to start it all afresh though a bit baffled,
    mainly because you don’t know where to go and how to start with
    it again.

  18. Clash Of Clans Pirater

    April 09, 2014

    It’s a pity you don’t have a donate button! I’d without
    a doubt donate to this superb blog! I guess for now i’ll settle for book-marking
    and adding your RSS feed to my Google account. I look forward to brand new
    updates and will talk about this blog with my Facebook group.
    Chat soon!

  19. Clash Of Clans Pirater

    April 09, 2014

    It’s a pity you don’t have a donate button! I’d without a doubt donate to this superb blog!
    I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to brand new updates and will talk about
    this blog with my Facebook group. Chat soon!

  20. clash of clans free gems

    April 16, 2014

    This event occurred right after thee time Mario return to thhe Mushroom Kingdom so it had to take place after
    Wario Land because Wario was also in this game as a second main character as
    well and had his own story mode. Music, art and
    verbal literature help to reinforce relogious and
    their social patterns. If the battle had been a siege that
    length would have beedn understandable.

Add a Comment