Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS How to Make a Website Debugging HTML and CSS Problems How to Fix Problems with Code

Navigation menu will not centre?

Hi all,

I am struggling to centre my nav menu, its set up as a responsive website so when I reduce the page width I want the Nav menu links to be centred and not to the left, I had it working before but now it seems to be mis behaving.

Would really appreciate some suggestions.

Thanks, Josh

Hi Josh,

Can you post your HTML and CSS code for the navigation using 3 backticks (```) on the line before and the same on the line after your code please?

This is a good how-to guide for posting code if needed.

Thanks

-Rich

7 Answers

Muhammad Rizwan
Muhammad Rizwan
8,595 Points

Hi Joshua Nevard,

Please post your html and css code to debug it.

Hey people thanks for helping,

HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Josh Nevard My Website</title> <link rel="stylesheet" href="css/normalize2.css"> <link href='http://fonts.googleapis.com/css?family=Averia+Sans+Libre%7CIndie+Flower' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="css/main.css"> <link rel="stylesheet" href="css/responsive.css"> <meta name="viewport" content="width-device-width, intial-scale-1.0"> </head> <body> <header> <a href="index.html" id="logo"> <h1>Josh Nevard</h1> <h2>My website</h2> </a> <nav> <ul> <li><a href="index.html" class="select">Portfolio</a></li> <li><a href="about.html">About</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav> </header> <div id="wrapper"> <section> <ul id="gallery"> <li> <a href="img/farleyth1.JPG"> <img src="img/farleyth1.JPG" alt="Farley Shoot"> <p>Farley down the beach.</p> </a> </li> <li> <a href="img/farleyth2.JPG"> <img src="img/farleyth2.JPG" alt="Farley Shoot"> <p>Farley posing:).</p> </a> <li> <a href="img/farleyth3.JPG"> <img src="img/farleyth3.JPG" alt="Farley Shoot"> <p>Farley being sleepy.</p> </a> <li> <a href="img/farleyth4.JPG"> <img src="img/farleyth4.JPG" alt="Farley Shoot"> <p>Farley chilling down the beach.</p> </a> <li> <a href="img/farleyth5.JPG"> <img src="img/farleyth5.JPG" alt="Farley Shoot"> <p>Farley show me his tongue.</p> </a> </ul> </section> <footer> <a href="http://www.facebook.com/josh.nevard.7"><img src="img/facebook-wrap.png" alt="Facebook Logo" class="social-icon"></a>

    <p>&copy; 2014 Josh Nevard.</p>
  </footer>
</div>

</body> </html>

CSS:

/*******************
General 
********************/

body {
  font-family: 'Averia Sans Libre';
}

body{
  padding: 0; 
  margin: 0;
}

#wrapper {
  max-width: 940px;
  margin: 0 auto;
  padding: 0 5%;
} 

a {
  text-decoration:none;  
}

img {
  max-width: 100%;
}

h3 {
  margin: 0 0 1em 0;
}

/*******************
Heading
********************/

header {
  float: left;
  margin: 0 0 30px 0;
  padding: 5px 0 0 0;
  width: 100%;
}

#logo {
  text-align: center;
  margin: 0;
}

h1 {
  font-family:'indie flower', Averia Sans Libre;
  margin: 15px 0px;
  font-size: 1.75em;
  font-weight: normal;
  line-height: 0.9em; 
}

h2 {
  font-size:0.75em;
  margin: -5px 0 0;
  font-weight: normal;
}

/*******************
Navagation 
********************/


nav {
  text-align: centre;
  padding: 10px 0;
  margin: 20px 0 0;
}

nav ul {
  list-style: none;
  margin: 0 10px;
  padding: 0;

}

nav li  {
  display: inline-block;
}

nav a {
  background-color: none;
  font-weight: 800;
  padding: 15px 10px;    
}

/*******************
Footer
********************/

footer { 
  font-size: 0.75em;
  text-align: center;
  clear: both;
  padding-top: 50px;
  color:#ccc;
}

.social-icon {
  width: 20px;
  height: 20px;
  margin: 0 5px;
}

/*******************
Page Portfolio
********************/

#gallery {
  margin: 0;
  padding: 0;
  list-style: none;
}

#gallery li {
  float: left;
  width: 45%;
  margin: 2.5%;
  background-color: #f5f5f5;
  color: #bdc3c7;
}

#gallery li a p {
  margin: 0;
  padding: 5%;
  font-size: 0.75em;
  color: #bdc3c7;

}

/*************
About
*************/

.profile-photo {
  display: block;
  max-width: 150px;
  margin: 0 auto 30px;
  border-radius: 100%;
}

/*******************
Page: Contact 
********************/

.contact-info {
  list-style: none;
  padding: 0 0 0 0;
  margin: 0;
  font-size: 0.9em;
}

.contact-info a {
  display: block;
  min-height: 20px;
  background-repeat: no-repeat;
  background-size: 20px 20px;
  padding: 0 0 0 30px;
  margin: 0 0 10px;
}

.contact-info li.phone a {
  background-image: url('../img/phone.png');
}

.contact-info li.mail a {
  background-image: url('../img/mail.png');
}

/*******************
Colours 
********************/


/*site body */
body {
  background-color:#fff;
  color: #999;
  margin-top: 0;

}

/* green header*/
header {
  background: #888;
  border-color:#777;
 }

/*nav background on mobile devices*/
nav {
  background-color:#999;
}

/*logo text*/
h1,h2 {
  color:#fff;
}

/*links*/
a {
  color: #555;

  }

/*nav link*/
nav a, nav a: visited {
  color: #fff;
}

/*selected nav link*/
nav a.selected, nav a:hover {
  color: #fff;
}

Responsive CSS:

@media screen and (MAX-width: 480px) {
  /*******************
Two column layout
********************/
  #primary {
    width: 50%;
    float: left;
    display: inline-block;
    clear: both;
  }

  #secondary {
    width: 40%;
    display: inline-block;
    clear: both;
  }

  /*******************
Page: Portfolio
********************/

   #gallery li {
     width: 28.3333%;
  }

  #gallery li:nth-child(4n) {
    clear: left;
  }



  /*******************
Page: About
********************/

.profile-photo {
  float: left;
  margin: 0 10% 80px 0;
}

 }

@media screen and (MAX-width: 660px) {


    /*******************
Header
********************/

  nav {
    background: none;
    float: right;
    font-size: 1.125em;
    margin-right: 5%;
    text-align: right;
    width: 45%;
  }

  #logo {
    float: left;
    margin-left: 5%;
    text-align: left;
    width: 45%;
  }

  h1 {
    font-size: 2.5em;
  }

  h2 { 
    font-size: 0.825em;
    margin-bottom: 20px;
  }

  header {
    border-bottom: 5px solid #777;
    margin-bottom: 60px;
  }




}

Thanks people !

Muhammad Rizwan
Muhammad Rizwan
8,595 Points

Joshua I am not seeing <html>, <body> tags, like this,

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Josh Nevard My Website</title> <link rel="stylesheet" href="css/normalize2.css"> <link href='http://fonts.googleapis.com/css?family=Averia+Sans+Libre%7CIndie+Flower' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="css/main.css"> <link rel="stylesheet" href="css/responsive.css"> <meta name="viewport" content="width-device-width, intial-scale-1.0"> </head> <title></title>

<body> <header> <a href="index.html" id="logo"> <h1>Josh Nevard</h1> <h2>My website</h2> </a> <nav> <ul> <li><a href="index.html" class="select">Portfolio</a></li> <li><a href="about.html">About</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav> </header> <div id="wrapper"> <section> <ul id="gallery"> <li> <a href="img/farleyth1.JPG"> <img src="img/farleyth1.JPG" alt="Farley Shoot"> <p>Farley down the beach.</p> </a> </li> <li> <a href="img/farleyth2.JPG"> <img src="img/farleyth2.JPG" alt="Farley Shoot"> <p>Farley posing:).</p> </a> <li> <a href="img/farleyth3.JPG"> <img src="img/farleyth3.JPG" alt="Farley Shoot"> <p>Farley being sleepy.</p> </a> <li> <a href="img/farleyth4.JPG"> <img src="img/farleyth4.JPG" alt="Farley Shoot"> <p>Farley chilling down the beach.</p> </a> <li> <a href="img/farleyth5.JPG"> <img src="img/farleyth5.JPG" alt="Farley Shoot"> <p>Farley show me his tongue.</p> </a> </ul> </section> <footer> <a href="http://www.facebook.com/josh.nevard.7"><img src="img/facebook-wrap.png" alt="Facebook Logo" class="social-icon"></a>

<p>&copy; 2014 Josh Nevard.</p>

</footer> </div> </body> </html>

Muhammad Rizwan
Muhammad Rizwan
8,595 Points

On your css, nav { text-align: centre; You do not need to mention text align here, padding: 10px 0; margin: 20px 0 0; } you have to give your nav tag a width because its covering whole space

final code that will work is nav { margin: 0 auto; width: 20%; margin-bottom: 5px; } This will bring your nav menu in the center.

Hi Muhammad,

It has aligned but to the centre but has done so whilst the browser is at full page, Im needing it to respond to the centre when its in mobile view or when the web browser is moved to condense the page.

Thanks man

I have solved the issue, many thanks!.

jason chan
jason chan
31,009 Points

could be spelled center the american way.