Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML DIV element Disappears with no content

Tags:

html

css

layout

When I create an HTML div element with no content, it disappears.

When the div is populated, like this HTML, then it works right.

<!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 no content:

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

The red box disappears:

alt text

It's like there is no div there! How can I always have the program show the div with no content?

like image 336
Alix Axel Avatar asked Jan 22 '10 02:01

Alix Axel


1 Answers

When the div is 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.

like image 110
cletus Avatar answered Sep 27 '22 21:09

cletus