html - &lt;header&gt; 文本覆盖 <figure>

标签 html css

<分区>

这是我的代码。如何在不使用 position: absolute 的情况下使红色背景出现在图形上方?

谢谢。

article:nth-child(2) { margin-bottom: 1.5em; }

article figure { margin: 0; }

article header {
    background-color: red;
    padding: 2em 1.5em;
    margin: -5em 0 0;
    max-width: 72%;
    /*position: absolute;*/
    -webkit-box-sizing: content-box;
       -moz-box-sizing: content-box;
            box-sizing: content-box;
}

article p {
    font-size:1.4em;
    margin: 20px 0 0;
}

article p.author { font-size:1.6em; }
<article>
  <figure>
    <img src="http://lorempixel.com/600/337">
  </figure>
  <header>
    <h1>This is a feature post with a three line title</h1>
    <p class="author"><em>By <a href="#">John Doe</a></em></p>
    <p>
      <a href="#">Continue</a>
      <span>11 min read</span>
    </p>
  </header>
</article>

最佳答案

您不需要 position:absolute..您需要 position:relative 来“重置”已被负边距打乱的堆叠顺序。

article header {
  background-color: rgba(255,0,0,0.5);
  padding: 2em 1.5em;
  margin: -5em 0 0;
  max-width: 72%;
  position: relative; /* here */
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
}

article:nth-child(2) {
  margin-bottom: 1.5em;
}
article figure {
  margin: 0;
}
article header {
  background-color: rgba(255,0,0,0.5);
  padding: 2em 1.5em;
  margin: -5em 0 0;
  max-width: 72%;
  position: relative;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
}
article p {
  font-size: 1.4em;
  margin: 20px 0 0;
}
article p.author {
  font-size: 1.6em;
}
<article>
  <figure>
    <img src="http://lorempixel.com/600/337">
  </figure>
  <header>
    <h1>This is a feature post with a three line title</h1>
    <p class="author"><em>By <a href="#">John Doe</a></em>
    </p>
    <p>
      <a href="#">Continue</a>
      <span>11 min read</span>
    </p>
  </header>
</article>

关于html - &lt;header&gt; 文本覆盖 <figure>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30219632/

上一篇:css - 自定义页面类未显示在代码中

下一篇:javascript - 奇怪的形状 Pace.js

相关文章:

jquery - 建表时使用 if 语句

html - 如何使图像和容器具有相同的高度

jquery - 可滚动面板捕捉到滚动上的元素

html - 包含图像和文本的子 div 有高度,但父 div 没有

html - 字体选择器样式 css 类在 IE 中不起作用

javascript - 在下拉过渡中包含 CSS 三 Angular 形

javascript - 链接打开新窗口并刷新当前窗口

javascript - 每行内都有一个嵌套表的数据表

javascript - 无法在一行中对齐单选按钮及其标签

html - 单击纯 css 关闭下拉菜单