【问题标题】:Content div won't expand to 100% of its container内容 div 不会扩展到其容器的 100%
【发布时间】:2012-04-03 15:31:43
【问题描述】:

我有 2 个 div:#container 和 #main-content,它们下方是一个页脚,即使 #main-content 内的内容高度小于正文高度,页脚也需要看不见。
当我有很多内容时一切都很好,但是当我只有几行文本时,只有#container div 扩展,#main-content 没有。

我尝试为#main-content 使用 100% 高度,但它没有扩展到其父高度。

html:

<div id="container" class="cf">
    <div id="main-content" role="main" class="cf">

        <?php while ( have_posts() ) : the_post(); ?>

            <h2><?php wp_title(); ?></h2>
            <?php get_template_part( 'content', 'page' ); ?>
            <?php the_content(); ?>

        <?php endwhile; ?>
    </div><!-- #main-content -->
</div><!-- #container -->

css:

body { 
   width: auto; 
   height: 100%;
}

html {
   height: 100%;
}

#container {
   width: 1180px;
   margin: 0 auto;
   height: auto !important;
   min-height: 100%;
   height: 100%;
}

#main-content {
    background: white;
    width: 850px;
    height: 100%;
}

【问题讨论】:

  • 您必须将 min-height 设置为 main-content,因为默认情况下 div 不会扩展高度,div 是块级元素,默认情况下会扩展 100% 的宽度。
  • 我试过了,不行
  • 你能告诉我们你的工作代码吗?我是说网站
  • 您只是想获得 100% 高度的白色背景吗?
  • 我正在使用第二个 div 为主要内容获取白色背景,稍后我将在 #container 中添加一个没有白色背景的侧边栏

标签: css html height


【解决方案1】:

您是否尝试删除:

height: auto !important;

!important 删除你后面的 100% 样式。 必须为孩子 100% 的身高设置父母的身高。

【讨论】:

  • 如果我这样做 #container 不会展开
  • 奇怪,因为它应该可以工作:jsfiddle.net/NneAU。容器是 body 的孩子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2011-10-15
  • 2015-09-21
相关资源
最近更新 更多