我正在尝试创建一个包含整页背景图片的网站。我已经让它在桌面版本上运行,但是当我将它推送到 github
并在我的手机上查看它时,背景图像只是顶部的长图像。我在我的 css
中使用了 background-size: cover
。下面的屏幕截图。
我怎样才能让它在移动设备上占据整个空间?谢谢:)
.background1
{
/* Location of the image */
background-image: url(images/background-photo.jpg);
/* Image is centered vertically and horizontally at all times */
background-position: center center;
/* Image doesn't repeat */
background-repeat: no-repeat;
/* Makes the image fixed in the viewpoint so that it doesn't move when
the content height is greater than the image height */
background-attachment: fixed;
/* This is what makes the background image
rescale based on itscontainer's size */
background-size: cover;
/* Pick a solid background color that will
be displayed while the background image is loading */
background-color:#464646;
}
Html如下
<head>
<script src="https:
//ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"</script>
<script
src="https://cdn.jsdelivr.net/lodash/4.11.2/lodash.min.js"></script>
</head>
<meta charset="utf-8">
<title>Color</title>
<link rel="stylesheet" href="style.css">
<link href="animate.css" rel="stylesheet">
</header>
<body id="bodyID" class="background1">
</body>
<script src="javascript.js"></script>
最佳答案
问题源于你的<body>
元素的高度不适合您的设备。你可以贴一个height: 100%
在 html, body
,但我认为更简单的方法是添加以下内容:
body {
height: 100vh;
}
这会将主体元素的高度设置为加载时视口(viewport)高度的 100%。我对此进行了测试,它解决了我的 Android 设备上的问题,并且不会在桌面上破坏它。
旁注:您可以按照 Google's instructions 使用 Chrome 检查器工具调试您的 Android 设备。 .
关于css - 背景封面不适用于移动设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854417/