tags:

views:

181

answers:

5

I'm having a lot of trouble with this, given the following HTML:

<!doctype html>
<head>
<style>
    .nav {
        width: 26%;
        display: inline;
        float: left;
        margin-left: 2%;
        margin-right: 2%;
        background-color: #FF0000;
    }
    .content {
        width: 56%;
        display: inline;
        float: left;
        margin-left: 2%;
        margin-right: 2%;
        background-color: #0000FF;
    }
</style>
</head>

<body>
    <div style="width: 600px;">
        <div class="nav"><p>nav</p></div>
        <div class="content"><p>content</p></div>
    </div>
</body>
</html>

I get the following (expected) output:

alt text

However, if I change the div element with the class = nav to this:

<div class="nav"></div>

I get this (not expected and undesirable) output:

alt text

It's like there is no div there! How can I get the following output:

alt text

Surely there must be a simple way to do this, no?

+9  A: 

When it's empty the element has no height. So what's actually happening is that it's there but has 0 height.

You could put something in it (like &nbsp; or give it height and/or line-height. I'd suggest giving the other div the same height.

cletus
Thanks, CSS can be a PITA (every|some)times. I went with the ` ` approach.
Alix Axel
+3  A: 

Put a non-breaking space in it. That's what I do when I need something, but not nothing. You may also be able to give it an explicit height to get the same result.

Jonathan Sampson
+1  A: 

Make sure the contains some kind of content.   is usually the best. It ensures that there is at least something for the browser to display/render. This also might be a cause of your DTD.

Joel Etherton
A: 

Alix,

you need to add a height value to the class .nav

    <!doctype html> 
<head> 
<style> 
    .nav { 
        width: 26%; 
        height: 50px;
        display: inline; 
        float: left; 
        margin-left: 2%; 
        margin-right: 2%; 
        background-color: #FF0000; 
    } 
    .content { 
        width: 56%; 
        display: inline; 
        float: left; 
        margin-left: 2%; 
        margin-right: 2%; 
        background-color: #0000FF; 
    } 
</style> 
</head> 

<body> 
    <div style="width: 600px;"> 
        <div class="nav"></div> 
        <div class="content"><p>content</p></div> 
    </div> 
</body> 
</html> 
PantsOffNow
Sweet display name.
jeerose
A: 

after you add a height value also add a display property to the nav class like:

height:1em;
display:block;
rob