开发者

Making a site with 3 div with center content and 2 "buffers" to fill the reminig horizontal space

开发者 https://www.devze.com 2023-01-26 02:32 出处:网络
I am making a css site and I want to look like that: 3 vertical divs. LEFT CENTER RIGHT. In the center div is the site content, and I want the left and right div to fill the space between the center a

I am making a css site and I want to look like that: 3 vertical divs. LEFT CENTER RIGHT. In the center div is the site content, and I want the left and right div to fill the space between the center and the browser borders.

Here is my code.

    #container
{
 width:100%;
 background-color:#000;
}
.center
{
 width:1000px;
 height:400px;
 background-color:#F90;
开发者_Go百科 margin: 0px auto;
 overflow: auto;
}
.spacer-left
{ 
 width:100%;
 height:400px;
 background-color:#F90;
 float:left;
}
.spacer-right
{ width:100%;
 height:400px;
 background-color:#F90;
}

And here is the 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=windows-1251" />
<title>Untitled Document</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
<div class="spacer-left"></div>
<div class="center" style="background-color:#F30;"></div>
<div class="spacer-right"></div>
<div style="clear:both"></div>
</div>
</body>
</html>

I've try with 2 divs and there was no problem. The left with float:left and width in pixels and right one with 100% width without float. It worked. But how to make it with 3 divs. Thanks in advance!


Thank you for the help Sebastian but I figured out other way. This is how the code looks now and it works.

#container
{
    width:1000px;
    background-color:#000;
    margin:0 auto;
}
.center
{
    width:100%;
    height:400px;
    background-color:#F90;
    margin: 0px auto;
    overflow: auto;
}
.spacer
{   
    width:50%;
    height:400px;
    background-color:#F90;
    float:left;
}
.head_warp
{
    width:100%;
    display:block;
    height:400px;
    margin:0 auto;
    padding:0;
    position:absolute;
    top: 0;
    left: 0;
    text-align:center;
    z-index:-9999;
}

and the 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=windows-1251" />
<title>Untitled Document</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="head_warp">
<div class="spacer"></div>
<div class="spacer" style="background-color:#F06"></div>
</div>
<div id="container">
<div class="center" style="background-color:#F30;"></div>
</div>
</body>
</html>

Thank you for the help one again. I will write here again if there is a problem with my solution.


Update: a different alternative to the answer below, using three divs and z-index.

http://jsfiddle.net/SebastianPataneMasuelli/mzvB4/1/

if you are trying to make a site with the content in the center div, you can replicate that layout using simply:

<body>
  <div class="center">center</div>
</body>

with the css:

html {height: 100%;   }
.center { width:750px; margin: 30px auto; height: 100%; }

play with it here: http://jsfiddle.net/SebastianPataneMasuelli/mzvB4/

from the class names .spacer_left and .spacer_right i'm assuming that those divs are empty spacers and not necessary.

this is a good resource for base css layouts: http://www.csseasy.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消