html - 为什么我的 html-css backgrpung 图像重复?
问题描述
背景图片:
页面显示的内容:
我使用 colorzilla CSS 渐变生成器生成了图像文件,然后将 base64 转换为 SVG,因为我认为这可以解决它,但猜猜是什么......它没有。
<!DOCTYPE HTML>
<html>
<div>
<style>
div {
/* Location of the image */
background: url(https://tamescalytest.chadfreeman.repl.co/static/image.svg) center center cover no-repeat fixed;
background-color: #464646;
}
</style>
<head>
<title>Flask Template</title>
</head>
<body>
<form action="/user">
<input type="text" placeholder="username" name="username"></input>
<input type="submit" value="go to your page"></input>
</form>
</body>
</div>
</html>
解决方案
这背后的真正原因是您使用了无效的 CSS。来自CSSbackground
文档的引用:
如果简写声明中的属性之一是 bg-size 属性,则必须使用 /(斜杠)将其与 bg-position 属性隔开,例如 background:url(smiley.gif) 10px 20px/50px 50px; 将生成一个背景图像,位于距左侧 10 像素、距顶部 20 像素的位置,并且图像的大小将为 50 像素宽和 50 像素高。
bg-size
是cover
在你的情况下,并且bg-position
是center center
。cover
和之间没有斜线,center center
必须有斜线。
此外,您应该给背景 div 一个类或更好的 id 并在 CSS 选择器中使用它,因为否则,您的所有 div 都将具有该渐变作为背景,这可能不是您想要的。
遵循使用类的整理和工作代码:
<!DOCTYPE HTML>
<html>
<head>
<style>
div#formbackground {
/* Location of the image */
background: url(https://tamescalytest.chadfreeman.repl.co/static/image.svg) center center/cover no-repeat fixed;
background-color: #464646;
}
</style>
<title>Flask Template</title>
</head>
<body>
<div id="formbackground">
<form action="/user">
<input type="text" placeholder="username" name="username">
</br>
<input type="submit" value="go to your page"></input>
</form>
</div>
</body>
</html>
推荐阅读
- ssl - 根据地理位置减少 TLS 握手延迟的最佳方法?
- javascript - 如何检查 React 中安装的一些或一个元素?
- sql - 从表中获取多个变量并将它们存储在一些变量中
- bash - 如何使用bash获取文件列表中某些字符串的列表?
- linux - Git拉服务器消息:警告:url没有方案IP:Port,致命:无法解析凭证url IP:Port
- c - 将 UnsafeMutablePointer 转换为数组会给出错误的值
- react-native-android - 导航到堆栈导航内的未渲染屏幕
- mysql - LEFT JOIN 两个 MySql 表,而第二个表按 desc 排序并限制为 1
- javascript - 将一些值从 js 文件发送到 html 以便在 web 上显示
- chisel - 如何将 chisel3 的 bundle 扩展到 this.getWidth 的特定宽度?