Stingray
Stingray

Reputation: 11

Why are these elements not staying within their DIV container?

I am new to CSS and it's not clear to me why the header and menu links stay within the container but not the other elements. I can fix this by including margins for the other elements but I would like to understand why this is happening.

Is there a way to keep elements within the container without specifying margins for each element or (div). See JS fiddle code and code below, for example. I would like to keep the #main content within #container.

JSFiddle link: http://jsfiddle.net/6qt8ry1L/

body {
  background-color: #F7F7F0;
}
#container {
  width: 1000px;
  position: relative;
  margin: 0 auto;
  text-align: center;
  background-color: #8e8e20;
}
header {
  background-image: url("header.jpg");
  background-repeat: no-repeat;
  height: 224;
}
h1 {
  color: #ffffff;
  padding: 10px;
  text-align: left;
}
#nav ul {
  list-style: none;
  background-color: black;
  height: 40px;
  margin-top: 0;
  color: white;
}
#nav li {
  display: inline-block;
  padding-top: 10px;
  padding-left: 50px;
  padding-right: 50px;
  padding-bottom: 10px;
}
#MainContent {
  margin-top: 10px;
  padding-top: 20px;
  border-top: 1px solid #545454;
  background-color: #b6c5a3;
  height: 200;
  color: #492b40;
  font: 11px/12px Verdana, Arial, Helvetica, sans-serif;
}
#col1 {
  width: 20%;
  height: 30%;
  margin-top: 15px;
  float: left;
  background-color: lightgray;
}
#col2 {
  float: left;
  margin-left: 1%;
  margin-top: 15px;
  height: 30%;
  width: 20%;
  background-color: lightgray;
}
#col3 {
  float: left;
  margin-top: 15px;
  width: 20%;
  height: 30%;
  margin-left: 1%;
  background-color: lightgray;
}
#Content:after {
  content: '';
  display: block;
  clear: both;
}
#footer {
  float: left;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #545454;
  background-color: #b6c5a3;
  height: 60;
  width: 950px;
  color: #492b40;
  font: 11px/12px Verdana, Arial, Helvetica, sans-serif;
}
<html>

<head>

  <link rel="stylesheet" href="Style.css">

</head>

<body>

  <div id="container">


    <header>

      <div id="heading">

        <h1> Hello there !!! </h1>

      </div>

    </header>



    <div id="nav">


      <ul>

        <li>Home</li>
        <li>About</li>
        <li>Contact</li>
        <li>Links</li>

      </ul>

    </div>


  </div>

  <div id="MainContent">

    Main Content

  </div>


  <div id="Content">

    <div id="col1">

      Col1

    </div>

    <div id="col2">

      col2

    </div>

    <div id="col3">

      col 3

    </div>

  </div>

  <div id="footer">


    <p>Copyright 2004 xyz Association</p>

    <p>All rights reserved etc etc..</p>

  </div>

  </div>
  <!--end container-->

</body>

</html>

Upvotes: 0

Views: 133

Answers (2)

AndronsWeb
AndronsWeb

Reputation: 1

I noticed you have more closing tags than you have opening DIVs. As a habit when ever you create a <DIV> be sure to insert </DIV> immediately. To keep track of the opening and closing DIVs. I am guessing this mismatch closing DIVs is likely the cause of your problem.

Upvotes: 0

Vikas Baru
Vikas Baru

Reputation: 162

Your container is closed just after closing div#nav. Put that closing div at the end and your problem'd be fixed.

Upvotes: 1

Related Questions