Search code examples
csshtmlmarginpadding

<div>'s automatically adding a gap/padding/margin when content is added


I made a simple site using stack-able divs inside of a wrapper div. The problem is that every time I add content to the , it suddenly adds a space of some type, and I cannot get rid of it. The content in the divs will be dynamic so the height won't be always the same.

Why does this happen? Is it a problem the div or the content itself?

Image below the div's without content

http://i.imgur.com/wbPfwfW.png

Image below the div's with content

http://i.imgur.com/R6ycvYP.png

My CSS

@charset "utf-8";
/* CSS Document */

/* Basic */
body {
    background-color: #666;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
}
#wrapper {
    width: 900px;
    height: auto;
    margin: 10px auto;
    position: relative;
    background-color: #999;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
    -moz-box-shadow: 0 5px 18px rgba(0, 0, 0, 0.3);
    -webkit-box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
}
#header {
    width: 900px;
    height: 180px;
    background-color: #9C0;
}
#menubar {
    width: 900px;
    height: 36px;
    background-color: #906;
}
#content {
    width: 900px;
    height: 350px;  
    position: relative;
    background-color: #036;
}
#footer {
    width: 900px;
    height: 40px;
    position: relative;
    background-color: #F90;
}

/* Nav */
#nav {
    width: 100%;
    margin: 0;
    padding: 0;
    list-style: none;
}
#nav li {
    float: left;
}
#nav li a {
    display: block;
    padding: 8px 15px;
    text-decoration: none;
    font-weight: bold;
    color: #000;
}
#nav li a:hover {
    color: #000;
    background-color: #fff; 
}

My HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
  <div id="header">
  </div>
  <div id="menubar">
  <ul id="nav" >
      <li><a href="#">Item 1</a></li>
      <li><a href="#">Item 2</a></li>
      <li><a href="#">Item 3</a></li>
      <li><a href="#">Item 4</a></li>
  </ul>
  </div>
  <div id="content">
  </div>
  <div id="footer">
  </div>
</div>


Solution

  • Looking at the CSS you provided, it seems that you are forgetting to normalize it. Add at the very least this to the top of your CSS file:

    * { 
      margin: 0; 
      padding: 0; 
    }
    

    This will remove any browser specific margins or padding from elements, which may be causing the whitespace.