保持html布局

时间:2015-11-16 18:16:15

标签: html css

今天我的问题是询问如何使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>

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

2 个答案:

答案 0 :(得分:1)

用这个替换你的页脚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>

答案 1 :(得分:1)

不确定您想要获得什么,无论您是否可以根据输出设备的宽高比设置特定规则:

...

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

依旧......

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