I am attempting to overlay the comments div element over the main div element however am unsure of the following;
- Is this correct?
- Is there a better way to achieve it?
An example of the code can be found at http://jsfiddle.net/fTbP5/
CODE
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type=" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en-us" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<title>sample layout</title>
<base href="" />
<link rel="stylesheet" type="text/css" media="all" href="" />
<style type="text/css" media="all">
* {
margin: 0px;
padding: 0px;
}
body {
background-color: #eeeeee;
font-family: Arial, Verdana, sans-serif;
color: #ffffff;
}
#content {
width: 700px;
margin-top: 10px;
margin-right: auto;
margin-bottom: 10px;
margin-left: auto;
border-width: 1px;
border-color: #ffffff;
border-style: solid;
overflow: auto;
padding-top: 40px;
padding-bottom: 40px;
}
#header {
font-size: 1em;
color: #FFC700;
margin-left: 100px;
margin-bottom: 20px;
}
.main {
float: left;
width: 300px;
height: 300px;
background-color: #00ACED;
margin-left: 100px;
padding: 20px;
position: relative;
}
.comments {
width: 320px;
background-color: black;
position: absolute;
top: 305px;
left: 0px;
padding: 10px;
}
.shoutbox {
float: left;
width: 100px;
height: 100px;
background-color: orange;
margin-left: 50px;
margin-bottom: 20px;
}
.border {
border-width: 15px;
border-color: #ffffff;
border-style: solid;
}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<div id="container">
<div id="content">
<div id="header"><h1>Title</h1></div>
<div class="main border">
Hi {Name}, <br /> Your details are.
<div class="comments">comments</div>
</div>
<div class="shoutbox border">shoutbox1</div>
<div class="shoutbox border">shoutbox2</div>
</div>
</div>
</body>
</html>
As I said in the comments above, I think your solution is perfectly okay. Your comments div is nested into main, and absolutely positioned inside it. I see no hack here.
But that is not the only way to achieve that, and I’ll show you another one, which is a little more flexible. Namely, it makes your main box and your comment box allow variable height contents (while with your current solution, text would overflow outside their containers).
It’s based on two things:
main-contentsto wrap the contents. It’s a child of.mainmin-heighton.main,.main-contentsand.comments, to guarantee your original dimensions, but expand down if needed.HTML
CSS
To demonstrate that, I have set up a live example showing both your original content, and another block with taller content below it.