css - 背景封面不适用于移动设备

标签 css mobile responsive-design background-image responsive-images

我正在尝试创建一个包含整页背景图片的网站。我已经让它在桌面版本上运行,但是当我将它推送到 github 并在我的手机上查看它时,背景图像只是顶部的长图像。我在我的 css 中使用了 background-size: cover。下面的屏幕截图。 我怎样才能让它在移动设备上占据整个空间?谢谢:)

桌面版: desktop version

手机版: mobile version

.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/

相关文章:

android - Android 上的 Firefox 浏览器在链接上以灰色突出显示

.htaccess - 带有 .htaccess 的移动重定向,用于 www.example.com 或 example.com

javascript - 在 Android 浏览器中禁用横向?

html - 难以将输入和带边框的文本居中

jquery - 使用 jQuery 或 CSS 保持图像比例

html - 调整移动设备背景图像的大小

css - 图库灯箱插件问题

html - 如何在导航栏上垂直居中菜单项?

iphone - 防止 iPhone 对 HTML 表单元素进行圆 Angular 处理

javascript - 尝试用 jQuery 制作一个简单的网格