html - 为什么对象边距是基于绝对父级而不是直接相对父级应用的

标签 html css

标题有点难=>

我有以下代码

==> 问题 <== https://codepen.io/anon/pen/WVpEGQ?editors=1100

==> 好的<== https://codepen.io/crocsx-the-styleful/pen/EqWvRR

基本上,我希望 subcontent-header 中包含的元素的边距/填充等相对地应用于 subcontent-header div 这个 div 有一个 position:relative,但它应用在 div 之外,而是将我的 subcontent-header 向下推

/* -- MAIN -- */
    body, html {
        background: @body-background-color url(/assets/images/bg.jpg) no-repeat center 0;
        background-size: contain;
        color:@body-color;
        font-size: 0.9rem;
        height: 100%;
        max-height: 100%;
        font-family: 'Helvetica Neue','Helvetica','Arial','Hiragino Sans','ヒラギノ角ゴシック',YuGothic,'Yu Gothic','メイリオ', Meiryo,'MS Pゴシック','MS PGothic';
        // font-family: @font-name !important;
        margin:0;
        padding:0;
        position:relative;
    	  overflow: auto;
    }
    
    .header {
        position: relative;
        background:green;
        width: 100%;
        height: 40px;
    }
    
    .content {
        width: 100%;
        min-height: calc(100% - 60px) !important;
        position: relative;
        display: flex;
        flex-direction: column;
      background: red;
    }
    
    .footer{
        width: 100%;
        height: 20px;
        position: relative;
        background: blue;
    }
    
    .subcontent{
      position:absolute;
      width: 100%;
      height: 100%;
      background:rgba(155,155,155,0.5)
    }
    
    .subcontent-header{
      position: relative;
      width: 100%;
      height: 30%;
      background:rgba(155,0,0,0.5)
    }
    
    .subcontent-content{
       position: relative;
      width: 100%;
      height: 70%;
      background:rgba(0,0,155,0.5) 
    }
<div class="header"></div>
    <div class="content">
      <div class="subcontent">
        <div class="subcontent-header">
          <h1>title</h1>
        </div>
        <div class="subcontent-content"></div>
      </div>
    </div>
    <div class="footer">
 </div>

最佳答案

原因是你没有将h1的位置设置为absolute。

尝试将它添加到您的 CSS 中

.subcontent-header h1 {
  position: absolute;
}

下面的示例工作代码

/* -- MAIN -- */
    body, html {
        background: @body-background-color url(/assets/images/bg.jpg) no-repeat center 0;
        background-size: contain;
        color:@body-color;
        font-size: 0.9rem;
        height: 100%;
        max-height: 100%;
        font-family: 'Helvetica Neue','Helvetica','Arial','Hiragino Sans','ヒラギノ角ゴシック',YuGothic,'Yu Gothic','メイリオ', Meiryo,'MS Pゴシック','MS PGothic';
        // font-family: @font-name !important;
        margin:0;
        padding:0;
        position:relative;
    	  overflow: auto;
    }
    
    .header {
        position: relative;
        background:green;
        width: 100%;
        height: 40px;
    }
    
    .content {
        width: 100%;
        min-height: calc(100% - 60px) !important;
        position: relative;
        display: flex;
        flex-direction: column;
      background: red;
    }
    
    .footer{
        width: 100%;
        height: 20px;
        position: relative;
        background: blue;
    }
    
    .subcontent{
      position:absolute;
      width: 100%;
      height: 100%;
      background:rgba(155,155,155,0.5)
    }
    
    .subcontent-header{
      position: relative;
      width: 100%;
      height: 30%;
      background:rgba(155,0,0,0.5)
    }
    
    .subcontent-header h1 {
      position: absolute;
    }
    
    .subcontent-content{
       position: relative;
      width: 100%;
      height: 70%;
      background:rgba(0,0,155,0.5) 
    }
<div class="header"></div>
    <div class="content">
      <div class="subcontent">
        <div class="subcontent-header">
          <h1>title</h1>
        </div>
        <div class="subcontent-content"></div>
      </div>
    </div>
    <div class="footer">
 </div>

关于html - 为什么对象边距是基于绝对父级而不是直接相对父级应用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57265877/

相关文章:

javascript - 通过脚本调用时,iframe 内容未进入正确位置

html - 如何摆脱为我的悬停效果动画显示的这条线?

javascript onclick代码更改

android - jQuery Mobile 中的文本区域

html - 响应式水平布局

javascript - 当我直接点击 index.html 时,聚合物不起作用

css - 用 CSS flex 矩形的长度?

PHP永久旋转图像文件

javascript - 为什么我使用 javascript 时得到 "Illegal Character Syntax Error Unterminated String Literal"

html - CSS3 向右移动元素