在html布局中保留位置



今天我的问题是,如何使html页面布局即使在任何不同比例的屏幕大小下也保持不变。例如,我正在15.6"16:9比例的屏幕大小上创建一个html页面,我想在更改屏幕大小时使其保持不变。

这里有一个例子:

<!DOCTYPE html>
<html>
<head>
<style>
#header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px;
}
#nav {
    line-height:30px;
    background-color:#eeeeee;
    height:300px;
    width:100px;
    float:left;
    padding:5px;          
}
#section {
    width:350px;
    float:left;
    padding:10px;        
}
#footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
   padding:5px;      
}
</style>
</head>
<body>
<div id="header">
<h1>City Gallery</h1>
</div>
<div id="nav">
London<br>
Paris<br>
Tokyo<br>
</div>
<div id="section">
<h2>London</h2>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</div>
<div id="footer">
</div>
</body>
</html>

所以现在我希望页脚保持在底部,所以是的,我该怎么做?

用这个替换页脚CSS

#footer {
    background-color: black;
    color: white;
    text-align: center;
    padding: 5px;
    bottom: 2px;
    position: fixed;
    width: 100%;
}

完整的代码应该是

<!DOCTYPE html>
<html>
<head>
<style>
#header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px;
}
#nav {
    line-height:30px;
    background-color:#eeeeee;
    height:300px;
    width:100px;
    float:left;
    padding:5px;          
}
#section {
    width:350px;
    float:left;
    padding:10px;        
}
#footer {
    background-color: black;
    color: white;
    text-align: center;
    padding: 5px;
    bottom: 2px;
    position: fixed;
    width: 100%;
}
</style>
</head>
<body>
<div id="header">
<h1>City Gallery</h1>
</div>
<div id="nav">
London<br>
Paris<br>
Tokyo<br>
</div>
<div id="section">
<h2>London</h2>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</div>
<div id="footer">
</div>
</body>
</html>

不确定你想要得到什么,除非你可以根据输出设备的纵横比设置特定的规则es:

...
@media screen and (device-aspect-ratio: 16/9) {
.foo {
...
     }
}
@media screen and (device-aspect-ratio: 4/3) {
.foo {
...
     }
}

等等…

还有很多其他方法可以实现这种转变,在我看来,媒体查询是最简单的。

最新更新