简体   繁体   English

允许子元素扩展到父 div 的填充之外

[英]Allow child element expand beyond parent div's padding

One of my wrapper divs has it's own padding, and needs it for organizational layout purposes.我的一个包装 div 有它自己的填充,并且需要它用于组织布局目的。

However, one of the children components needs to go completely across the screen, so basically ignoring the padding it's parent set.但是,其中一个子组件需要 go 完全穿过屏幕,因此基本上忽略了父组件的填充。

How could I make this child component go completely across it's parent div, ignoring that padding set for it?我怎样才能使这个子组件 go 完全跨越它的父 div,而忽略为其设置的填充?

Here is an example of what I have with some further instructions on what I am trying to do:这是我所拥有的示例以及有关我正在尝试执行的操作的一些进一步说明:

 const App = () => { return ( <div style={{ padding: 20, backgroundColor: 'red' }}> <h3>See the following blue background paragraph. I want it to go beyond the padding I set for the parent div</h3> <p style={{overflowX: 'scroll', whiteSpace: 'nowrap', backgroundColor: 'blue'}}> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> <h3>Think of it this way: I still want it to scroll as needed, but, I want it to completely go across my parent div, surpassing the padding set on it.</h3> </div> ) } ReactDOM.render( <App />, document.getElementById('app') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

You could give it negative margin equal to your parent's padding, so something like this perhaps?你可以给它负边距等于你父母的填充,所以也许是这样的?

 const App = () => { return ( <div style={{ padding: 20, backgroundColor: 'red' }}> <h3>See the following blue background paragraph. I want it to go beyond the padding I set for the parent div</h3> <p style={{overflowX: 'scroll', whiteSpace: 'nowrap', backgroundColor: 'blue', margin: -20}}> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> <h3>Think of it this way: I still want it to scroll as needed, but, I want it to completely go across my parent div, surpassing the padding set on it.</h3> </div> ) } ReactDOM.render( <App />, document.getElementById('app') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

You can set the <p> as an absolute element.您可以将<p>设置为绝对元素。 See below:见下文:

 const App = () => { return ( <div style={{ padding: 20, backgroundColor: 'red', position: 'relative' }}> <h3>See the following blue background paragraph. I want it to go beyond the padding I set for the parent div</h3> <div style={{ paddingBottom: 30}}><p style={{overflowX: 'scroll', whiteSpace: 'nowrap', backgroundColor: 'blue', position: 'absolute', left: 0, right: 0, margin: 'auto'}}> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> </div> <h3>Think of it this way: I still want it to scroll as needed, but, I want it to completely go across my parent div, surpassing the padding set on it.</h3> </div> ) } ReactDOM.render( <App />, document.getElementById('app') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

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

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