0

我希望我的博客容器具有与整个页面分开的背景。但是,它以 wood.png 为背景,而不是 bg.png。我正在使用以下内容:

CSS

body {
 background: url('img/bg.png') repeat;
}
.above {
 background: url('img/wood.png') repeat;
 width: 100%;
}
#blog-header {
 background: url('img/wood.png') repeat; 
 height: 160px;
 width: 100%;
 text-align: center;
}
#blog-container {
 background: url('img/bg.png') repeat;
 margin: 0 auto;
 width: 960px;
}
#blog {
 float: left;
 margin-top: 80px;
 position: relative;
 width: 620px;
}

HTML/PHP

<body>
<div class="above">
 <div id="blog-header">
  <div class="logo">
   <a href="<?php bloginfo('url'); ?>"><img src="<?php echo get_template_directory_uri(); ?>/img/logo.png" alt="Urban Palate logo" id="logo" /></a>
   </div><!-- end logo -->
   <nav>
    <ul>
     <li><a href="/?page_id=7"><img src="<?php echo get_template_directory_uri(); ?>/img/about.png" alt="Urban Palate intro" /></a></li>
     <li><a href="/?page_id=12"><img src="<?php echo get_template_directory_uri(); ?>/img/portfolio.png" alt="Urban Palate portfolio" /></a></li>
     <li><a href="/?page_id=15"><img src="<?php echo get_template_directory_uri(); ?>/img/blog.png" alt="Urban Palate blog" /></a></li>
     <li><a href="/?page_id=10"><img src="<?php echo get_template_directory_uri(); ?>/img/contact.png" alt="Urban Palate contact" /></a></li>
    </ul>
   </nav>
   </div><!-- end blog-header -->
   <div id="blog-container">
    <div id="blog">
     <div id="post">
       //content
     </div><!-- end post -->
    </div><!-- end blog -->
   </div><!-- end blog-container -->

任何想法可能导致问题?

预计到达时间:JSFiddle

4

2 回答 2

1

如果元素包含浮动元素,则包装元素需要 aoverflow:auto或 aclear:both;来获得为其添加背景所需的高度。

在您的情况下,您可以添加overflow:auto或添加clear:both到,#blog-container或者您可以将其添加为单独的类,如下所示:

这里有一些不错的行可以用来向你的 CSS 添加一个 clearfix 声明,你可以将该类添加到其中包含浮动元素的任何元素:

/*
 * Clearfix: contain floats
 *
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    `contenteditable` attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that receive the `clearfix` class.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */

.clearfix:before,
.clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.clearfix:after {
    clear: both;
}

/*
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */

.clearfix {
    *zoom: 1;
}

clearfix 取自:Html5Boilerplate

这是一个关于 clearfix 差异的额外好问题:哪种 clearfix 方法最好(检查答案二)

于 2012-09-30T18:34:03.697 回答
0

您可能需要在身体上设置一些填充(或将 div 宽度设置为 95% 而不是 100%)。看起来 div 将占据整个屏幕的宽度。

于 2012-09-30T18:41:15.773 回答