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 CSS Layout Basics CSS Layout Project Column Layout with Media Queries

Colin Sygiel
Colin Sygiel
5,249 Points

Why are my 3 columns not centering? Also, why aren't the nav categories vertically centering on nav bar?

html:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Best City Guide</title>
    <link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <div class = page>
    <header class="main-header">
        <h1 class="name clearfix"><a href="#">Best City Guide</a></h1>
        <ul class="main-nav clearfix">
            <li><a href="#">ice cream</a></li>
            <li><a href="#">donuts</a></li>
            <li><a href="#">tea</a></li>
            <li><a href="#">coffee</a></li>
        </ul>
    </header><!--/.main-header-->   

    <div class="banner">
        <img class="logo" src="img/city-logo.svg" alt="City">
        <h1 class="headline">The Best City</h1>
        <span class="tagline">The best drinks and eats in the best city ever.</span>
    </div><!--/.banner-->

    <div class ="container">
    <div class="secondary col">
        <h2>Welcome!</h2>
        <p>Everything in this city is worth waiting in line for.</p>
        <p>Cupcake ipsum dolor sit. Amet chocolate cake gummies jelly beans candy bonbon brownie candy. Gingerbread powder muffin. Icing cotton candy. Croissant icing pie ice cream brownie I love cheesecake cookie. Pastry chocolate pastry jelly croissant.</p>
        <p>Cake sesame snaps sweet tart candy canes tiramisu I love oat cake chocolate bar. Jelly beans pastry brownie sugar plum pastry bear claw tiramisu tootsie roll. Tootsie roll wafer I love chocolate donuts.</p>
    </div><!--/.secondary-->

    <div class="primary col">
        <h2>Great food</h2>
        <img class="feat-img" src="img/treats.svg" alt="Drinks and eats">
        <p>Croissant macaroon pie brownie. Cookie marshmallow liquorice gingerbread caramels toffee I love chocolate. Wafer lollipop dessert. Bonbon jelly beans pudding dessert sugar plum. Marzipan toffee drag&#233;e chocolate bar candy toffee pudding I love. Gummi bears pie gingerbread lollipop.</p>
    </div><!--/.primary-->

    <div class="tertiary col">
        <h2>How to get here</h2>
        <p><strong>Plane: </strong>Tiramisu caramels gummies chupa chups lollipop muffin. Jujubes chocolate caramels cheesecake brownie lollipop drag&#233;e cheesecake.</p>
        <p><strong>Train: </strong>Pie apple pie pudding I love wafer toffee liquorice sesame snaps lemon drops. Lollipop gummi bears dessert muffin I love fruitcake toffee pie.</p>
        <p><strong>Car: </strong>Jelly cotton candy bonbon jelly-o jelly-o I love. I love sugar plum chocolate cake pie I love pastry liquorice.</p>
    </div><!--/.tertiary--> 
    </div>

    <footer class="main-footer">
        <span>&copy;2015 Residents of The Best City Ever.</span>
    </footer>
    </div>
</body>
</html>

css:

/* ================================= 
  Base Element Styles
==================================== */

* {
    box-sizing: border-box;
}

body {
    font-family: 'Varela Round', sans-serif;
    line-height: 1.6;
    color: #3a3a3a;
}

p {
    font-size: .95em;
    margin-bottom: 1.8em;
}

h2,
h3,
a {
    color: #093a58;
}

h2,
h3 {
    margin-top: 0;
}

a {
    text-decoration: none;
}

img {
    max-width: 100%;
}

.main-footer {
  text-align: center;
}

/* ================================= 
  Base Layout Styles
==================================== */

/* ---- Navigation ---- */

.name {
    font-size: 1.25em;
}

.name a,
.main-nav a {
    text-align: center;
}

.main-nav a {
    font-size: .95em;
    color: #3acec2;
    text-transform: uppercase;
}

.main-nav a:hover {
    color: #093a58;
}

/* ---- Layout Containers ---- */

.banner {
    color: #fff;
    background: #3acec2;
}

.main-footer {
    background: #d9e4ea;
    padding: 2em 0;
    margin-top: 30px;
}

/* ---- Page Elements ---- */

.logo {
    width: 190px;
}

.banner {
  text-align: center;
  padding-top: 40px;
  padding-bottom: 40px;
}


.col {
  vertical-align: text-top;
  margin-top: 4%;
}

.container {
  margin: 0 auto;
  width: 90%;
}

/* ================================= 
  Media Queries
==================================== */

@media (min-width: 300px) {


.main-header {
  text-align: center;
  width: 100%;
}

.col {
  padding-left: 1em;
  padding-right: 1em;
}

}

@media (min-width: 769px) {

.main-header {
  display:inline-block;
  width: 100%;
}

.headline {
  margin-bottom: -3px;
}

.col {
  display: inline-block;
  margin: 0 auto;
  margin-top: 4%;
}

.primary {
  width: 40%;
}

.secondary,
.tertiary {
  width: 30%;
  }

.name {
  float:left;
  margin-left: 3%;
  vertical-align: middle;
}

.main-nav {
  float: right;
}

.main-nav li {
  display:inline-block;
  margin-left: 15px;
}

.main-header {
  vertical-align: middle;
}

.main-nav {
  margin: auto 0;
}


    /* ---- Float clearfix ---- */

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

}

3 Answers

Jean Paul Giraldo
Jean Paul Giraldo
15,701 Points

Hi Colyn,

If I recall correctly this is from the Flexbox module right? so this is what I would do:

  1. To center the 3 columns on your layout I would add

.container{display:flex;} .col{flex:1;}

remember to always add display: flex; in your container elements, is mandatory if you want your flexbox properties to work and flex: 1; is just so all your columns are the same width, this would mean you don't need

.primary{width:40%;} .secondary, .tertiary{width:30%;}

if you want the .primary column larger than the other too you can add .primary{flex:2;} for a given media query, this would automatically set the .primary column to take twice as much space as other columns in your container element.

  1. Your navigation menu is not vertical aligning because you set:

.main-nav{margin:0 auto;}

I removed it and it worked. This is forcing margin-top: 0; and margin-bottom: 0; if it doesn't try vertical-align: middle;

good luck!

lorenaolguin
lorenaolguin
6,034 Points

If you reduce the width of any of the three columns, for example the primary column by just 1%, the three columns will appear centered on your container. I think the problem is that though in theory the three columns fit in the container, it doesn´t always work like that in the browser.

And if you remove the margin: 0 auto, from your @media (min-width: 769px) .main-nav; your navigation bar will be vertically centered. I hope this helps.

Tomasz Halaczkiewicz
Tomasz Halaczkiewicz
21,845 Points

You didn't flow your columns left. Just do it and they will appear next to each other. your nav bar list items are floated right, undo it if you want them in other place.