html - flex 容器的绝对定位子容器不参与 flex 布局,如何使元素居中?

标签 html css twitter-bootstrap flexbox

如何使绝对定位的 flex 容器的子元素居中?

容器已经设置为 justify-content:center,但由于 child 是绝对定位的,这不会影响它,它也不会居中。

Logo (手头的问题是 Logo )被“绝对定位”以便能够应用 z-index,所以我可以让它可点击(在它会在几个层中丢失之前我不会'看不到它或无法单击它)。

我附上图片来解释我的观点(请多多包涵,因为我无法嵌入图片 - stackoverflow 因果关系不够)。相关代码如下。

这是我试过的:

  • 尝试使用 flex box 居中,直到我意识到这对绝对定位的子元素不起作用
  • 已使用,显示: block margin-left: auto; margin-right: auto;但这没有任何区别

如何在移动 View 中将 Logo 置于导航栏上方的中央?对我放轻松,我是初学者 :-)

Relevant HTML:

<header class="masthead__navigation mb-auto d-flex w-100 p-3 mx-auto flex-column">
      <div class="inner">
        <a class="inner-masthead" href="#"><img src="assets/images/logo.jpg" style="max-height:40px; width:auto;"></a>
        <nav class="nav nav-masthead justify-content-center">
          <a class="nav-link active" href="#">Home</a>
          <a class="nav-link" href="#">Blog</a>
          <a class="nav-link" href="#">Join Newsletter</a>
          <a class="nav-link" href="#">Contact</a>
        </nav>
      </div>
    </header>

Relevant CSS:

@media (min-width:768px) {

    .masthead__navigation {
                padding-right: 2.5rem !important;
                padding-left: 4rem !important
    }

    .nav-masthead .nav-link {
                color: #262626 !important;
              color: rgba(26, 26, 26, .5) !important;
    }

    .nav-masthead .nav-link:hover,
    .nav-masthead .nav-link:focus {
            border-bottom-color: rgba(26, 26, 26, .5) !important;
    }

    .nav-masthead .active {
              color: #000 !important;
              border-bottom-color: #000 !important;
    }
}

.masthead__navigation {
        margin-bottom: 2rem;
        padding-bottom: 0 !important;
        width: 100vw !important;
        position: absolute !important
}

.inner-masthead {
    margin-bottom: 0;
        position: absolute !important;
        display: block;
      margin-left: auto;
      margin-right: auto;
        z-index: 4 !important
}

.nav-masthead .nav-link {
          padding: .25rem 0;
          font-weight: 700;
            color: #999;
          color: rgba(255, 255, 255, .5);
          background-color: transparent;
          border-bottom: .25rem solid transparent;
            z-index: 4 !important
}

.nav-masthead .nav-link:hover,
.nav-masthead .nav-link:focus {
          border-bottom-color: rgba(255, 255, 255, .5);
}

.nav-masthead .nav-link + .nav-link {
          margin-left: 1rem;
}

.nav-masthead .active {
          color: #fff;
          border-bottom-color: #fff;
}

@media (min-width: 48em) {
    .inner-masthead {
        float: left;
    }
    .nav-masthead {
        float: right;
    }
}

最佳答案

For a positioned box (that is, one with any position other than static), the z-index property specifies: 1)The stack level of the box in the current stacking context. 2)Whether the box establishes a local stacking context.

根据 MDN - z-index , z-index 适用于每个位置不是静态的元素。也许您应该尝试使用 position:relative 或类似的方法,它不会将元素从其父项中弹出。

关于html - flex 容器的绝对定位子容器不参与 flex 布局,如何使元素居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51518779/

上一篇:css - 如何修复 Internet Explorer 9 和 10 忽略@font-face 和字体系列?

下一篇:css - 使滚动条 css 在网页中可见

相关文章:

html - Bootstrap 3 自定义 li item div 样式

html - 上传到服务器时,CSS 自定义不起作用

html - 仅在页面加载时使用 CSS3 关键帧动画?

javascript - Div 悬停指针

html - 用作行的 div 旁边的文本

angularjs - 文本输入框不接受来自 Angular UI Bootstrap 的模式输入

twitter-bootstrap - 将图像放在 DIV 的底部

javascript - 如何为 div 设置固定高度和宽度

javascript - 解析 .txt 文件以使用 jQuery 填充 html 选择

html - Yii2 Krajee Select2 插件格式化