简体   繁体   English

将标题中的背景图像比例级别与背景大小匹配:cover

[英]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.

相关问题 具有background-size:cover的可拖动背景图像 - Draggable background-image with background-size:cover 根据内容固定图像尺寸比例,并带有背景尺寸封面 - Fixed image size scale according content with background-size cover 如何像背景大小的背景图像一样全屏拉伸图像:封面? - How stretching an image fullscreen like a background-image with background-size: cover? 如何由于背景尺寸(封面)而停止放大背景图像? - How do I stop background-image from zooming in because of background-size: cover? background-size:封面无法完全用背景图像填充div - background-size: cover is not working to completely fill div with background-image 使用“background-size : 100% 100%”时,DIV“background-image”没有在整个DIV上拉伸 - DIV "background-image" not stretched over entire DIV when using "background-size : 100% 100%" 没有基于px的宽度的background-image / background-size - background-image / background-size without a px based width 背景尺寸:封面未设置背景图片的高度 - Background-size: cover not setting height of background image 在背景图片之后放置文字(background-size:cover) - Position Text after Background Image (background-size:cover) Chrome动画gif背景图像不播放 - 背景大小 - Chrome animated gif background-image not playing - background-size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM