简体   繁体   English

根据屏幕宽度和到中心的偏移量在左侧填充屏幕宽度上的 div

[英]Make div on left side fill screen width based on screen width and with an offset to the center

I have a parent div with two children inside and I want the first child to go to the left side and the second child should go to the right side.我有一个父 div,里面有两个孩子,我希望第一个孩子 go 到左侧,第二个孩子应该 go 到右侧。 My main problem is they can't start from the exact middle point.我的主要问题是他们不能从确切的中间点开始。 They have to start like -100px and +100px from the middle.他们必须从中间开始 -100px 和 +100px。 They should then from that point fill the remaining width of the left or right side of the screen and if the window is resized it should still stick to the exact same position in the middle minus the X Amount of Pixels.然后,它们应该从该点填充屏幕左侧或右侧的剩余宽度,如果调整 window 的大小,它仍应保持在中间完全相同的 position 减去 X 像素数量。

This is what my code looks like so far:这是我的代码到目前为止的样子:

<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],
      },
    },
  },

i wrote it in css u can implement it in your app if this is the result you want我在 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>

You can use flex to make gaps between nested div's and add margin-left: 100px .您可以使用flex在嵌套div's之间制作间隙并添加margin-left: 100px

An example:一个例子:

 .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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM