基本上我想在这里做什么设备: http://www.w3schools.com/cssref/tryit.asp?filename=trycss_background-position
让我的背景图像在 div 上有一个 id。
#wrapper {
height: 900px;
width: 900px;
border: solid black;
margin-left: auto;
margin-right: auto;
}
#idbackground {
background-image: url("http://i.imgur.com/7h8ejPJ.png");
width: 324px;
height: 250px;
background-repeat: no-repeat;
background-attachment: fixed;
background-position:center center;
}
<DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="wrapper">
<div id="idbackground"></div>
</div>
</body>
</html>
图像没有居中而是消失了..
最佳答案
您忘记了
后面的分号背景图片:url("http://i.imgur.com/7h8ejPJ.png")
还将 width
和 height
属性添加到图像 div 中,设置为 100%。
#wrapper {
height: 900px;
width: 900px;
border: solid black;
margin-left: auto;
margin-right: auto;
}
#idbackground {
background-image: url("http://i.imgur.com/7h8ejPJ.png"); /* add semicolon here*/
background-repeat: no-repeat;
background-attachment: fixed;
background-position:center center;
width:100%; /*add width*/
height:100%; /*add height*/
}
<DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="wrapper">
<div id="idbackground"></div>
</div>
</body>
</html>
问题是您设置了宽度和高度,并且图像被包裹在其中,因此图像被限制在这个边界内,因此您会看到图像的部分不可见,并且在滚动时它完全被隐藏,因为图像 div 向上滚动..
关于html - 使用 div Id 使用背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36523671/