CSS网格100%宽度和最大宽度

4
我创建了一个网格,现在遇到了max-width的问题。我希望有容器占用可用宽度,并通过左右边距进行限制。这些容器可以包含子元素。这些子元素可能比父容器大,并且可以使用类.move-to-right-border将其移到右侧边框以占用整个右侧宽度。
我现在已经向容器添加了max-width来限制宽度。但现在我无法设置子元素占用整个宽度。我尝试使用100vw,但是100vw宽度包括滚动条。有人有解决此问题的方法吗?
也许使用这个例子会更清楚,打开和关闭max-width注释以查看我的意图。
.row-right {
  box-sizing: border-box;
  margin-left: 200px;
  margin-right: 100px;
  max-width: 700px; /* to see the problem comment max-width in and out */
  width: calc(100% - 100px - 200px);
  border: 1px solid red;
}

.move-to-right-border {
  box-sizing: border-box;
  width: calc(100% + 100px);
  border: 2px solid blue;
}

http://codepen.io/anon/pen/eJymOL


你只是想让页面两侧保持相等的边距吗?你尝试给 body 添加 padding 吗?在你的代码片段中,body{ padding-right:200px;} 对我来说效果很好。 - Patrick
我已经建立了一个网格,其中行可以具有不同的边距,这就是为什么边距添加到行右侧的原因。可能会有另一行的行为不同。 - geierwally
2个回答

1

只需使用以下CSS

CSS

.row-right p {
  text-align: justify;
  width : 100%
}

希望这能帮到你 :)

0

我认为你想要的是这样的:

.parent{
  position: relative;
  height: 300px;
  padding: 10px 0;
  background-color: #99ff99;
  text-align: center;
}

.container{
  width: 200px;
  margin: 0 auto;
  height: 100px;
  padding: 30px 0;
  background-color: #ff9999;
}

.child{
  position: absolute;
  width: 100%;
  height: 50px;
  left: 0;
  background-color: #9999ff;
}
<div class="parent">
  This is parent
  <div class="container">
    This is container
    <div class="child">
        This is child
    </div>
  </div>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接