CSS:无法对齐两个flex元素

时间:2017-01-28 02:43:07

标签: html css html5 css3 flexbox

我有一个带背景的弹性容器,它包含一些文本,包括垂直和水平居中对齐的内容(页面标题)和绝对对齐的内容(面包屑)。

当我拥有标题时,一切看起来都很棒,我显然有些东西搞乱了面包屑类。

我希望面包屑在容器内对齐,距离顶部25px(参见示例):

知道哪里出错了?

#site-wrapper {
  background: #ececec;
}
.site-wrapper-inner {
  overflow: hidden;
  position: relative;
  background: #fff;
}
.site-wrapper-inner.contained {
  max-width: 1500px;
}
.container {
  margin: 0 auto;
  width: 100%;	
}
header .container, 
main .container {
  position: relative;
  padding: 0 30px;
  max-width: 1118px;
}
header.hero {
  position: relative;
  clear: both;
  overflow: hidden;
  height: 340px;
  margin-bottom: 50px;
  display:flex;
  align-items:center;
}
.container.hero-center {
  text-align: center;
  max-width: 560px;
}
.breadcrumbs {
  color: rgba(255,255,255,0.7);
  font-size: 0.8em;
  align-self: flex-start;
  text-align: right;
  position: absolute;
  top: 25px;
  right: 0;
}
.breadcrumb-bg {
  padding: 0.2em 1em;
  background: rgba(19,71,91,0.7);
  display: inline-block;
  border-radius: 0.25em;
}
<div id="site-wrapper">
  <div class="site-wrapper-inner container contained">
    <main id="main">
      <section>
        <header class="hero" style="background:#ccc;">
          <div class="breadcrumbs container"><div class="breadcrumb-bg">Breadcrumb</div></div>
          <div class="hero-center container">
            <h1>Center Aligned Content</h1>
          </div>
        </header>	
      </section>
    </main>
  </div>
</div>

<h1>Without Breadcrumb</h1>

<div id="site-wrapper">
  <div class="site-wrapper-inner container contained">
    <main id="main">
      <section>
        <header class="hero" style="background:#ccc;">
          <div class="hero-center container">
            <h1>Center Aligned Content</h1>
          </div>
        </header>	
      </section>
    </main>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

问题在某种程度上是你的立场:绝对是被其他东西覆盖的。只需设置一个!对你的面包类很重要,这样就可以得到它。

找到它,你的痕迹中的容器类会覆盖它,你可以将!important设置为你的痕迹符号类的position属性,或者删除你的痕迹导体元素的容器类。

答案 1 :(得分:1)

您正在使用的绝对位置被此规则中的position: relative覆盖 - header .container, main .container

使用更多特定的样式覆盖它,并且对齐工作正常:

header .breadcrumbs.container {
  position: absolute;
  right: 0;
  left: 0;
  width: auto;
}

见下面的演示:

#site-wrapper {
  background: #ececec;
}
.site-wrapper-inner {
  overflow: hidden;
  position: relative;
  background: #fff;
}
.site-wrapper-inner.contained {
  max-width: 1500px;
}
.container {
  margin: 0 auto;
  width: 100%;	
}
header .container, 
main .container {
  position: relative;
  padding: 0 30px;
  max-width: 1118px;
}
/*ADDED THIS*/
header .breadcrumbs.container {
  position: absolute;
  right: 0;
  left: 0;
  width: auto;
}
/**/
header.hero {
  position: relative;
  clear: both;
  overflow: hidden;
  height: 340px;
  margin-bottom: 50px;
  display:flex;
  align-items:center;
}
.container.hero-center {
  text-align: center;
  max-width: 560px;
}
.breadcrumbs {
  color: rgba(255,255,255,0.7);
  font-size: 0.8em;
  align-self: flex-start;
  text-align: right;
  position: absolute;
  top: 25px;
  right: 0;
}
.breadcrumb-bg {
  padding: 0.2em 1em;
  background: rgba(19,71,91,0.7);
  display: inline-block;
  border-radius: 0.25em;
}
<div id="site-wrapper">
  <div class="site-wrapper-inner container contained">
    <main id="main">
      <section>
        <header class="hero" style="background:#ccc;">
          <div class="breadcrumbs container"><div class="breadcrumb-bg">Breadcrumb</div></div>
          <div class="hero-center container">
            <h1>Center Aligned Content</h1>
          </div>
        </header>	
      </section>
    </main>
  </div>
</div>

<h1>Without Breadcrumb</h1>

<div id="site-wrapper">
  <div class="site-wrapper-inner container contained">
    <main id="main">
      <section>
        <header class="hero" style="background:#ccc;">
          <div class="hero-center container">
            <h1>Center Aligned Content</h1>
          </div>
        </header>	
      </section>
    </main>
  </div>
</div>