How can I make my navbar responsive?

Jeff Geoff

I have this navbar at codepen:http://codepen.io/DogburnDesign/pen/zymID. It's nice and all but I've realized that it is not responsive. I am quite experienced in making websites but I am new to this responsive thing. Any help guys?

Thanks. here is the code:

HTML:

    <div id="bg"></div>
<section>
  <nav>
    <div></div>
    <ul>
      <li data-xcoord="0px" class="active">Home</li>
      <li data-xcoord="160px">About</li>
      <li data-xcoord="320px">Contact</li>
      <li data-xcoord="480px">Store</li>
    </ul>
  </nav>
</section>

CSS:

#bg{
  position:fixed;
  left:0;
  top:0;
  width:100%;
  height:100%;
background: #38a8d1;
background: -moz-linear-gradient(top,  #38a8d1 0%, #34bc9a 36%, #34bc9a 57%, #8645f7 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#38a8d1), color-stop(36%,#34bc9a), color-stop(57%,#34bc9a), color-stop(100%,#8645f7));
background: -webkit-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: -o-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: -ms-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: linear-gradient(to bottom,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#38a8d1', endColorstr='#8645f7',GradientType=0 );
}

section{
  position:relative;
  width:640px;
  margin:50px auto;
}
nav{
  width:100%;
}
nav ul li{
  display:inline-block;
  list-style:none;
  width:160px;
  text-align:center;
  font-family:Helvetica, sans-serif;
  border: 1px dashed rgba(255,255,255, 0);
  color:#fff;
  padding:10px 0 10px 0;
  margin:-1px -5px -1px -1px;
  cursor:pointer;
  transition:all .2s;
  -webkit-transition:all .2s;
}
nav ul li:hover{
  /*border:1px dashed #fff;*/
  background:rgba(255,255,255,.1);
}
nav ul{
  border: 1px solid #fff;
  position:absolute;
  width:100%;
  padding:0;
  z-index:100;
}
nav div{
  position:absolute;
  left:0;
  top:16px;
  background: #fff;
  width:162px;
  height:40px;
  z-index:99;
}
.active{
  color:rgba(55, 186, 177, 1);
}

And last but not least, JS:

$("nav ul li").click(function(){
  var xcoord = $(this).data("xcoord");

  $("nav div").stop().animate({marginLeft:xcoord}, 500, "easeInOutExpo");
  $(this).addClass("active");
  $("nav ul li").not(this).removeClass("active");

});
Timo

You need to use media queries. See the working example below (You should check the full screen version and play with browser width)

If you create responsive layouts, it is easier if you think in percentages. You do not create an element that is 640px wide, but you create element that is 100% wide but max-width is 640px. So that if user's viewport is wider than 640px then element is 640px, if viewport is less than 640px wide, then the element is x% of the width. My point is that you need to think responsive, it is not a good practice to create thousands of media queries for every 10px of screen width

I also recommend you to use some responsive css frameworks, i.e Bootstrap 3

$("nav ul li").click(function(){
  var xcoord = $(this).data("xcoord");

  $("nav div").stop().animate({marginLeft:xcoord}, 500, "easeInOutExpo");
  $(this).addClass("active");
  $("nav ul li").not(this).removeClass("active");

});
#bg{
  position:fixed;
  left:0;
  top:0;
  width:100%;
  height:100%;
background: #38a8d1;
background: -moz-linear-gradient(top,  #38a8d1 0%, #34bc9a 36%, #34bc9a 57%, #8645f7 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#38a8d1), color-stop(36%,#34bc9a), color-stop(57%,#34bc9a), color-stop(100%,#8645f7));
background: -webkit-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: -o-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: -ms-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: linear-gradient(to bottom,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#38a8d1', endColorstr='#8645f7',GradientType=0 );
}

section{
  position:relative;
  width: 100%;
  max-width:640px;
  margin:50px auto;
}
nav{
  width:100%;
}
nav ul li{
  display:inline-block;
  list-style:none;
  width:160px;
  text-align:center;
  font-family:Helvetica, sans-serif;
  border: 1px dashed rgba(255,255,255, 0);
  color:#fff;
  padding:10px 0 10px 0;
  margin:-1px -5px -1px -1px;
  cursor:pointer;
  transition:color .2s, background .2s;
  -webkit-transition:color .2s, background .2s;
}
nav ul li:not(.active):hover{
  /*border:1px dashed #fff;*/
  background:rgba(255,255,255,.1);
}
nav ul{
  border: 1px solid #fff;
  position:absolute;
  width:100%;
  padding:0;
  z-index:100;
}
.active{
  color:rgba(55, 186, 177, 1);
  background: #fff;
}

@media only screen and (max-width : 720px) {
  nav ul{
    width: 80%;
    left: 10%;
  }
  nav ul li{
    display: block;
    width: 100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="bg"></div>
<section>
  <nav>
    <ul>
      <li class="active">Home</li>
      <li>About</li>
      <li>Contact</li>
      <li>Store</li>
    </ul>
  </nav>
</section>

Solution number 2 does not require media queries and keeps list elements floating. See the solution below

$("nav ul li").click(function(){
  var xcoord = $(this).data("xcoord");

  $("nav div").stop().animate({marginLeft:xcoord}, 500, "easeInOutExpo");
  $(this).addClass("active");
  $("nav ul li").not(this).removeClass("active");

});
#bg{
  position:fixed;
  left:0;
  top:0;
  width:100%;
  height:100%;
background: #38a8d1;
background: -moz-linear-gradient(top,  #38a8d1 0%, #34bc9a 36%, #34bc9a 57%, #8645f7 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#38a8d1), color-stop(36%,#34bc9a), color-stop(57%,#34bc9a), color-stop(100%,#8645f7));
background: -webkit-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: -o-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: -ms-linear-gradient(top,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
background: linear-gradient(to bottom,  #38a8d1 0%,#34bc9a 36%,#34bc9a 57%,#8645f7 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#38a8d1', endColorstr='#8645f7',GradientType=0 );
}

section{
  position:relative;
  width: 100%;
  max-width:640px;
  margin:50px auto;
}
nav{
  width:100%;
}
nav ul li{
  display:inline-block;
  list-style:none;
  width:25%;
  text-align:center;
  font-family:Helvetica, sans-serif;
  border: 1px dashed rgba(255,255,255, 0);
  color:#fff;
  padding:10px 0 10px 0;
  margin:-1px -5px -1px -1px;
  cursor:pointer;
  transition:color .2s, background .2s;
  -webkit-transition:color .2s, background .2s;
}
nav ul li:not(.active):hover{
  /*border:1px dashed #fff;*/
  background:rgba(255,255,255,.1);
}
nav ul{
  border: 1px solid #fff;
  position:absolute;
  width:100%;
  padding:0;
  z-index:100;
}
.active{
  color:rgba(55, 186, 177, 1);
  background: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bg"></div>
<section>
  <nav>
    <ul>
      <li class="active">Home</li>
      <li>About</li>
      <li>Contact</li>
      <li>Store</li>
    </ul>
  </nav>
</section>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I make the navbar responsive depending on the viewport?

From Java

How do I make my absolute-positioned elements responsive?

From Java

How can I make my ngx-line-chart responsive?

From Dev

How can I make an image responsive in a bootstrap modal?

From Dev

How can I make my navbar appear on every page in my rails app?

From Dev

How can I make an image in a div responsive?

From Dev

HTML -- How can I make my navbar stick after scrolling to a certain point on page?

From Dev

How can I make Woocommerce shopping cart table responsive?

From Dev

How can I make this container and table responsive for mobile and ipad size?

From Dev

How can i set responsive height to my div children?

From Dev

how can a make my gridster responsive

From Dev

How to make my website responsive

From Dev

How Can I Force IE to Make Joomla Article Images Responsive?

From Dev

How can I add my custom style for my bootstrap navbar?

From Dev

How can I make my HTML page Flexible/Responsive for iPhone 6s

From Dev

how can i make text wrapping in responsive div?

From Dev

How can I make the background image to be responsive without losing the proportions

From Dev

How do I make my website's background image responsive?

From Dev

How can I make Ubuntu more responsive on a netbook?

From Dev

How can I make my webpage responsive when I minimize the page

From Dev

How can I make this logo,slogan and video responsive?

From Dev

how can i make bootstrap menu responsive anytime?

From Dev

How can I make this image responsive?

From Dev

How can I make responsive JQuery left margin?

From Dev

How can I make this section of my webpage responsive?

From Dev

How can i make content of an css table responsive

From Dev

How can I make my form responsive on all sizes

From Dev

How can I make my navbar appear at a certain section?

From Dev

can I make my recycler view responsive with respect to screen sizes?

Related Related

  1. 1

    How do I make the navbar responsive depending on the viewport?

  2. 2

    How do I make my absolute-positioned elements responsive?

  3. 3

    How can I make my ngx-line-chart responsive?

  4. 4

    How can I make an image responsive in a bootstrap modal?

  5. 5

    How can I make my navbar appear on every page in my rails app?

  6. 6

    How can I make an image in a div responsive?

  7. 7

    HTML -- How can I make my navbar stick after scrolling to a certain point on page?

  8. 8

    How can I make Woocommerce shopping cart table responsive?

  9. 9

    How can I make this container and table responsive for mobile and ipad size?

  10. 10

    How can i set responsive height to my div children?

  11. 11

    how can a make my gridster responsive

  12. 12

    How to make my website responsive

  13. 13

    How Can I Force IE to Make Joomla Article Images Responsive?

  14. 14

    How can I add my custom style for my bootstrap navbar?

  15. 15

    How can I make my HTML page Flexible/Responsive for iPhone 6s

  16. 16

    how can i make text wrapping in responsive div?

  17. 17

    How can I make the background image to be responsive without losing the proportions

  18. 18

    How do I make my website's background image responsive?

  19. 19

    How can I make Ubuntu more responsive on a netbook?

  20. 20

    How can I make my webpage responsive when I minimize the page

  21. 21

    How can I make this logo,slogan and video responsive?

  22. 22

    how can i make bootstrap menu responsive anytime?

  23. 23

    How can I make this image responsive?

  24. 24

    How can I make responsive JQuery left margin?

  25. 25

    How can I make this section of my webpage responsive?

  26. 26

    How can i make content of an css table responsive

  27. 27

    How can I make my form responsive on all sizes

  28. 28

    How can I make my navbar appear at a certain section?

  29. 29

    can I make my recycler view responsive with respect to screen sizes?

HotTag

Archive