css - 使用位置固定时如何保持 block 级别?

标签 css css-position

当使用position: fixed 时,浏览器会移除布局的原始位置。如何预防?

在我的例子中,搜索应用栏的实际固定位置充当

enter image description here

期望充当。

enter image description here

模拟css代码。

.search-appbar-container {
  position: fixed;

  display: flex;
  flex-direction: row;
  align-items: center;

  width: 100%;
  height: 48px;
}

.lef-arrow-icon-container`
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  width: 60px;
`;

.search-textfiled-container`
  display: flex;
  flex: 1;
  flex-direction: row;
  align-items: center;
`;

.add-icon-container`
  display: flex;
  flex-direction: row;
  align-items: center;
`;

.search-history-container {
  display: flex;
  flex-direction: column;
  justify-content: center;

  width: 100%;
  margin-top: 5px;
}
.history-toggle-button`
  position: relative;

  width: 100%;
  height: 48px;

  background-color: transparent;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;

  border: none;
  outline: none;

  -webkit-appearance: none;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -webkit-tap-highlight-color: transparent;
`;

.history-icon-container`
  position: absolute;
  top: 11.5px;
  left: 0;

  width: 60px;
`;

.history-text-container`
  position: absolute;
  top: 11.5px;
  left: 60px;

  display: flex;
  flex-direction: row;
  align-items: center;
`;

模拟html代码。

<div>
  <div class="search-appbar-container">
    <div class="left-arrow-container>
      ...
    </div>
    <div class="search-textfield-container>
      ...
    </div>
    <div class="add-icon-container>
      ...
    </div>
  </div>
  <div class="search-history-div">
    <button class="history-toggle-button">
      <div class="history-icon-container>
        ...
      </div>
      <div class="history-text-container>
        ...
      </div>
    </button>
  </div>
</div>

编辑

检查我所有的代码,现在我可以找到发生这种情况的原因了。

如果.search-appbar-container的位置是static,history-icon-container和history-text-container的absolute位置与相关>history-toggle-button 的相对 位置,否则它们都是search-appear-container 的相关fixed 位置。

最佳答案

您可以将 margin-top 或 padding-top 设置为 .search-history-div 或设置 .search-history-div{position: fixed;top: 48px}

关于css - 使用位置固定时如何保持 block 级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51699690/

相关文章:

html - CSS3 链接样式没有被一致地拾取

html - Flexbox 布局 - 等高和 fittext

html - 使用相对定位时将元素的宽度设置为比父元素宽

javascript - 将 div 设置为固定在相对位置上的位置

html - SVG 路径图在 Firefox 中被截断

javascript - 将 div 缩略图切换脚本与图像缩放脚本相结合

jquery - 有很多定位嵌套元素时,如何给特定的div元素设置绝对位置

javascript - 表格单元格中的 JS 工具提示位置

CSS 子菜单定位在除 Firefox 之外的所有内容中

css - 网格将元素放在列中,而不是行中