使 div 高度适合背景图像高度(具有视差效果)
Fit div height to background-image height (with parallax effect)
我检测到 100% 宽度视差背景图像存在问题。我必须使用 div 高度的固定值,因为它是空的。
问题是:当视口狭窄时背景图像下空 space。
问:如何创建响应式 div 高度等于背景图像高度。
我目前的搜索和尝试:
找到:How to get div height to auto-adjust to background size?
尝试了两种最流行的方法都没有效果,
// tried, but didn't work
// <div class="parallax">
// <img src="https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
// </div>
// img{
// visibility: hidden;
// max-width: 100%;
// }
//second method didn;t work too
// .parallax{
// background-image: url("https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
// background-attachment: fixed;
// background-repeat: no-repeat;
// background-size: 100% auto;
// padding-top: 60%;
// height: 0;
// }
html, body{
width: 100%;
margin: 0;
padding: 0;
}
.empty-space-20{
height: 20vh;
background-color: lime;
}
.parallax{
background-image: url("https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
background-attachment: fixed;
background-repeat: no-repeat;
background-size: 100% auto;
height: 40vh;
}
.empty-space{
height: 1000px;
background-color: coral;
}
<div class="empty-space-20">some empty space</div>
<div class="parallax"></div>
<div class="empty-space">some empty space</div>
使用 vw
而不是 vh
以获得更好的响应并避免图像下方的 space。图像的高度在变化以保持其比例,因此您对 div 执行相同的操作。您还可以将图像的位置调整到 div.
内部
html, body{
width: 100%;
margin: 0;
padding: 0;
}
.empty-space-20{
height: 20vh;
background-color: lime;
}
.parallax{
background-image: url("https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
background-attachment: fixed;
background-repeat: no-repeat;
background-size: 100% auto;
background-position: 0 20vh;
height: 50vw;
}
.empty-space{
height: 1000px;
background-color: coral;
}
<div class="empty-space-20">some empty space</div>
<div class="parallax"></div>
<div class="empty-space">some empty space</div>
如果您不想 space 滚动,请像下面那样降低高度并保持背景的默认位置:
html, body{
width: 100%;
margin: 0;
padding: 0;
}
.empty-space-20{
height: 20vh;
background-color: lime;
}
.parallax{
background-image: url("https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
background-attachment: fixed;
background-repeat: no-repeat;
background-size: 100% auto;
height: calc(50vw - 20vh);
}
.empty-space{
height: 1000px;
background-color: coral;
}
<div class="empty-space-20">some empty space</div>
<div class="parallax"></div>
<div class="empty-space">some empty space</div>
我检测到 100% 宽度视差背景图像存在问题。我必须使用 div 高度的固定值,因为它是空的。
问题是:当视口狭窄时背景图像下空 space。 问:如何创建响应式 div 高度等于背景图像高度。
我目前的搜索和尝试:
找到:How to get div height to auto-adjust to background size?
尝试了两种最流行的方法都没有效果,
// tried, but didn't work
// <div class="parallax">
// <img src="https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260">
// </div>
// img{
// visibility: hidden;
// max-width: 100%;
// }
//second method didn;t work too
// .parallax{
// background-image: url("https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
// background-attachment: fixed;
// background-repeat: no-repeat;
// background-size: 100% auto;
// padding-top: 60%;
// height: 0;
// }
html, body{
width: 100%;
margin: 0;
padding: 0;
}
.empty-space-20{
height: 20vh;
background-color: lime;
}
.parallax{
background-image: url("https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
background-attachment: fixed;
background-repeat: no-repeat;
background-size: 100% auto;
height: 40vh;
}
.empty-space{
height: 1000px;
background-color: coral;
}
<div class="empty-space-20">some empty space</div>
<div class="parallax"></div>
<div class="empty-space">some empty space</div>
使用 vw
而不是 vh
以获得更好的响应并避免图像下方的 space。图像的高度在变化以保持其比例,因此您对 div 执行相同的操作。您还可以将图像的位置调整到 div.
html, body{
width: 100%;
margin: 0;
padding: 0;
}
.empty-space-20{
height: 20vh;
background-color: lime;
}
.parallax{
background-image: url("https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
background-attachment: fixed;
background-repeat: no-repeat;
background-size: 100% auto;
background-position: 0 20vh;
height: 50vw;
}
.empty-space{
height: 1000px;
background-color: coral;
}
<div class="empty-space-20">some empty space</div>
<div class="parallax"></div>
<div class="empty-space">some empty space</div>
如果您不想 space 滚动,请像下面那样降低高度并保持背景的默认位置:
html, body{
width: 100%;
margin: 0;
padding: 0;
}
.empty-space-20{
height: 20vh;
background-color: lime;
}
.parallax{
background-image: url("https://images.pexels.com/photos/574205/pexels-photo-574205.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
background-attachment: fixed;
background-repeat: no-repeat;
background-size: 100% auto;
height: calc(50vw - 20vh);
}
.empty-space{
height: 1000px;
background-color: coral;
}
<div class="empty-space-20">some empty space</div>
<div class="parallax"></div>
<div class="empty-space">some empty space</div>