[英]Match background-image scale-level in header over background-size:cover
I have a two block elements, the first one ( <section>
) being the size of the browser window and rendering a dynamically sized background image (using background-size: cover;
).我有两个块元素,第一个(
<section>
)是浏览器窗口的大小并呈现动态大小的背景图像(使用background-size: cover;
)。 The other one a fixed <header>
with constant height of 62px and a bigger z-index
.另一个是固定的
<header>
具有 62px 的恒定高度和更大的z-index
。
HTML body: HTML 正文:
<header></header>
<section></section>
CSS: CSS:
html, body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
header {
position: fixed;
z-index: 100;
height: 62px;
width: 100%;
background-image: url(image.jpg);
background-repeat: no-repeat;
background-position: top center;
background-size: cover;
}
section {
height: 100%;
width: 100%;
background-image: url(image.jpg);
background-repeat: no-repeat;
background-position: top center;
background-size: cover;
}
I want the background image of the <header>
to have a "scale-factor" equal to that of the <section>
, but only showing the constant height.我希望
<header>
的背景图像具有等于<section>
的“比例因子”,但只显示恒定高度。 That means it should show exactly the first 62 pixel rows that the <section>
element is rendering, and therefore be invisible to the user (when he is at vertical position 0/hasn't scrolled yet).这意味着它应该准确地显示
<section>
元素正在呈现的前 62 像素行,因此对用户不可见(当他处于垂直位置 0/尚未滚动时)。
Link to a fiddle: https://jsfiddle.net/f1nhum9u/1/链接到小提琴: https : //jsfiddle.net/f1nhum9u/1/
You can add the fixed
behavior for the bg header.您可以为 bg 标头添加
fixed
行为。 With background-attachment:带有背景附件:
This keyword means that the background is fixed with regard to the viewport.
这个关键字意味着背景相对于视口是固定的。 Even if an element has a scrolling mechanism, a 'fixed' background doesn't move with the element.
即使元素具有滚动机制,“固定”背景也不会随元素移动。
html, body { margin: 0; padding: 0; height: 100%; width: 100%; } header { position: fixed; z-index: 100; height: 62px; width: 100%; background-image: url(http://www.nasa.gov/images/content/696289main_O_Star_Binary.jpg); background-repeat: no-repeat; background-position: top center; background-size: cover; /*ADD THIS*/ background-attachment: fixed; } section { height: 100%; width: 100%; background-image: url(http://www.nasa.gov/images/content/696289main_O_Star_Binary.jpg); background-repeat: no-repeat; background-position: top center; background-size: cover; }
<header></header> <section></section> <br><br><br><br>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.