【问题标题】:blog containter not showing proper background博客容器没有显示正确的背景
【发布时间】:2012-09-30 18:26:53
【问题描述】:

我希望我的博客容器具有与整个页面不同的背景。但是,它以 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

【问题讨论】:

  • @AdamPlocher 抱歉,已编辑原始问题。

标签: php css html containers


【解决方案1】:

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

在您的情况下,您可以将overflow:autoclear: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 差异的好问题:which method of clearfix is best(检查答案二)

【讨论】:

  • 真的不需要clear:bothoverflow:auto 足以使容器扩展以包含它的浮动子项。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 2021-07-12
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2023-02-18
    • 1970-01-01
    相关资源
    最近更新 更多