html - CSS 适合 BG 图像?
问题描述
几天来......我一直在尝试以正确的方式修复/居中我的背景图像。我附上了一个例子来展示这个。
图 A:我需要我的背景图像全屏显示(自定义大小适合)。图 B:我还需要我的背景图像适合垂直分辨率,并且在我调整浏览器窗口等时将地平线居中。
我有谷歌到地狱然后回来......没有好的解决方案?是否有可能做到这一点?还是我对 css/html5 要求太多了
好的,这是我的代码:如果你想看看下面的图片... 在此处输入图片描述
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
body {
margin: 0px;
font-family: Arial;
font-size: 14px;
color: #999;
text-align: center;
background-image: url(bg.jpg);
background-repeat: no-repeat;
background-size: 100%;
background-color: #975d7f;
}
</style>
</head>
<body>
</body>
</html>
解决方案
不是 100% 确定您在寻找什么,但我认为这可能符合您的需求。如果您希望图像覆盖屏幕的整个高度,您需要将 html 和 body 标签的高度设置为 100%。
html {height:100%;}
body {
height: 100%;
margin: 0px;
font-family: Arial;
font-size: 14px;
color: #999;
text-align: center;
background: #975d7f url("bg.jpg") no-repeat top center;
background-size: cover;
}
另外,我使用了背景 css 属性的“速记”版本。
推荐阅读
- ios - 无法使用助手编辑器将表格单元 UI 连接到代码
- python-3.x - 在 R markdown 中渲染熊猫表
- c++ - g++ 是否错误地抛出 -Wnarrowing?
- amazon-web-services - 使用 AWS 的 Terraform 设置,一个账户用于多个环境
- swift - 单击行并让该行转到另一个视图控制器并将有关该行的一些数据存储在变量中?
- pure-react-carousel - 如何在纯反应轮播中的 CSS 中使可见幻灯片的数量响应
- php - 如何使用正则表达式在 php 中查找完全相同的序列?
- c++ - 与对所有人使用相同的 typedef 字符串相比,在 C 中指定不同的 typedef 字符串长度是否会减少内存使用?
- python - 在 Python 中模拟像 Sqlite3 这样的元组排序
- vue.js - 无法使用 v-pre 检测 vue 组件中的组件