html - 如何在不裁剪的情况下将完整图像的高度调整为适合 chrome/任何浏览器的背景?
问题描述
我是css的初学者,想在背景上设置一个图像。我正在尝试将图像高度设置为适合屏幕而不裁剪图像。我只是想让滚动条消失。这是代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
.bgimage {
background-position: center center;
margin: 0px 0px 0px 0px;
background-image: url('Images/56216134.jpg');
background-size: 100%;
background-repeat: no-repeat;
width: 100%;
height: 1000px;
padding: 0px 0px 0px 0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="bgimage">
</div>
</form>
</body>
</html>
解决方案
您可以做的是将您的div
高度设置为您的 100% form
(如果您form
是 100% 的高度body
)。
然后你必须将你的background-image
size属性设置为“包含”:
background-size: contain;
关键字 contains 将调整背景图像的大小以确保它完全可见。
这是一个代码片段示例:
html, body, form{
width: 100%;
height: 100%;
}
.bgimage {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background-image: url('https://www.capebretonpost.com/media/photologue/photos/cache/yv-23052017-billcurry-1_large.jpg');
background-position: center;
background-repeat: no-repeat;
background-size: contain;
}
<body>
<form id="form1" runat="server">
<div class="bgimage">
</div>
</form>
</body>
如果你想让你的图像占据你的全宽,那么你可以使用background-size: cover;
如下:
html, body, form{
width: 100%;
height: 100%;
}
.bgimage {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background-image: url('https://www.capebretonpost.com/media/photologue/photos/cache/yv-23052017-billcurry-1_large.jpg');
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
<body>
<form id="form1" runat="server">
<div class="bgimage">
</div>
</form>
</body>
推荐阅读
- android - android sdk 28 中的 PayUmoney 集成在添加依赖项时发生错误,gradle 同步失败
- angular - 单页应用程序Angular 6中的组件路由
- c++ - 我可以使用多线程多次调用一个简单的函数吗?c++11
- mysql - innodb_table_locks 设置究竟做了什么?
- android - Activity 或 Fragment 的理想选择应该是什么?
- java - java序列化如何解决循环引用问题?
- python - 机器学习在一组数据上创建方程
- spring - 在 Webflux Spring 中使用 OAuth2 进行身份验证
- node.js - “MongoDB:未知的顶级运算符:$expr”
- python - 为什么 MS-Word 内置样式在使用 python-docx 时不起作用?