简体   繁体   English

背景图像定位问题

[英]Background image positioning problem

I have this as my CSS styling. 我将其作为CSS样式。 My #pg_wrap is not centering with the background image when the browser window is resized. 调整浏览器窗口大小时,我的#pg_wrap没有与背景图像居中。 The screen shot I have is when you reduce the browser window less than 1344px than the #pg_wrap width (the width doesn't have to be that, was just a test number). 我的屏幕截图是当您将浏览器窗口缩小为比#pg_wrap宽度小1344px时(宽度不必一定是,只是一个测试数字)。

The screen shot was taken with Firefox, which does it. 屏幕截图是使用Firefox拍摄的。 Chrome does not do this. Chrome不会这样做。 How do I get the background image to stay put? 如何使背景图片保持原样?

Chrome

Firefox 火狐浏览器

body
{
    text-align: center;
    background-image: url('p_bg_75.jpg');
    background-repeat: repeat-y;
    background-color: black;
    background-attachment:fixed
    margin: 0px;
    padding: 0px;

}
#pg_wrap
{
    margin-left: auto;
    margin-right: auto;
    width: 1344px;
    height: 1000px;
    border: 1px solid white;
    text-align: center;
}

The difference is because the browsers handle incorrect css in different ways. 区别在于浏览器以不同的方式处理错误的CSS。

Change 更改

background-attachment:fixed

to

background-attachment:fixed;

Did you forget to declare the background position? 您是否忘记了声明背景位置? Remember that text-align does not align the background. 请记住,text-align不对齐背景。 As a single declaration, it seems like you're after: 作为一个声明,似乎您在追求:

background: #000000 url('p_bg_75.jpg') repeat-y fixed top center;

I can't tell by your question whether you really do want the attachment to be 'fixed', but based on your screen caps, I doubt it. 我不能通过您的问题来告诉您您是否确实希望附件被“修复”,但是根据您的屏幕截图,我对此表示怀疑。 So you can probably ditch that from the declaration too. 因此,您也可以从声明中删除它。

as advised above, try: 如上所述,请尝试:

background-position: center;

and paste a snippet of the HTML so we can test in Safari/Chrome. 并粘贴HTML的摘要,以便我们可以在Safari / Chrome中进行测试。

Normally with a center aligned site you are best off with using 2 background images. 通常,在中心对齐的站点上,最好使用2张背景图像。 1 background image is placed on the BODY and another background is placed on the center aligned DIV. 一个背景图像放置在主体上,另一个背景放置在居中对齐的DIV上。

If the body has no margins like in your example and you make your browser smaller than 1344px you will not see the body background any more. 如果主体没有像您的示例中所示的边距,并且您使浏览器小于1344px,则您将不再看到主体背景。 To make sure that in cases the content is short, there are no strange effects you can give the DIV also a minimum height of 100%. 为确保内容简短,没有奇怪的效果,您可以将DIV的最小高度也设置为100%。

Maybe have a look at this site where this technique is also used: http://www.prgs.nl/ 也许看看这个也使用了这种技术的站点: http : //www.prgs.nl/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM