html - 为什么 margin 不适用于组件,即使是用相对位置定义的组件

标签 html css

我的代码如下所示:

xyz.html

<div class='main-container'>
    <div class="q-background">
    </div>
    <div class="q-text">
        <div>Order History</div>
    </div>

    <div class ="q-orders">
    </div>
</div>

xyz.scss

.main-container {
    height: 100vh;
    width: 100vw;
    margin-top: -4rem;
    .q-background {
        float: left;
        width: 100%;
        height: 14.3vw;
        background: url("../../image/i-header-list.jpg") no-repeat;
        background-size: contain;
    }
    .q-text {
        position: relative;
        margin-top: 1rem;
        font-size: 2rem;
        text-align: center;
    }
    .q-orders {
        position: relative;
        margin-left: 2rem;
        float: left;
        height: 50%;
        width: 50%;
        background-color: #ff3;
    }
}

现在这里发生的是 ma​​rgin-top:1remq-text 不起作用,即使在定义 position:relative 之后也是如此。那么有什么遗漏吗?

最佳答案

margin-top 正在处理 .q-text,但是您在父级上设置了 margin-top:-4rem。您应该避免 margin 出现负值。

.main-container {
  height: 100vh;
  width: 100vw;
}
.main-container .q-background {
  float: left;
  width: 100%;
  height: 14.3vw;
  background: url("../../image/i-header-list.jpg") no-repeat;
  background-size: contain;
}
.main-container .q-text {
  position: relative;
  margin-top: 1rem;
  font-size: 2rem;
  text-align: center;
  top:0;
}
.main-container  .q-orders {
  position: relative;
  margin-left: 2rem;
  float: left;
  height: 50%;
  width: 50%;
  background-color: #ff3;
}
<div class='main-container'>
    <div class="q-background">
    </div>
    <div class="q-text">
        <div>Order History</div>
    </div>

    <div class ="q-orders">
    </div>
</div>

关于html - 为什么 margin 不适用于组件,即使是用相对位置定义的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43954304/

相关文章:

javascript - 如何在 Javascript 中使用 Wordnik API 获取随机词?

html - 如何使用 CSS 获取两个 div 类实例中的第二个?

css - 为什么框阴影不显示在页脚 div 上方?

javascript - TinyMCE - 创建带有空/无 href 的 anchor 标记

html - 将 <button> 插入层样式文本溢出 :ellipsis 的末尾

javascript - 使用 CSS 类作为 jQuery 变量

html - 如何在 HTML5 中旋转对象以使其保持原位

CSS 溢出 : auto out of div boundaries

javascript - 在 javascript 中,期望用户输入什么类型的表单会产生 null?

javascript - 为什么浏览器在 html 中创建了两个 div 但代码只说了一个?