?
Solved

Why containing div doesn't stretch?

Posted on 2007-11-18
2
Medium Priority
?
3,072 Views
Last Modified: 2012-06-21
I have a page with a container div that holds a content div. What I don't understand is why doesn't the container div stretch and show the background color to the bottom of the content div? It stops half way down the page? Scaled down code sample provided? I want the container to stretch dynamically depending on the height of the content div. Why isn't it doing that?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<style>
	*{margin:0; padding:0}
	body{
	 	font-family: Verdana, Ariel, Sans-Serif;
	 	font-size: 12px;
	 	color: #5A5A5A;
	 	margin: 0;
     	padding: 0;
     	text-align:center;
	 	background: #8E8E8E;
 	}
	
	div#container {
		position:relative;
		width: 766px;
	 	background: #7f0b29;
	   	padding: 0px;
	 	margin-right: auto;
	 	margin-left: auto;
	 	margin-top: 0px;
	 	margin-bottom: 0px;
		text-align:left;
	 }
	 
	 		
	div#content {
		position:relative;
		top:232;
		left:30;
     	width: 500px;
	 	height:500px;
	 	background: #162732;
	 
	}
</style>
<body>
<div id="container">
	<div id="content">
		TESTING HERE<br><br><br>TESTING HERE<br><br><br>TESTING HERE<br><br><br>TESTING HERE
	</div>
</div>
</body>
</html>

Open in new window

0
Comment
Question by:894359
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
ToastedWeb earned 1000 total points
ID: 20307452
try not to position elements with position relative.... it is a bad practice ...

btw. <style></style> should be inside <head> tags, but the best way is to include it into html file...

take a look at my example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>
<style>
 
* { margin:0; padding:0; }
	
#container1 {
margin:0 auto;
padding:0;
width:800px;
background:red;
border:solid 1px white;
}
 
#container2 {
margin:200px 0 0 20px;
background:black;
width:300px;
height:700px;
}
	
</style>
</head>
 
<body>
<div id="container1">
	<div id="container2">
		<p>Your test content here :)</p>
	</div>
</div>
</body>
</html>

Open in new window

0
 
LVL 3

Assisted Solution

by:Mr_Splash
Mr_Splash earned 1000 total points
ID: 20309949
ToastedWeb is correct, relative positioning should not be used as the default way of positioning. Personally I would only use it to tweak an elements position when adding seperate browser dependent stylesheets.

Below is the code you gave with some changes.

Also some other CSS pointers are that
1) When setting a value as 0, you can drop the 'px'.
2) try to avoid * { margin:0; padding:0; } as it messes around with things you may want padding and margin on, such as lists.
3) Add <!-- and --> inside your style tags as shown below, this stops older browsers that can't read CSS showing all the code on the page. Not very common now a days, but it's good practive

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Page</title>
<style type="text/css" media="all">
<!--
body, html {
  font-family: Verdana, Ariel, Sans-Serif;
  font-size: 12px;
  color: #5A5A5A;
  margin: 0; padding: 0;
  text-align: center;
  background: #8E8E8E;
 }
        
#container {
  width: 766px;
  background: #7f0b29;
  padding: 0; margin: 0 auto;
  text-align: left;
}
         
#content {
  margin: 232px 0 0 30px;
  width: 500px; height:500px;
  background: #162732;
}
-->
</style>
</head>
<body>
 
<div id="container">
  <div id="content">
    TESTING HERE<br><br><br>TESTING HERE<br><br><br>TESTING HERE<br><br><br>TESTING HERE
  </div>
</div>
 
</body>
</html>

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question