【问题标题】:width in pixels ignoring max-width:100%忽略最大宽度的像素宽度:100%
【发布时间】:2016-10-11 20:40:01
【问题描述】:

我在 widthmax-width 使用具有祖父 flex 定位容器的元素时遇到问题。

当我在此元素中使用width 像素和max-width: 100% 时,如果宽度大于其父元素,它将忽略max-width:100%。但是,例如,如果我使用 width: 150%,那么 max-width:100% 就会发挥作用。

body{
  width: 640px;
  height: 360px;
  position: relative;
}
body:before{
  content:"";
  width:100%;
  height:100%;
  box-sizing: border-box;
  position: absolute;
  border: 2px solid black;
}
body:after{
  content:"16:9 screen size ratio example";
  position: absolute;
  bottom: 0;
  right: 10px;
}
#flex-container{
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: row nowrap;
  background-color: #95a5a6;
}
#sidebar{
  min-width:150px;
  flex: 0 1 150px;
  background-color: #7f8c8d;
}
#main-content{
  width:100%;
  flex: 1 1 auto;
  background-color: white;
}
.page{
  width: 100%;
  padding: 16px;
  box-sizing: border-box;
}
.grid{
  width: 800px;
  max-width: 100%;
  height: 200px;
  background-color: red;
  color: white;
  padding: 16px;
  box-sizing: border-box;
}
<div id="flex-container">
  <div id="sidebar"><h2>Sidebar</h2></div>
  <div id="main-content">
    <div class="page">
      <h1>Content page</h1>
      <div class="grid">
        A grid with certain width defined in units and based on the device. Specified width should not exceed its parent width (max-width: 100%).
      </div>
    </div>
  </div>
</div>


<br><br><br>


Should work like this:
<br>
<div style="width:640px;height:360px; border: 2px solid black;background-color:white;">
  <div style="width:100%;height:100%;">
    640px width parent
    <div style="max-width:100%; width:800px; height:100px; background-color:green;color:white;">
      800px width with max-width:100%
    </div>
  </div>
</div>

这里是JSFiddle

【问题讨论】:

  • 最大宽度总是优于宽度。不确定您的问题是什么。
  • @Paulie_D 应该。这就是问题所在,在我的示例中,max-width 没有击败width。看看jsfiddle。

标签: html css width flexbox


【解决方案1】:

您的容器 (body) 是 width: 640px

body#flex-container)的孩子是width: 100%。换句话说,640px。

您的左侧弹性项目 (#sidebar) 是 min-width: 150px。为了争论:width: 150px.

您的右侧弹性项目 (#main-content) 是 width: 100%。计算到:531.989px(原因待定)。

#main-content (.page) 的孩子是width: 100%。还有,531.989px

.page (.grid) 的孩子是width: 500px。这很适合父级,剩余空间(31.989 像素)。

您的max-width: 100% 永远不会看到任何操作。它有效,只是没有被调用。

在您的第二个示例中,您将 width: 800px 应用于大于 531.989 像素的 .grid 等效项,因此将调用 max-width: 100%


弹性项目最小尺寸

请注意,默认情况下,弹性项目不会缩小到超过其内容的大小。

min-width: 0 添加到#main-content

解释:Why doesn't flex item shrink past content size?


语法错误

另请注意,您的 line commenting syntax...

.grid {
  width: 500px; // For some reason this ignores max-width when is defined in units.
  max-width: 100%;
  height: 200px;
  background-color: red;
  color: white;
  padding: 16px;
  box-sizing: border-box;
}

....和parenthesis around the flex property...

#sidebar{
  min-width:150px;
  flex(0, 1, 150px);
  background-color: #7f8c8d;
}
#main-content{
  width:100%;
  flex(1, 1, auto);
  background-color: white;
}

正在造成重大问题。

Revised Fiddle

【讨论】:

  • 我更新了 sn-p 设置 .grid 宽度为 800px,它仍然击败了 max-width 规则。关于#main-content 宽度,我怎样才能给它正确的父级剩余宽度? (动态地,sn-p 维度只是一个例子)
  • 不要设置width,只需给#main-content 一个flex: 1。这将告诉它消耗行上的任何剩余空间。
  • @Michael_B 来救援 ;)