【问题标题】:Make div on left side fill screen width based on screen width and with an offset to the center根据屏幕宽度和到中心的偏移量在左侧填充屏幕宽度上的 div
【发布时间】:2021-07-13 08:25:00
【问题描述】:

我有一个父 div,里面有两个孩子,我希望第一个孩子去左边,第二个孩子应该去右边。我的主要问题是他们不能从确切的中间点开始。他们必须从中间开始 -100px 和 +100px。然后,它们应该从该点开始填充屏幕左侧或右侧的剩余宽度,如果调整窗口大小,它仍应保持在中间完全相同的位置减去 X 像素量。

这是我的代码到目前为止的样子:

<div className={classes.backPartDivs}>
  <div />
  <div />
</div>

  backPartDivs: {
    width: '100%',
    position: 'absolute',
    top: 0,
    zIndex: 4,
    left: 0,
    height: 68,
    '& > div': {
      '&:first-child': {
        position: 'absolute',
        left: 'calc(-50vw + 50%)',
        height: 68,
        width: '100%',
        backgroundColor: 'red',
        boxShadow: theme.shadows[18],
      },
    },
  },

【问题讨论】:

标签: javascript css reactjs material-ui jsx


【解决方案1】:

我用 css 写的,如果这是你想要的结果,你可以在你的应用中实现它

.parentDiv{
    width: 100%;
    height: 68px;
    background:red;
    display:flex;
    padding:0 100px;
}
.parentDiv div{
width:100%;
height:68px;
margin:0 10px;
background:yellow;
}
<div class="parentDiv">
  <div></div>
  <div></div>
</div>

【讨论】:

  • 不,这是不对的。检查我的回答下的评论。这就是解决方案。
【解决方案2】:

您可以使用flex 在嵌套的div's 之间创建间隙并添加margin-left: 100px

一个例子:

.container {
    display: flex;
    outline: 1px solid blue;
}

.container > div:first-of-type {
    width: calc(50% - 50px);
    background-color: orange;
}

/* ----- Flexbox gap: ----- */
.container > * + * {
    margin-left: 100px;
    width: calc(50% - 50px);
    background-color: chocolate;
}
  <div class="container">
    <div>
      left
    </div>
    <div>
      right
    </div>
  </div>

【讨论】:

  • 这正是我不想要的。如果您调整屏幕大小,您会看到中间宽度发生变化,但必须保持不变!
  • @KrisKodira 在我看来margin 总是一样的。并且margin 始终设置为100px
  • 再次阅读我的评论。尝试调整浏览器的大小并查看中间部分。在调整大小时,它正在移动,我明确要求它不要这样做。无论如何,感谢您的帮助,但没有帮助。
  • @KrisKodira 请查看我的更新答案。看来我明白你想要什么了。试一试:)
  • 谢谢,但我不会更改代码,因为我已经得到了 2 次提到的正确答案;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
相关资源
最近更新 更多