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 Techniques Display Modes Inline-Block Formatting

melissa brown
melissa brown
4,670 Points

inline block not working ?

hi when i use inline the nav elements align side by side by when i use inline block they go back to being on top of one another. Not sure what im doing wrong.

<!doctype>
<!DOCTYPE html>
<html>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="css/style.css">
<head>
    <title></title>
</head>
<body>
<div class="main-wrapper">
<header class="main-header">
      <nav class="main-nav">
        <ul>
          <li><a href="index.html"class="selected">Portfolio</a></li>
          <li><a href="about.html">About</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>
    <div>
    <h1>hello </h1>
    </div>
</div>
</body>
</html>

css

/* page styles */

* {
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.main-wrapper {
    width: 900px;
    height: 600px;
    margin: auto;
    background: #FF8C69;
    color: white;
}

.main-header, {
    padding: 30px;
    background-color: blue;

}

h1 {
    position: relative;
    text-align: left;
}

h2 {
    position: relative;
    text-align: left;
}
d

/* navigation */
.main-nav,
.main-nav li {
    display: inline-block;
    width: 100%;
}

.main-nav li {
    margin-right: 10px;

}

.main-nav {
    background: yellow;
    vertical-align: midle;
    padding: 20px;
}


/* nav link*/ 
nav a, nav a:visited {
  color: #fff;
}
/* selected nav link*/ 
nav a.selected, nav a:hover {
  color: #32673f;
}
melissa brown
melissa brown
4,670 Points
/* page styles */

* {
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.main-wrapper {
    width: 900px;
    height: 600px;
    margin: auto;
    background: #FF8C69;
    color: white;
}

.main-header, {
    padding: 30px;
    background-color: blue;

}

h1 {
    position: relative;
    text-align: left;
}

h2 {
    position: relative;
    text-align: left;
}
d

/* navigation */
.main-nav,
.main-nav li {
    display: inline-block;
    width: 100%;
}

.main-nav li {
    margin-right: 10px;

}

.main-nav {
    background: yellow;
    vertical-align: midle;
    padding: 20px;
}


/* nav link*/ 
nav a, nav a:visited {
  color: #fff;
}
/* selected nav link*/ 
nav a.selected, nav a:hover {
  color: #32673f;
}
melissa brown
melissa brown
4,670 Points

sorry reposted the css code below

4 Answers

I would answer this but, if you check out the link below, there is no way I could explain it any better without copying his answer. He also provides links to W3C and another page with a visual example of both.

http://stackoverflow.com/questions/9189810/css-display-inline-vs-inline-block

melissa brown
melissa brown
4,670 Points

sorry maybe my question wasnt that clear. i know what the difference is between them. my question is more that inline-block is not working in my code.

Sorry, I was hinting more towards Oldskool's answer where he mentions inline items cannot have a set width.

Placid Rodrigues
Placid Rodrigues
12,630 Points

Hi melissa,

I think the problem is the width: 100% declared for the .main-nav and .main-nav li elements. Remove the width: 100% and check.

Regards, Placid

Stephen Bolton
seal-mask
.a{fill-rule:evenodd;}techdegree
Stephen Bolton
Front End Web Development Techdegree Student 7,933 Points

There is a lot of errors in your html. I wonder if you didn't copy all of the text when you posted it. Also your css has a lot of classes that aren't matched to the html. Are you working on a personal project and just trying to use the CSS that was in the video?