布局困境:标题,内容和页脚水平扩展背景颜色(但同时具有居中内容)?

时间:2009-12-05 21:30:37

标签: css layout colors background

我创建了一个布局,所有内容都是居中的(使用“margin =”0 auto“技术)。我还希望页眉和页脚是黑色的,当浏览器向两侧放大时会扩展到两侧。如果我居中所有黑色背景也将居中,它不会无限扩展。我找到的唯一解决方案是将背景样式应用于页眉和页脚,并使用其中的类(.container)来居中内容(我认为Stack Overflow使用此使用页脚的技术。。这很好,但我想知道是否有更好的方法而不是添加额外的div?

我的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>globo design</title> 
        <link href="styles/layout.css" rel="stylesheet" type="text/css" />
        <link href="styles/slideshow.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript" src="scripts/slideshow.js"></script> 
    </head> 
    <body id="home">
    <!-- header -->
    <div id="header">
        <div class="container">
            <h1><a href="http://widerdesign.co.nr/">wider design</a></h1>
            <!-- navigation -->
            <ul id="navigation"> 
                <li class="home"><a href="index.php"><span>home</span></a></li>
                <li class="portfolio"><a href="portfolio.php"><span>portfolio</span></a></li>
                <li class="about"><a href="about.php"><span>about</span></a></li>
                <li class="contact"><a href="contact.php"><span>contact</span></a></li>
            </ul>
        </div>
    </div>
    <!-- content -->
    <div id="content">
        <div id="top-column">
            <p>We <strong>design and develop</strong> clean and effective webs in the <strong>top 3 languages</strong> 
            on the Internet. Internet is mean to reach the whole world.You are mean to reach the whole audience:</p> 
        </div>
        <div id="middle-column">
            <h2>Our Work</h2>
            <!-- slideshow -->
            <div id="slideshow">
                <div id="slidesContainer">
                    <div class="slide">
                        Content for slide 1 goes here
                    </div>
                    <div class="slide">
                        Content for slide 2 goes here
                    </div>
                    <div class="slide">
                        Content for slide 3 goes here
                    </div>
                    <div class="slide">
                        Content for slide 4 goes here
                    </div>  
                </div>
            </div>
        </div>
            <div id="left-column">
                <h2>Web Design</h2>
                <p>Create a web site easily with this online HTML generator. Follow the steps below to create web pages then click "view html page" to test it once it's completed. You can copy and paste generated code where you wish within the generated document(s). For example: You created an HTML table with 3 rows and 3 columns. You then added a link, which appears below the HTML table. If you want the link inside the HTML table, just cut and paste it inside the table in place of an "ADD TEXT" statement. Any where text,images,or links need to be, there will be a generated "ADD TEXT" statement in the HTML pages.</p>
            </div>
            <div id="right-column">
                <h2>Web Translation</h2>
                <p>Create a web site easily with this online HTML generator. Follow the steps below to create web pages then click "view html page" to test it once it's completed. You can copy and paste generated code where you wish within the generated document(s). For example: You created an HTML table with 3 rows and 3 columns. You then added a link, which appears below the HTML table. If you want the link inside the HTML table, just cut and paste it inside the table in place of an "ADD TEXT" statement. Any where text,images,or links need to be, there will be a generated "ADD TEXT" statement in the HTML pages.</p>
            </div>
        </div>
    </div>
    <!-- footer -->
    <div id="footer">
        <div class="container">

        </div>
    </div>
    <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
        </script>
        <script type="text/javascript">
        try {
        var pageTracker = _gat._getTracker("UA-11932489-1");
        pageTracker._trackPageview();
        } catch(err) {}</script>
    </body> 
    </html> 

我的CSS:

/* reset */
* { 
    margin: 0;
    padding: 0;
}
/* tags */
body {
    background-color: #FFFFFF;
    color: #5D5D5D;
    font-family: Arial, "MS Trebuchet", sans-serif;
    font-size: 75%;
}
h1 {
    background: #2D2D2D url(../images/logo.png) no-repeat scroll 0 0;
    margin-bottom: 20px;
    text-indent: -9999px;
}
h2 {
    color: #418EE4;
    font-family: Arial;
    font-size: 16px;
    font-weight: normal;
    margin-bottom: 10px;
}
a {
    font-family: Arial, "MS Trebuchet", sans-serif;
}
/* classes */
.container {
    margin: 0 auto;
    width: 960px;
}
/* header */
#header {
    background-color: #2D2D2D;
    padding-top: 10px;
}
/* navigation */
ul#navigation {
    height: 20px;
    list-style: none;
    margin: 0;
    padding: 0;
}
ul#navigation li {
    float: left;
}
ul#navigation li a {
    background: url(../images/tab.png);
    color: #C0C0C0;
    display: block;
    height: 20px;
    outline: none;
    padding-left: 10px;
    text-decoration: none;
}
ul#navigation li a:hover {
    color:  #418EE4;
}
#home li.home a, #portfolio li.portfolio a, #about li.about a, #contact li.contact a {
    background-position: 0 -32px;
    color: #757575;
}
ul#navigation span {
    background: url(../images/tab.png) 100% 0;
    display: block;
    line-height: 20px;
    padding-right: 20px;
}
#home li.home span, #portfolio li.portfolio span, #about li.about span, #contact li.contact span {
    background-position: 100% -32px;
}
/* content */
#content {
    background-color: #FFFFFF;
    padding: 20px;
    overflow: hidden;
    margin: 0 auto;
    width: 960px;
}
#content h2 {
    border-top: 2px dashed #F0F0F0;
    border-bottom: 2px dashed #F0F0F0;
    padding: 5px 0 5px 0;
    margin: 15px 0 15px 0;
}
#top-column {
    color: #818181;
    font-size: 16px;
    font-family: Arial, "MS Trebuchet", sans-serif;
    margin: 10px 0 10px 0;
    padding: 10px 0 20px 0;
}
#top-column strong {
    font-weight: normal; 
    color: #3C3C3C;
}
#middle-column {
    float: left;
}
#right-column {
    float: left;
    width: 420px;
}
#left-column {
    float: right;
    width: 500px;
}
/* footer */
#footer {
    clear: both;
    background-color: #2D2D2D;
    height: 200px;
}

1 个答案:

答案 0 :(得分:1)

我还没有测试过您的代码,但从描述中我听起来就像编写像这样的堆叠布局。我的HTML结构将是这样的(显然我已经省略了头,链接到样式表等等,为了一个简单的例子):

<html>
<body>
  <div id="header-wrap">
    <div id="header"> ... </div>
  </div>
  <div id="main-wrap">
    <div id="main"> ... </div>
  </div>
  <div id="footer-wrap">
    <div id="footer"> ... </div>
  </div>
</body>
</html>

我认为你可以看到如何为此编写CSS - 每个包装器(#header-wrap,#main-wrap,#footer-wrap)将应用背景和100%宽度,然后内部容器(#header,#main,#footer)将具有固定宽度而不应用背景。例如,如果您正在使用主要内部的2个浮动列,则必须使用与下方清除的div相似的内容来填充内容的高度。这不是最好的解决方案,但它很常见且效果很好。

<div id="main">
  <div style="float: left; width: 30%;"> ... </div>
  <div style="float: right; width: 60%;"> ... </div>
  <div style="clear: both; height: 0;"> &nbsp; </div>
</div>