javascript - 渐变不使用“背景附件:固定!重要;”?什么是替代方案?
问题描述
作为 web 开发的新手,我尝试了一个不错的登录。我无法正确定位元素,我被迫使用“背景附件:固定!重要;”。
有没有其他方法可以在不使用 ``` background-attachment: fixed !important 的情况下获得相同的结果{下面给出的图片}
这是我的代码:
body {
margin: 0;
padding: 0;
font-family: sans-serif;
height: 100%;
background: linear-gradient( red, green);
background-attachment: fixed !important;
}
.box {
width: 300px;
padding: 40px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #191919;
text-align: center;
}
.box h1 {
color: white;
text-transform: uppercase;
font-weight: 500;
}
.box input[type="text"],
.box input[type="password"] {
border: 0;
background: none;
display: block;
margin: 20px auto;
text-align: center;
border: 2px solid #3498db;
padding: 14px 10px;
width: 200px;
outline: none;
color: white;
border-radius: 24px;
transition: 0.25s;
}
.box input[type="text"]:focus,
.box input[type="password"]:focus {
width: 280px;
border-color: #2ecc71;
}
.box input[type="submit"] {
border: 0;
background: none;
display: block;
margin: 20px auto;
text-align: center;
border: 2px solid #3498db;
padding: 14px 40px;
width: 200px;
outline: none;
color: white;
border-radius: 24px;
transition: 0.25s;
cursor: pointer;
}
.box input[type="submit"]:hover {
background: #2ecc71;
}
<form class="box" action="index.html" method="POST">
<h1>Login</h1>
<input type="text" name="" placeholder="Username">
<input type="password" name="" placeholder="password">
<input type="submit" name="" value="Login">
</form>
我在谷歌上搜索了很多来为这个简单的问题找到一个很好的解决方案,但是变得更加困惑,所以我想要一些有经验的人对此的建议,以及作为初学者应该做和不应该做的事情。
{ 对于答案,如果您能提供整个代码并解释为什么选择特定样式以及它如何解决一般问题,我将不胜感激(我读到使用“背景附件:固定!重要;”可能会造成麻烦) } 谢谢。
------------------------ 编辑后 ------------- ---------
这里的问题是,没有“背景附件:固定!重要;”背景渐变不起作用。如果我将位置作为“box”元素的相对位置,则结果与我想要的不同。
所以,我希望位置正确{根据图像},有背景渐变,没有 background-attachment: fixed !important;
解决方案
您可以使用 javascript 设置背景高度等于您的滚动。
推荐阅读
- reactjs - 尝试导入错误:“OBJLoader”未从“三”导出(导入为“三”)
- apache-nifi - 一个处理器中的多个依赖创建语句
- npm-install - Npm install 动作超级慢:finalize
- java - XSLT:如何在同一行中制作 2 个零件?
- amazon-dynamodb - 如何在 dynamodb 查询中按时间戳排序
- python - 如何在 sphinx 中渲染 readme.md,其中包含一些图像的相对路径
- google-play - Play 管理中心无法点击创建新发布按钮
- c++ - 在不进行内存复制的情况下,在 C++ 中获取一定范围的内存
- ios - 快速调整日期选择器和 TextView 的大小有什么问题
- regex - 在多行中提取两个字符串之间的数字