我正在开发一个可重复使用的组件,它有一个横幅,应该固定在其父组件的顶部.

我正在努力让横幅保持对其父母的响应,而不是将自己固定在整个应用程序的顶部.

我的基本应用 struct 如下:

.header {
  display: flex;
  -webkit-box-flex: 1;
  flex-grow: 1;
  margin: 0px 25px;
  height: 64px;
  background-color: blue;
  position: fixed;
  top: 0px;
  z-index: 999;
  width: 100%;
}

.content {
  width: inherit;
  box-sizing: border-box;
}

.banner {
  position: sticky;
  top: 0px;
  width: inherit;
  z-index: 100;
  box-sizing: border-box;
  background-color: red;
  width: 100%;
}

.form {
  padding-top: 15px;
  padding-left: 24px;
  padding-right: 24px;
  display: flex;
  flex-flow: wrap;
  width: 100%;
  height: 1000px;
  background-color: green;
  z-index: -9;
}
<div class="App">
  <div class="header"></div>
  <div class="content">
    <div class="banner">HEllo</div>
    <div class="form"></div>
  </div>
</div>

其中的content部分被抽象为自己的组件,所以我无法更改top:0

Sandbox

推荐答案

我修改了您的CSS代码并添加了 comments 来解释我包含的内容和原因.以下是一些有用的读物.每种类型的职位都值得熟悉.

.header {
  display: flex;
  -webkit-box-flex: 1;
  flex-grow: 1;
  margin: 0px 25px;
  height: 64px;
  background-color: blue;
  /* position: fixed; */ /* the issue with 'fixed' is that it doesn't interact with the rest of the page's elements */
  position: sticky; /* in contrast, 'sticky' will keep it at the top of the page, but only when scrolling downwards */
  top: 0px;
  z-index: 999;
  width: 100%;
}

.content {
  position: relative; /* for relative elements, you can position children with absolute alignment */
  width: inherit;
  box-sizing: border-box;
}

.banner {
  position: absolute; /* so instead of 'sticky', you can align it to the relative parent with 'absolute' */
  top: 0px;
  width: inherit;
  z-index: 100;
  box-sizing: border-box;
  background-color: red;
  width: 100%;
}

.form {
  padding-top: 15px;
  padding-left: 24px;
  padding-right: 24px;
  display: flex;
  flex-flow: wrap;
  width: 100%;
  height: 1000px;
  background-color: green;
  z-index: -9;
}
<div class="App">
  <div class="header"></div>
  <div class="content">
    <div class="banner">Hello</div>
    <div class="form"></div>
  </div>
</div>

不清楚您想要什么,因为您没有提供屏幕截图.如果目标是始终显示标题,然后显示标题下方的横幅,然后显示标题下方的内容,那么您只需要稍微调整顶部像素即可.

.header {
  display: flex;
  -webkit-box-flex: 1;
  flex-grow: 1;
  margin: 0px 25px;
  height: 64px;
  background-color: blue;
  /* position: fixed; */ /* the issue with 'fixed' is that it doesn't interact with the rest of the page's elements */
  position: sticky; /* in contrast, 'sticky' will keep it at the top of the page, but only when scrolling downwards */
  top: 0px;
  z-index: 999;
  width: 100%;
}

.content {
  width: inherit;
  box-sizing: border-box;
  background: yellow; /* highlighted it in yellow
                         so you can see that the .banner actually starts from the top of the .content,
                         it's just that the .form doesn't start from the top of the .content
                         because the .banner is there */
}

.banner {
  position: sticky;
  top: 64px; /* instead of 0, place it so that it starts below the height of the header. */
  width: inherit;
  z-index: 100;
  box-sizing: border-box;
  background-color: red;
  width: 100%;
}

.form {
  padding-top: 15px;
  padding-left: 24px;
  padding-right: 24px;
  display: flex;
  flex-flow: wrap;
  width: 100%;
  height: 1000px;
  background-color: green;
  z-index: -9;
}
<div class="App">
  <div class="header"></div>
  <div class="content">
    <div class="banner">Hello</div>
    <div class="form"></div>
  </div>
</div>

Css相关问答推荐

在 CSS 中跨列对齐文本基线

为什么align-content:start应用于单行项目?

如何使用 :has() 父 Select 器 Select 父元素的子元素,在父元素和子元素之间没有中间层的情况下?

Firefox未正确设置SVG的父div的宽度

是否有一个 CSS 可以根据字符的高度 for each 字符呈现可变高度?

SCSS - Lighten/Darken 不编译?

Mat table - 保留第一列和复选框

Angular 以Angular 获取当前聚焦的元素

Tailwind CSS 隐藏和可见

为什么同一个 CSS 类连续出现两次

react 样式的条件类名称设置

使用 node-sass 在压缩文件末尾添加 min.css 文件扩展名

Angular 导航栏问题

你如何调试可打印的 CSS?

如何将 bootstrap 列高设为 100% 行高?

如何使用 CSS 绘制复选标记/勾号?

如何根据容器大小设置字体大小?

使用 :focus 设置外部 div 的样式?

如何在页面上居中 twitter bootstrap 选项卡?

html元素(div)的全高,包括边框,填充和边距?