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

HTML

Why do these elements not appear beside each other?

Here is my code:

<html>
<head>
<style>
nav {
   padding: 25px;
   background-color: firebrick;
   border-radius: 5px;
}
.rantHeader {
   font-size: 25px;
   font-family: "Chalkduster", fantasy;
}
.homeButton {
   float: left;
}
</style>
</head>
<body>
<nav>
<div>
<h3 class = rantHeader>Rant.com</h3>
<button class = homeButton>Home</button>
</div>
</nav>
</body>
</html>

In the div, why does the home button appear under the h3 header? They are siblings, so shouldn't they appear side by side?

1 Answer

andren
andren
28,558 Points

The h3 element is a block level element. Block level elements take up the entire space of their container, or put more simply they will always occupy a line all by themselves, regardless of the width of the element.

The button element is an inline element, which essentially means that it only occupies the space of the element itself.

You can use CSS to change whether an element is treated as a block level element or an inline one by setting its display property. With this code for example:

<html>
<head>
    <style>
        nav {
            padding: 25px;
            background-color: firebrick;
            border-radius: 5px;
        }
        .rantHeader {
            font-size: 25px;
            font-family: "Chalkduster", fantasy;
            display: inline;
        }
        .homeButton {
            float: left;
        }
    </style>
</head>
<body>
    <nav>
        <div>
            <h3 class=rantHeader>Rant.com</h3>
            <button class=homeButton>Home</button>
        </div>
    </nav>
</body>
</html>

The h3 and button will appear next to each other.

Edit: Cleaned up the code a bit.

Was not aware of this, thanks Andren!