无论页面大小如何,标题图像均居中
我的网页有一个始终保留在窗口顶部的标题图像。当窗口最大化时,标题图像居中,但在小窗口(或智能手机浏览器)中查看时,标题设置在窗口的左侧,右侧被裁剪。由于标题 div 比页面内容宽,因此当窗口小于标题而不是页面时,会出现水平滚动条。
我希望标题图像始终位于页面中央,如果窗口小于标题,则左侧和右侧都会被裁剪。此外,只有在窗口宽度小于页面宽度时才打开水平滚动条。
这张图片可能会更清楚地解释它:
http://postimage.org/image/49ne3k3o/
到目前为止我的代码:
CSS:
#headerwrap {
position: relative;
width: 998px;
height: 100px;
margin: 0 auto;
pointer-events: none;
}
#header{
position: fixed;
top: 0;
width: 998px;
margin: 0;
background: url('../images/top.png') no-repeat;
height:100px;
z-index: 1;
pointer-events: none;
}
和 HTML实现:(
<div id="headerwrap"\>
<div id="header">
</div>
</div>
页面宽 690 像素,标题为 998 像素)
任何帮助将不胜感激。
My webpage has a header image that always remains at the top of the window. When the window is maximised the header image is centred, but when viewing in a small window (or a smart-phone browser) the header is set to the left of the window, and the right side cropped. Because the header div is wider than the page content, the horizontal scroll-bar appears when the window is smaller than the header, not the page.
I would like the header image to be centered on the page at all times, with both left and right sides being cropped if the window is smaller than header. Also the horizontal scroll-bar to only be turned on if window width is less than page width.
This image might explain it more clearly:
http://postimage.org/image/49ne3k3o/
My code so far:
CSS:
#headerwrap {
position: relative;
width: 998px;
height: 100px;
margin: 0 auto;
pointer-events: none;
}
#header{
position: fixed;
top: 0;
width: 998px;
margin: 0;
background: url('../images/top.png') no-repeat;
height:100px;
z-index: 1;
pointer-events: none;
}
and HTML implementation:
<div id="headerwrap"\>
<div id="header">
</div>
</div>
(the page is 690px wide, the header is 998px)
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在具有
position:fixed
的标头元素上设置left: 50%
,然后应用该元素宽度一半的负边距以使其居中。这是一个快速小提琴: http://jsfiddle.net/blineberry/w8P2S/
你真的不知道为此需要一个包装器。
You need to set
left: 50%
on the header element that hasposition: fixed
, and then apply a negative margin of half that element's width to center it.Here's a quick fiddle: http://jsfiddle.net/blineberry/w8P2S/
You don't really need a wrapper for this.
正如布伦特所说,您确实需要负的左边距,但该边距会根据用户的浏览器分辨率而变化,如果用户调整浏览器的大小,则可能会出现问题。
为了计算出这个边距,我使用了一个简单的 jquery 方法,每次都像魅力一样工作:
必须在文档第一次准备好时调用此函数,然后每次用户调整浏览器大小时调用它。
You do need the negative margin-left, like Brent says, but that margin will vary depending on the user's browser resolution, and could be a problem if the user resizes the browser.
To figure out that margin I've used a simple jquery method that works like a charm everytime:
This function must be called on document ready for the first time, and then it gets called everytime the browser is resized by the user.
标题缩小以适应窗口是否可以接受?
另外,您需要这是一个纯 HTML / CSS 解决方案吗?
编辑:
这是我刚刚编写的一个小脚本,它将调整两个 div 的大小以适应浏览器窗口的大小。它不需要任何库(如 jQuery)。
或者,如果您希望两侧都有间隙,您可以执行以下操作:
所以,总之
,将包装器更改为
position:absolute; top: 0px
和内部 div 到position:absolute;顶部:0px;
希望这有帮助!
Would it be acceptable for the the header to shrink to fit the window?
Also, do you need this to be a pure HTML / CSS solution?
edit:
Here's a little script I just wrote that will resize both divs to the size of the browser window. It doesn't require any libraries (like jQuery).
Or if you want a gap at both sides you could do something like:
So ya, in summary:
Also, change the wrapper to
position: absolute; top: 0px
and the inner div toposition: absolute; top: 0px;
Hope this helps!
另一种选择是从 #header div 中删除 background 属性,并将其放入 #headerwrap 中,如下所示:
我还更改了一些样式,使所有内容都以跨浏览器为中心。为了防止水平滚动条显示,您可以将#headerwrap的overflow属性设置为hidden。
这应该可以解决你的两个问题。
Another option would be to remove the background property from the #header div and put it in the #headerwrap like so:
I also changed some of the style to make everything center cross-browser. And to keep the horizontal scrollbar from showing up, you set the #headerwrap's overflow property to hidden.
That should solve both your issues.