ajax应用程序中css页面布局的问题

时间:2009-04-24 11:08:56

标签: html css ajax

我有一个css样式,当应用于静态html页面时效果很好。

问题是,内容是动态加载到第3层,然后是第2层。

我真的不了解一致性。有时这些层相距很远,有时它们相互啮合。

为什么会发生这种情况,如何将layer3保持在静态位置?

层相距很远的例子:

<html><head>
<title>Test page</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<div id="Layer0">
<div id="Layer1" class="Layer1">
<h3 align="left">A menu</h3>
<div align="left">
<ul class="BLUE">
<li><a href="#">item 1</a></li>
<li><a href="#">item 2</a></li>
</ul>
</div>
</div>
<div id="Layer2">
<div id="leftlayer" class="leftlayer">
<form name="searchForm">
An example form: 
<input name="search" type="text">
<br>
<input name="Submit" value="Update" onclick="searchUserInfo('parallelimport');return false" type="submit">
</form>
</div></div>
<div id="Layer3"><h1> Why is the layer so far away?</h1>
</div>
</div>
</div>
</body></html>

合并图层的示例

<html><head>
<title>Test page</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<div id="Layer0">
<div id="Layer1" class="Layer1">
<h3 align="left">A menu</h3>
<div align="left">
<ul class="BLUE">
<li><a href="#">item 1</a></li>
<li><a href="#">item 2</a></li>
</ul>
</div>
</div>
<div id="Layer2">Content goes here</div>
<div id="Layer3">
<h1>Why is the layer up here now?</h1>
<table width="100%" border="0">
<tbody>
<tr>
<td width="15%">This wrecks</td>
<td width="10%">The very</td>
<td width="65%">Layout of the page</td>
</tr>
</table>
</div>
</div>
</div>
</body></html>

我想要实现的一个例子:

<html><head>
<title>Test page</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<div id="Layer0">
<div id="Layer1" class="Layer1">
<h3 align="left">A menu</h3>
<div align="left">
<ul class="BLUE">
<li><a href="#">item 1</a></li>
<li><a href="#">item 2</a></li>
</ul>
</div>
</div>
<div id="Layer2">
<h1>I would like information to be here</h1>
<div id="Layer3">
<h1>And then the table to be below the above info</h1>
<table width="85%" border="0">
<tbody>
<tr>
<td width="15%"><strong>This is</strong></td>
<td width="20%"><strong>much better</strong></td>
</tr>
</tbody>
</table>
<h1>I would like the Layer 3 to always be in a constant position</h1>
<h1>Does this mean I should have it outside of Layer2?</h1>
</div>
</div>
</div>
</body></html>

我的CSS:

#Layer0 {
  width: 100%;
  height: 100%;
}

body{
margin:10px 10px 0px 10px;
padding:0px;
color:#999999;
font-family:"Trebuchet MS",arial,sans-serif;
font-size:70.5%;

}

#Layer2 {
background:#fff;
color:#000;
voice-family: "\"}\"";
voice-family: inherit;
padding:20px;

}

#Layer2 p {color:#888;}

#Layer2 a, a:link { color:#006633; text-decoration: none;}

#Layer2 a:hover, a:active{ color:#FF6666; text-decoration: none;}

html>body #Layer2 {
}

p,h1,pre {
margin:0px 10px 10px 10px;
font:Arial, Helvetica, sans-serif;
font-size:12px;
line-height: 1.6em;
text-align:justify;
text-decoration:none;
}

h1 {
font-size:2.5em;
text-align: center;
color:#ccc;
padding-top:15px;

}

h3 {
font-size:14px;
color:#999;

}

.leftlayer {
  float: left;
  left: 20%;
  width: 20%;
  height: 100%;
  margin-right: 10%;
}
.leftlayer strong {
  text-align: left;
}
.leftlayer2 {
  float: left;
  width: 20%;
  height: 100%;
  margin-left: 2%;
}
#rightlayer {
  float: left;
}
#Layer3 {
  float: bottom;
}

1 个答案:

答案 0 :(得分:2)

当您遇到棘手的CSS问题时,为各种布局元素添加边框确实可以帮助调试。例如,如果您将以下内容添加到.leftlayer CSS类中,您将看到您发布的第一个示例有什么问题:

 border: 1px dashed #ccc;

你会注意到你的表单周围的div = class =“leftlayer”正在向右推送Layer3。发生这种情况是因为leftlayer div出现在HTML流中的Layer3 div之前。为了获得更好的结果,将Layer3移到.leftlayer上方,如下所示:

<div id="Layer3">
    <h1> Why is the layer so far away?</h1>
</div>
<div id="Layer2">
    <div id="leftlayer" class="leftlayer">
        <form name="searchForm">
            An example form: 
            <input name="search" type="text">
            <br>
            <input name="Submit" value="Update" onclick="searchUserInfo('parallelimport');return false" type="submit">
        </form>
    </div>
</div>

我不确定这是否是您需要实现的目标,但这是一个开始。

Floats可以真正破坏您的布局,因此有助于彻底了解盒子模型。我发现这是一篇很好的参考文章:http://www.brainjar.com/css/positioning/default.asp