2015-01-09 74 views
0

我已经设置html和身体宽度和高度为100%。为什么这不足以让我的背景图像显示(#home1,#home2,#home3)?这不是设置所有div将基于其高度的像素吗?我想让内容指定页脚和页眉的高度,即没有像素高度。布局应该是:logo ...全高(减去页眉和页脚)。 #photo-group ...全高(减去页眉和页脚)。类.pic-group应使每张照片的#photo-group的30%。目前,除非我在.pic-group类上创建像素高度,否则不显示图像。我想做什么,我错过了一些明显的东西?设置基于像素的高度将'侮辱'我的尝试流畅的布局。了解基于百分比的div和响应式设计

<?php 
session_start(); 
include('debug-code.php'); 
$thisPage = "home"; 
$title=""; 
$metaDesc=""; 
include('head.php');?> 
<style> 
html { 
height:100%; 
font-size: 100%; 
overflow-y: scroll; 
} 

body { 
font-size:16px; 
color:#434c50; 
font-family: arial, serif; 
background:white; 
width:100%; 
height:100%; 
} 

.clearfix:after { 
content: " "; /* Older browser do not support empty content */ 
visibility: hidden; 
display: block; 
clear: both; 
height:0; 
} 

#home-container { 
position:relative; 
margin:0 auto; 
width:70%; 
background:white; 
} 

#home-content { 
position:relative; 
} 

#home-content .content { 
width:100%; 
} 

div.logo.home { 
position:relative; 
float:left; 
width:60%; 
background:#fdf2fd; 
} 

div.logo.home img { 
width:60%; 
} 

#home-footer { 
position:relative; 
bottom:0; 
background:#d5d0d5;/*url('../images/footerBG.png')*/; 
color:white; 
border-top:1px solid #05577a; 
} 

#home-footer .content { 
margin:0 auto; 
width:70%; 
padding:1.5rem 0; 
} 

#photo-group { 
display:block; 
width:20%; 
height:100%; 
float:left; 
} 

.pic-group { 
height:30%; 
display:block; 
position:relative; 
} 

#home1 { 
background: blue url('../images/home1.png') no-repeat top left; 
background-size:100%; 
} 

#home2 { 
background:yellow url('../images/home2.png') no-repeat top left; 
background-size:100%; 
} 

#home3 { 
background:red url('../images/home3.png') no-repeat top left; 
background-size:100%; 
} 
</style> 
</head> 
<body> 
<?php include('home-header.php')?> 
<div id="home-container"><div class="content clearfix"> 
     <div id="home-content"><div class="content clearfix"> 
      <div class="logo home"><?php include('logo-home-image.htm');?></div> 
      <div id="photo-group"> 
      <div id="home1" class="pic-group"></div><div id="home2" class="pic-group"></div><div id="home3" class="pic-group"></div> 
      </div> 
      <?php include('home-menu.php');?> 
      <div id='mobile-menu'></div>    
     </div></div><!--end content--> 
    <?php include("disclaimer.htm") ?> 
</div></div><!--end container--> 
<?php include("home-footer.htm") ?> 
</body> 
</html> 

回答

0

你需要明确在一个div上设置height,使其填写其offsetparent的可用高度。在这种情况下,身体。 Div元素是块元素。这意味着默认情况下它们占据了offsetparent的可用宽度。高度默认设置为自动。这意味着当div内的内容被换行时,它会增加。

然而,你希望你的divs占用身体的所有空间。但身体不是#home divs的偏移量。

Offsetparents:

#home > #photo-group > #home-content > #home-container > body 

但在#home-container你没有设置高度和设置位置相对。所以这成为所有子div的base。将它们的高度设置为相对于此div的。因此在0上设置30%结果为0。当然明确设置300px作品。这让我们的#home的内容溢出其父。

+0

您的回复以多种方式帮助我。我已经在html中包含了我需要的图像,而不是作为背景图像。我是否正确地从您的答案中推断出背景图像不能被使用,除非高度被明确地设置在div链的某处?如果是这样,那么如何保持响应式设计的流体布局(%)? Java的? – Leslie 2015-01-10 18:25:12

+0

@莱斯利设置背景不会触发高度。将它与现实生活进行比较。如果您将海报放入小照片列表中,则只会显示海报的一部分,因为尺寸不匹配。流体设计不是一个问题与CSS。你只需要设置正确的offsetparent与高度。如果你给'#家容器'高度'100%'它应该给你结果。 – Mouser 2015-01-10 18:46:09

+0

谢谢你的解释。 – Leslie 2015-01-11 18:24:17