我很难理解为什么对于我正在try 的内容,CSS会以这种方式工作.

假设我有一个外部和内部的div.内部div的宽度为100%,左边距为150px;在本例中,内部div向右溢出.我的理解是,内部div获得外部div的100%宽度,而空白符-Left将其推送到父div之外.但是,如果我将内部div的左边距更改为右边距,我不会看到它溢出到左边(div outter1和inner1).我判断了dev工具,发现inner1在右边增加了150px的边距,但这似乎没有任何作用.有谁能解释为什么会发生这种情况,以及利润率会发生什么?

enter image description here

 .outer {
      width:50vw;
      height:50vh;
      background-color:black;
      margin:auto;
    }
    
    .inner {
      width:100%;
      height:50%;
      background-color:pink;
      margin-left:150px
     
    }
    
    .outer1 {
  width:50vw;
  height:50vh;
  background-color:violet;
  margin:auto;
}

.inner1 {
  width:100%;
  height:50%;
  background-color:yellow;
  margin-right:150px
 
}
      
    <div class="outer">
      <div class="inner">
        <p>Inside parent container(not body), width:100% ; margin-left ; overflow to the right
      </div>
    </div>
    
    <div class="outer1">
  <div class="inner1">
    <p>Inside parent container(not body), width:100% ; margin-right ; doesnt overflow to the left
  </div>
</div>

推荐答案

the specification人起:

如果以上所有参数的计算值都不是‘AUTO’,则这些值被称为‘Over-Constraint’和one of the used values will have to be different from its computed value.如果计算'direction' property of the containing block has the value 'ltr‘、the specified value of 'margin-right' is ignored和该值,以使相等为真.如果‘Direction’的值是‘RTL’,则会发生在‘March-Left’上.

在ltr配置中忽略margin-right.改变方向,margin-left也会发生同样的事情

.outer {
  width: 50vw;
  height: 50vh;
  background-color: black;
  margin: auto;
  direction: rtl;
}

.inner {
  width: 100%;
  height: 50%;
  background-color: pink;
  margin-left: 150px
}

.outer1 {
  width: 50vw;
  height: 50vh;
  background-color: violet;
  margin: auto;
  direction: rtl;
}

.inner1 {
  width: 100%;
  height: 50%;
  background-color: yellow;
  margin-right: 150px
}
<div class="outer">
  <div class="inner">
    <p>Inside parent container(not body), width:100% ; margin-left ; overflow to the right
  </div>
</div>

<div class="outer1">
  <div class="inner1">
    <p>Inside parent container(not body), width:100% ; margin-right ; doesnt overflow to the left
  </div>
</div>

Html相关问答推荐

Tumblr博客:如何修复提交页面和询问页面框列计数问题?

Angular /HTML5不会播放本地文件夹中的音频mpeg

仅在加载视频时显示描述(<;Video>;标签)

嵌套表列高度不是100%高度的问题

如何将功能附加到嵌入式药剂中的按钮?

在TWebLabel标题中设置换行符/换行符的方法?

多次使用组件时计数Angular 14中嵌套数组的指令

带有图像的虚线边框

如何在 Bootstrap 5 中将两个导航栏元素放在末尾?

是否有语义 HTML 可以澄清含义?

jQuery 索引返回不正确的结果

四开将一个 qmd 文档中的单词链接到另一个 qmd 文档中的单词

悬停时宽度从 0 过渡到自动

文本不显示在 div 下方

SVG 填充 colored颜色 不使用轮廓填充空白区域

透明渐变凹矩形

如何使用 CSS 和 HTML 将文本放入图像中?

如何使具有两个元素的 div 行在移动设备中响应

两个按钮范围滑块的 CSS

SVG 适用于 Safari,不适用于 Chrome