html - 使用 div Id 使用背景图像

标签 html css

基本上我想在这里做什么设备: 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")

还将 widthheight 属性添加到图像 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/

相关文章:

html - iPad 方向问题的元标记

javascript - 阻止位置 :relative from influencing the width of a child

css - 最大宽度调整以适应文本?

jquery - 使用 jQuery 对齐嵌套表头

javascript - 导航栏无法点击

javascript - (TS)HTMLInputElement 类型上不存在属性行

javascript - 如何清除不透明的附加div高度?

jquery - 使用函数作为参数的包装方法

css - 将文本包装在内联 block 的 div 中

html - 为进度条的 50% 着色