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 Enhancing Design with CSS Finishing Touches Applying Media Queries

Between screen pixel widths of 940px and 925px (inclusive) my two columns fall out of alignment- why?

As per the title, between those two screen widths the left hand side column/div moves out of horizontal alignment the right hand side one, with the top of the left being about 20pixels below compared to the right column.

Outside of this range the media query kicks in from 720px as expected to put the two columns side by side, and works perfectly outside of the range of 940 to 925px. Is this just general CSS annoyance, or something I have missed out?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Lake Tahoe</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Work+Sans:ital,wght@0,300;0,700;1,300&display=swap" rel="stylesheet"> 
  <link rel="stylesheet" href="css/style.css">
</head>
<body>

  <header id="top">
    <span class="title">Journey Through the Sierra Nevada Mountains</span>
    <h1>Lake Tahoe, California</h1>
  </header>

  <main>
    <section>
      <p class="intro">Lake Tahoe is one of the most breathtaking attractions located in California. It's home to a number of ski resorts, summer outdoor recreation, and tourist attractions. Snow and skiing are a significant part of the area's reputation.</p>
      <a class="callout" href="#more">Find out more</a>
    </section>

    <section>
      <div id="wildlife">
        <h2>Check out the Wildlife</h2>
        <p>As spawning season approaches, the fish acquire a humpback and protuberant jaw. After spawning, they die and their carcasses provide a feast for gatherings of <a href="#mink">mink</a>, <a href="#bears">bears</a>, and <a href="#eagles">bald eagles</a>.</p>
      </div>
      <a class="callout" href="#wildlife">See the Wildlife</a>
    </section>

    <section>
      <div class="column">
        <img src="img/resort.jpg" alt="Resort">
        <h3>From Tents to Resorts</h3>
        <p>Lake Tahoe is full of wonderful places to stay. You have the ability to sleep in the outdoors in a tent, or relax like a king at a five star resort. Here are our top three resorts:</p>
        <ul>
          <li><a href="#hotels">Lake Tahoe Resort Hotel</a></li>
          <li><a href="#resorts">South Lake Tahoe Resorts</a></li>
          <li><a href="#lodging">Tahoe Ski Resort Lodging</a></li>
        </ul>
      </div>
      <div class="column">
        <img src="img/mountain-landscape.jpg" alt="Mountain Landscape">
        <h3>Pack Accordingly</h3>
        <p>One of most important things when it comes to traveling through the great outdoors is packing accordingly. Here are a few tips:</p>
        <ol>
          <li>Bring layers of clothing</li>
          <li>Pack sunscreen</li>
          <li>Carry extra water just in case</li>
          <li>Pack light</li>
        </ol>
      </div>
    </section>
  </main>

  <footer class="main-footer">
    <p>All rights reserved to the state of <a href="#">California</a>.</p>
    <a href="#top">Back to top &raquo;</a>
  </footer>
</body>
</html>
/* Web Fonts ----------------------- */
@font-face {
  font-family: 'Abolition Regular'; 
  src: url('../fonts/abolition-regular-webfont.woff') format('woff');
}

/* Base Styles --------------------- */
* {
  box-sizing: border-box;
}
html {
  font-size: 1.25em; /* 20px on most screens */  
}

h1, h2 {
  font: 400 4rem/1.1 'Abolition Regular', Impact, Charcoal, sans-serif; 
}

h1 {
  text-shadow: 1px 2px 3px rgba(0,10,30,0.5);
}

h1::after {
  content: '\2193'; 
  display: block;
}

h2 {
 font-size: 2.5rem;
}

h3 {
 font-size: 1.25rem; 
}

body {
  margin: 1rem;
  color: #454959;
  font: 300 100%/1.5 'Work Sans', sans-serif;
}

a {
 color: #cc4e13;
 text-decoration: none;
}

img {
  width: 100%; /* responsive images */
  border-radius: 10px;
}


/* Main Styles --------------------- */
header {
 padding: 10vh 1rem;
 margin: -1rem -1rem 1rem;
 background: url(../img/mountains.jpg) center / cover no-repeat #f3753b;  
 background-blend-mode: multiply;
 color: white;
}

header, footer {
  text-align: center;
}

.title {
 letter-spacing: 1px; 
}

main {
 max-width: 1000px;
  margin: 0 auto;
 hyphens: auto; 
}

section {
 padding: 4rem 0;
 border-bottom: 1px dashed #9599a9;
}

#wildlife {
 padding: 20% 15%;
  border-top: 12px solid #f3753b;
  border-radius: 12px 12px 0 0;
  background: url(../img/bear.jpg) center / cover #454959;
  color: white;
  box-shadow: inset 0 0 20px 10px rgba(0,10,30,0.8);
}

#wildlife a {
 color: #f7a27c;
}

.callout {
  display: block;
  width: 250px;
  padding: 0.25rem 1rem;
  border-radius: 20px/10px;
  margin: 1rem auto;
  box-shadow: 1px 2px 3px rgba(0,10,30,0.5);
  background-image: linear-gradient(135deg, rgba(255,255,224,0.8) 0%, rgba(250,100,50,0.2) 50%), linear-gradient(#b40 0%, #d63 50%, #c51 51%, #e74 100%); 
  color: white;
  text-align: center;
  font-size: 1.25rem;
}

.column:first-child {
 margin-bottom: 4rem; 
}

/* Media Queries ------------------- */
@media screen and (min-width: 720px) {
  header {
   padding: 20vh 1rem; 
  }
  h1 {
   font-size: 5rem; 
  }
  h1::after {
   margin-top: 1rem; 
  }
  .title {
   font-size: 1.25rem; 
  }
  .intro {
   padding: 0 15%;
    font-size: 1.1rem;
  }
  .column {
    display: inline-block;
    width: 48%;
    margin-right: -4px;
  }

  .column:first-child {
   margin-right: calc(4% - 4px);
   margin-bottom: 0;
  }
}

1 Answer

Alexandra Hadjidaki
Alexandra Hadjidaki
26,496 Points

For me it happens at a slightly different breakpoint, between 903px and 920px inclusive, but it does happen!

After playing around a little, this is basically what's happening: within this range of screen sizes, the two columns don't have the same height and so come out of alignment.

The left column remains at a height of 367px, but the right column wraps the paragraph to a new line which gives it a height of 397px.

If you look at the computed styles through the Dev Tools, you'll see that the two columns have a default vertical-align value of baseline. This is what's causing the misalignment.

You can fix this by assigning the columns a value of top for that property when the screen size is greater than 720px.

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

    .column {
        display: inline-block;
        width: 48%;
        margin-right: -4px;
        vertical-align: top; /* add this here */
    }

    ...
}