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

Header and Nav's background color disappear when screen's width is larger than 660px

My question was in the title. I was viewing the website in Chrome and typing code in Sublime text because the Internet was slow. Although I got the position of the logo and nav similar to Nick, the background color disappear. I will be attaching my code below, please excuse me if I don't follow the video's code precisely because I was playing around with the code

Here's my index.html

<!-- mobile-first approach to web design: design the mobile version of our site first -> desktop, tablets... Simple to complex instead of complex to simple-->
<!DOCTYPE html>
<html>
  <head> 
    <meta charset="utf-8">
    <title> Title </title>
    <link rel="stylesheet" href="CSS/normalize.css">  <!-- normalize.css resets styling and eliminate inconsistency btw web browsers-->
    <link href="https://fonts.googleapis.com/css?family=Changa+One|Dosis|Droid+Serif|Open+Sans:400,400i,700,700i|Sahitya" rel="stylesheet">     <!-- google.com.fonts. must be before main.css but after normalize.css -->
    <link href="https://fonts.googleapis.com/css?family=Dancing+Script:700" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Satisfy" rel="stylesheet">

    <link rel="stylesheet" href="CSS/main.css">
    <link rel="stylesheet" href="CSS/responsive.css">

  </head>

  <body>
    <header>
      <div class="logo">
        <a href = "index.html" id="logo">
          <h1> Anh Pham </h1>
          <h2> Student  </h2>         
        </a>
      </div>

      <nav>
        <ul>
          <li> <a href="index.html" class="selected"> Portfolio </a></li>    <!-- class is same as id. However, one page can have multiple  same name class, whereas each id must have unique name on each page-->
          <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="Image/numbers-01.jpg">
            <img src="../Image/numbers-01.jpg" alt="Error">    
            <!-- alt display the error message-->
            <p> Experimentation with color and texture </p>
            </a>
          </li>

          <li>
            <a href="../Image/numbers-02.jpg">
              <img src="../Image/numbers-02.jpg" alt="Error">    
            <!-- alt display the error message-->
              <p> Playing with blending modes in Photoshop </p>
            </a>
          </li>

          <li>
            <a href="../Image/numbers-06.jpg">
            <img src="../Image/numbers-06.jpg" alt="Error">    
            <!-- alt display the error message-->
            <p> Trying to create an 80's style of glows </p>
            </a>
          </li>

          <li>
            <a href="../Image/numbers-09.jpg">
            <img src="../Image/numbers-09.jpg" alt="Error">    
            <!-- alt display the error message-->
            <p> Drips created using Photoshop brushes </p>
            </a>
          </li>

          <li>
            <a href="../Image/numbers-12.jpg">
            <img src="../Image/numbers-12.jpg" alt="Error">    
            <!-- alt display the error message-->
            <p> Creating shapes </p>
            </a>
          </li>

        </ul>
      </section>
      <footer>


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

</html>

<!-- Cmd ]  to indent multiple lines -->

Here's my main.css

/**************************************
          GENERAL
**************************************/
/* WRAPPER = body - header */

#wrapper {
  max-width: 900px;           /* max width allowed for mobile, desktop*/
  margin: 0 auto;             /* set margin on top and bottom, auto center the content */         
  padding: 0 5%;              /* 0 on top and bottom, 5% left and right */
/*  background-color: #FBEEE6; */
}

body {
  font-family: 'Sahytia', sans-serif;
}


/* remove underline for hyperlinks */
a {
  text-decoration: none;
}


img {
  max-width: 100%;   /* tells image to fill the width of whatever platform viewer is using */
}
/**************************************
          HEADING
**************************************/
#header{
  float: left;
  margin: 0 0 30px 0;
  padding: 0 0 0 0;
  width: 100%;     /* float, element would stretch the entire page */
}

#logo {
  color: black ;
  text-decoration: none;
  text-align: center;
  margin: 0 auto;      /* 0pixel margin for all 4 sides of id="logo", change number to see difference */

}


#logo h1 {
  font-family: 'Dancing Script', cursive;
  /* Font must be in single quote. In case google failed to load Changa One, it will load font sans-serif *//*override body's font type*/
  margin: 20px 0;       /* 15px on top and bottom, 0 on left and right */
  padding: 30px 0 0;
  font-size: 4em;    /* 1em = 16px in most browser, auto-scale */
  font-weight: normal;  /* unbold the headline because headline element are bold by default */
  line-height: 0.8em;   /*determine the amount of space between each line of text, like in paragraph */ 
} 

#logo h2 {
  font-size: 1.25em;  
  margin: -5px 0 0px;       /* top left/right bottom */ /*top 5px closer to h1 */
  font-weight: normal;  
  font-family: 'Satisfy', cursive;

}

/**************************************
          NAVIGATION 
**************************************/

nav {
  text-align: center;
  padding: 10px 0;      /* between content and border */ /* 10px top and bottom, 0 left and right */
  margin: 20px 0 0;     /* 20px top, 0 left & right, 0 bottom */
}

nav ul {
  list-style: none;
  margin: 0 10px;       /* 10px both left and right */
  padding: 0;
}

nav li {
  display: inline-block;    /* align all list items to a line */  /* block has width and height */
}

nav a {
  font-weight: 800;
  padding: 0px 30px;
}

/**************************************
          FOOTER 
**************************************/
footer{ 
  font-size: 0.75em;
  text-align: center;
  clear: both;         /*footer was affected by "float", clear will negate this influence, "both" means both left and right side */
  padding-top: 50px;  

}

.social-icon {
  width: 35px;    /*resize */ 
  height: 35px;
  margin: 0 5px;
}

/**************************************
        PAGE: PORTFOLIO 
**************************************/
#gallery{
  margin: 0;
  padding: 0;
  list-style: none;       /* remove bullet points */
}

#gallery li {       /* select <li> inside gallery */
  float: right;     /* setting items side by side */
  width: 45%;       /* 45% on both left and right of each item, will auto-scale to parent's window */ /*45 * 2 + 10 = 100% */
  margin: 2.5%;     /* 2.5% on both left and right of each item, 2.5 * 2 * 2 = 10 */
  background-color: #AED6F1;
}

#gallery li a p {
 color: #A569BD;
 margin: 0%;     /* paragraph has margin by default */
 padding: 5%;
 font-size: 0.95em;

}

/**************************************
        PAGE: ABOUT 
**************************************/
.profile-photo{
  display: block;       
  width: 450px;
  height: 250px;

  max-width: 250px;
  margin: 0 auto 30px;     
  border-radius: 100%; 
}  

h5 {
  color: purple;
  text-align: center;

}

h2 {
  margin: 0 0 2em 0;    /*right 0, top 0, 1em left, 0 bottom */
  text-align: center;
} 

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




.contact-info li.phone-number a {  
  background-image: url('../Image/phone.png');   
}
  .contact-info li.email a {     
  background-image: url('../Image/mail.png');    
}

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


.contact-info li  {
  display: block; 
}

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

/**************************************
          COLOR
**************************************/
p {
  color: black;
}


body {
  background-color: #FFE4E1;
  color: grey;                 /* footer */
}

header{
  background-color: #FF1493 ;
  border-color: #2C3E50;
}

nav, nav a{
  color: black;
  text-decoration: none;
}


nav a, nav a:visited{       
  color: green;
}


nav a.selected, nav a:hover{  
  color: brown;
}

Here's my responsive.css

@media screen and (min-width: 480px) { 
  #primary-column {
    width: 50%;
    float: left;
  }

  #secondary-column{
    width: 40%;
    float: right;   /*in case unexpected browser issues */
  }

/**************************************
      PAGE: PORTFOLIO
**************************************/
    #gallery li {
      width: 28.33333%;
    }


    #gallery li:nth-child(3n+1) {   
        clear: both;     
    }


  /**********************************
      PAGE: ABOUT
  ***********************************/

  .profile-photo {
    float: left;
    margin: 3.125em 15% 12.5em 0;    
  }

  #about-content {
    float: right;
    margin: 3.25em 0 0 2%;
  }


} 




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


  /**********************************
      PAGE: HEADER
  ***********************************/


  nav {
      background: none; /* text can sit against the header background */
      float: right;
      font-size: 1.125em
      margin-right: 5%;   /* margin on just the right side */
      text-align: right;
      width: 45%;

  }

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

}

3 Answers

Hi Anh,

You have your header selector as #header but it's not an id. None of those styles are being applied. The header is floated to prevent the collapsing that you're seeing.

If you change it to header then the styles will be applied.

header{
  float: left;
  margin: 0 0 30px 0;
  padding: 0 0 0 0;
  width: 100%;     /* float, element would stretch the entire page */
}

Floating the header was how Nick chose to take care of the problem but the clearfix hack that Chris has posted is another way that you could do it.

Hi Anh

Most likely your header background color disappeared because you floated the nav. This causes its parent element (header) to collapse. So create this class

.clearfix:after {
content: "";
display: table;
clear: both;
}

And add this class to the header

<header class="clearfix">

Hi Jason, Thank you very much for your prompt help. Your answer did solve my problem