css - 如何制作填充 Gatsby 屏幕的背景图像?
问题描述
我正在使用 Gatsby,我正在尝试编辑我的 CSS 来给自己一个背景图像,它会填满屏幕,然后在下面我只想要一个页脚。这没有页脚,但它是我想要的一个很好的例子。
我有
body, html, #gatsby-focus-wrapper, #__gatsby {
height: 100%;
}
.landingDiv {
background-image: url("https://images.unsplash.com/photo-1493663284031-b7e3aefcae8e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80");
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
但是,背景不会扩展到 100%。我已尝试将父容器设置为 100%,以便扩展此背景。这就是它现在的样子。
这是此代码的代码笔 - https://codepen.io/norogoth/pen/zYwNqWX(下午 3:30 更新)
解决方案
如果要将其用作背景图像,则需要使其容器更宽更高,以使其适应屏幕的最大宽度和高度。使用相对单位可能对您有用。就像是:
.landingDiv {
background-image: url("https://images.unsplash.com/photo-1493663284031-b7e3aefcae8e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80");
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
/* making it 100% of the total width and height */
height: 100vh;
width: 100vw;
}
vh
分别vw
代表视口高度和视口宽度。
推荐阅读
- microsoft-graph-api - 添加日历事件,然后检索并更新该事件
- html - 跳转到表格行并在 html 中突出显示它?
- javascript - 获取 React Native 中有多少视图是可见的?
- python - Pytest:如何使用从夹具返回的列表对测试进行参数化?
- sql - offsetDateTime 非法ArgumentException nhibernate
- javascript - 在循环内为 HTML 制作换行符 For PHP
- python - 实现接收数组并将每个元素乘以给定 int 的函数
- python - 访问 argparse 的类属性文档字符串
- java - 如何设置意图以便我可以将数据发送到第二个活动并从那里发送到第三个活动?
- javascript - 尝试使用离子应用程序登录到 Azure AD 时出现 CORS 问题