我想居中一个背景图像。在这里没有使用 div,这是 CSS 样式:
body{
background-position:center;
background-image:url(../images/images2.jpg) no-repeat;
}
上面的CSS可以将图片平铺并居中,但是只有一半的图片可见,它似乎只是向上移动了。我想要做的是将图片居中。我是否可以调整图片以在21英寸屏幕上查看?
background-image: url(path-to-file/img.jpg);
background-repeat:no-repeat;
background-position: center center;
那应该可以行得通。
如果不行,为什么不使用带有图像的 div
并使用 z-index
将其作为背景呢?这比在 body 上使用背景图片更容易居中。
除此之外,尝试:
background-position: 0 100px;/*使用一个像素值使其居中*/
或者如果你将 body 的 min-height
设置为 100%,我认为你可以使用 50%。
body{
background-repeat:no-repeat;
background-position: center center;
background-image:url(../images/images2.jpg);
color:#FFF;
font-family:Arial, Helvetica, sans-serif;
min-height:100%;
}
min-height
吗? - Kyle我使用这段代码,它运行良好
html, body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
background: black url(back2.png) center center no-repeat;;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
-webkit-background-size
是特殊声明中存在的。 - BigBang1112我认为这个就是所需的内容:
body
{
background-image:url('smiley.gif');
background-repeat:no-repeat;
background-attachment:fixed;
background-position:center;
}
尝试
background-position: center center;
body
设置一个 min-height
为 100%
,这样它就可以延伸到整个屏幕。这可能会解决问题。 - Pekka就像这样:
background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
background-repeat:no-repeat;
background-position: center;
background-size: cover;
html{
height:100%
}
body{
background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
background-repeat:no-repeat;
background-position: center;
background-size: cover;
}
body{
background-position:center;
background-image:url(../images/images2.jpg) no-repeat;
}
body{
background:url(../images/images2.jpg) center center no-repeat;
}
或者
body
{
background-image: url(path-to-file/img.jpg);
background-repeat:no-repeat;
background-position: center center;
}
background-position: center center;
没有……对我有效
background-attachment: fixed;
使用居中对齐方式,将我的图像在x轴和y轴上都居中显示。
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
,这将解决你的问题。background-repeat:no-repeat;
background-position:center center;
在使用 html 4.01 'STRICT' doctype 时,无法垂直居中背景图片。
解决方法:
background-attachment: fixed;
应该解决了这个问题
(所以Alexander是正确的)
简单替换
background-image:url(../images/images2.jpg) no-repeat;
使用
background:url(../images/images2.jpg) center;