我有一个主容器 div,我希望它从屏幕顶部精确地设置边距,例如10%
屏幕宽度。这样我就不会遇到屏幕尺寸不均匀等问题。
我已经找到了一个肮脏的解决方法,即在 div 之前放置1px by 1px
背景颜色的图像,然后将其设置为具有10%
屏幕宽度的样式。但这看起来很脏,不是吗?有没有更好的解决方案?
我有一个主容器 div,我希望它从屏幕顶部精确地设置边距,例如10%
屏幕宽度。这样我就不会遇到屏幕尺寸不均匀等问题。
我已经找到了一个肮脏的解决方法,即在 div 之前放置1px by 1px
背景颜色的图像,然后将其设置为具有10%
屏幕宽度的样式。但这看起来很脏,不是吗?有没有更好的解决方案?
与 Rubens 相同的解决方案,但不使用表格。我还放置了一些代码来处理您询问的上边距,但使用填充代替。
<html>
<head>
<title>...</title></head>
<body>
<div id="content">
Your whole page comes here...
</div>
</body>
</html>
* {
padding:0;
margin:0;
}
html, body {
height:100%;
}
body {
padding:10% 0 0;
}
#content {
width: 850px; // replace with your desired width
margin:0 auto;
}
我发现非常优雅的解决方案是将页面插入表格中,从正文之后开始,并在正文之前终止。
你会有这个:
<html>
<head><title>...</title></head>
<body>
<table id="content"><tr><td>
Your whole page comes here...
</td></tr></table>
</body>
</html>
现在只需使用样式确定页面的大小:
#content {
width: 850px;
margin-left: auto;
margin-right: auto;
}