Skip to content Skip to sidebar Skip to footer

Center Nav Bar When Window Resized?

My navigation bar is centered, but when the window is smaller, it just goes onto the next line, rather than getting smaller to fit the size of the window, and I don't know how to r

Solution 1:

Your menu is specified as a variable width of 60%:

#page-navigation
{
    width: 60%;
    ...
}

This will cause the width of the bar to scale with the window, and affect the position of the elements within it. To prevent this, specify a static width, such as:

#page-navigation
{
    width: 1000px;
    ...
}

Solution 2:

I just insert a line of code and I think it looks pretty nice right now :)

ul.top-menu{
   padding: 0;
}

If you resize the screen there is in front of the navigation (next line) a small space and this resolves the problem.

See the resolution also on jsfiddle.

Solution 3:

Answer:

Because of the way your HTML document is structured, it's not possible for you to get the intended effect for the following reason:

  • You have an unordered list nested directly in another unordered list which is (1) not considered correct (see this discussion); but more importantly, while it looks like your navigation has 6 top level items, you really only have 4. So no matter what CSS you apply to it, it won't work.

Recommendations:

  1. Fix the structure of your HTML document first by using the proper classes only on the top navigation items and properly nest your navigation items.*

  2. I would advise restructuring you information architecture to contain less navigation items on the menu. For example, the recognition would make sense to go in your About page. And if this is a portfolio type website, collapsing your Photography, Graphic Design, and 3D Modeling into Projects would work well. And if you're concerned with the separation, that will happen within the page as a sub-navigation.

  3. If you are set on keeping the navigation structure, it's advisable to either collapse your menu into a select menu or hamburger menu on mobile devices since having a large chunk someone's mobile device screen consumed by your navigation is not a good experience for your user. On top of it, you have to consider that users can't "hover" on mobile devices and the size of those dropdowns would be difficult to navigate at best.

*Answer :Demo

HTML (Fixed):

<navid="page-navigation"><ul><li><ahref="../../index.html"title="Home">Home</a></li><liclass="top-menu"><ahref="../../_portfolio/photography.html"onclick="return false">Photography</a><ulclass="sub-menu"><li><ahref="../../_portfolio/_photography/bmc-himley-mini-show-2015.html">BMC Himley Mini Show 2015</a></li><li><ahref="../../_portfolio/_photography/kinver-snow.html">Kinver Snow</a></li><li><ahref="../../_portfolio/_photography/mini-runs-collection.html">"Mini Runs" Collection</a></li><li><ahref="../../_portfolio/_photography/hofner-bass.html">Hofner Bass</a></li><li><ahref="../../_portfolio/_photography/nature.html">Nature</a></li><li><ahref="../../_portfolio/_photography/haynes-motor-museum.html">Haynes Motor Museum</a></li><li><ahref="../../_portfolio/_photography/miscellaneous.html">Miscellaneous</a></li><li><ahref="../../_portfolio/_photography/classic-mini.html">Classic Mini</a></li></ul></li><liclass="top-menu"><ahref="../../_portfolio/graphic-design.html"onclick="return false">Graphic Design</a><ulclass="sub-menu"><li><ahref="../../_portfolio/_graphic-design/story-bag-artwork.html">"Story Bag" Artwork</a></li><li><ahref="../../_portfolio/_graphic-design/business-cards.html">Business Cards</a></li><li><ahref="../../_portfolio/_graphic-design/logo-design.html">Logo Design</a></li><li><ahref="../../_portfolio/_graphic-design/the-mexican-job.html">"The Mexican Job"</a></li><li><ahref="../../_portfolio/_graphic-design/magazine-covers.html">Magazine Covers</a></li><li><ahref="../../_portfolio/_graphic-design/wpap-artwork.html">WPAP Artwork</a></li><li><ahref="../../_portfolio/_graphic-design/lyric-posters.html">Lyrics Posters</a></li></ul></li><li><ahref="../../_portfolio/3d-modelling.html">3D Modelling</a></li><li><ahref="../../about.html">About</a></li><li><ahref="../../recognition.html">Recognition</a></li></ul></nav>

CSS (Fixed and Updated):

/*navigation*/#page-navigation {
  width: 100%;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 10px;
}

#page-navigationul {
  text-align: center;
}

#page-navigationulli {
  color: white;
  list-style: none;
  background-color: darkslategray;
  width: 9em;
  /* float: left removes any possibility of it centering */display: inline-block;
}

li {
  position: relative;
}

li.title {
  display: none;
}

lia {
  display: block;
  color: white;
  line-height: 1.3em;
  padding: 1em;
  text-align: center;
}

lia:link {
  text-decoration: none;
}

lia:visited {
  text-decoration: none;
  color: white;
}

lia:hover,
.top-menu > li:hover > a {
  background-color: rgb(48, 48, 48);
}

lia:active {
  background-color: dimgray;
}

ul.sub-menu {
  width: auto;
  height: auto;
  position: absolute;
  left: -9000em;
  overflow: hidden;
}

ul.sub-menuli {
  clear: left;
  float: none;
  margin-left: -2.5em;
  z-index: 1000;
}

.top-menu:hoverul {
  left: 0;
}

ul.sub-menulia {
  height: auto;
  border-bottom: 1px solid gray;
  padding: .4em1em;
  background-color: dimgray;
  padding-top: 1em;
  padding-bottom: 1em;
}

ul.sub-menuli:last-childa {
  border-bottom: none;
}

ul.sub-menulia:hover {
  background-color: darkslategray;
}

ul.sub-menulia:active {
  background-color: gray;
}

ul.top-menu {
  padding: 0;
}

There are still some minor stylings to adjust, but this should get you what you wanted based on your question.

Post a Comment for "Center Nav Bar When Window Resized?"