html - 如何显示整页背景图片?
问题描述
我正在为登录页面创建 HTML 设计。它工作得很好,但唯一的问题是:我无法在移动屏幕上显示背景图像。在大型桌面屏幕上,整个背景图像都可见,但在较小的屏幕上,背景图像仅显示一半。
请帮助我应该编辑什么以使背景图像始终适合屏幕,无论它有多大或多小?
登录.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
body, html {
height: 100%;
margin:0;
}
.background{
height: 100%;
background-image: url("7.jpg");
background-position: center;
background-repeat:no-repeat;
background-size:cover;
}
</style>
</head>
<body>
<div class="container-fluid background">
<br><br>
<h2 class="text-center" style="color:orange">Register</h2>
<div>
<form class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2" for="email" style="color:orange">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="email" placeholder="Enter Name" name="email" style="width:80%">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd" style="color:orange">Username:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Enter Username" name="pwd" style="width:80%">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd" style="color:orange">E-mail:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Enter E-mail" name="pwd" style="width:80%">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd" style="color:orange">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd" style="width:80%">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd" style="color:orange">Confirm Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Confirm Password" name="pwd" style="width:80%">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd" style="color:orange">Upload image:</label>
<div class="col-sm-10">
<input type="file" id="pwd" placeholder="Enter password" name="pwd" style="width:80%">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-danger">Sign up</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
解决方案
使用 Bootstrap 时,我永远不会弄乱容器(例如,background-image
在.container
类上放一个。我会在它的上方或下方创建一个全新的容器div
。此外,您需要确保<div class="background">
涵盖所有表单内容。如果没有't,那么这时候图像可能会发生一些奇怪的事情。
这解决了我的问题:
<div class="background">
<div class="container-fluid">
<!-- content goes here -->
</div>
</div>
(可选):我注意到.form-horizontal .form-group
Bootstrap CSS 将表单推送到图像之外。我通过添加这种样式来解决这个问题:
.form-horizontal .form-group {
margin-right: 0;
margin-left: 0;
}
推荐阅读
- tensorflow - 多台 GPU 机器上的 TensorFlow?
- java - Lombok的@NonNull的使用说明
- html - 在 Bootstrap 4 中使用 form-inline 时创建填充
- c++ - sizeof 运算符如何计算函数返回类型的大小?
- asp.net-mvc - 如何在 .net 核心中创建自定义会话包装类并访问控制器外部
- python - 如何计算召回率、精度和 f-measure?
- mp4 - 是否可以在 mp4 文件的一个轨道中存储多个视频段落,每个段落都有自己的参数?
- c++ - x86 asm 在 __declspec(naked) 函数中访问和编辑 stdcall 函数参数
- r - 避免在 .SD 越界索引期间创建 NA
- javascript - 在画布中裁剪图像