简体   繁体   English

位置绝对容器内的弹性容器不遵循父维度

[英]flex container inside position absolute container not follow parent dimension

I try to create modal box that 2 type design, my problem flex that inside absolute parent not follow parent dimension.我尝试创建 2 类型设计的模态框,我的问题是在绝对父级内部不遵循父级维度。 if u see image inside flex container, it not fill all space.如果您在 flex 容器中看到图像,它不会填满所有空间。 but the scroll appear.但滚动出现。

 const modal = document.querySelector('#modal') modal.style.width = `${screen.width}px`; modal.style.height = `${screen.height}px`;
 .container { width: 100% } #modal { position: absolute; top: 0; left: 0; z-index: 1001 !important; display: flex; justify-content: center; background-color: rgba(60, 60, 60, 0.5); overflow: hidden; } #modal > div { display: flex; flex-direction: column; justify-content: center; overflow: scroll; } #modal > div > div { background-color: bisque; } #modal > div > div > img { display: block; width: 100%; }
 <div class="container"> <div id="modal"> <div> <div> <img src="https://i.ytimg.com/vi/vGuQeQsoRgU/maxresdefault.jpg"> </div> </div> </div> </div>

You setting modal width bigger than your screenview by scripts, it can be done by CSS, see:您通过脚本设置模式宽度大于屏幕视图,可以通过 CSS 完成,请参阅:

 body { margin: 0; } * { box-sizing: border-box; } .container { position: relative; /* to make child element use parent dimentions, it have to be "relative" */ width: 100%; max-width: 1200px; /* set max container width */ margin: 0 auto; /* center align of container */ padding: 0 1rem; } /* use media queries to set you container max width size for larger screens */ @media (min-width: 1680px) { .container { max-width: 1400px; } } .modal { position: absolute; display: flex; justify-content: center; top: 0; left: 0; width: 100%; min-height: 70vh; /* minimal modal height will take 70% of screen height */ padding: 1rem; overflow: hidden; z-index: 1001; background-color: rgba(60, 60, 60, 0.5); } .modal-inner { display: flex; flex-direction: column; overflow: auto; } .image { width: 1000px; height: auto; }
 <div class="container"> <p>Phasellus volutpat, metus eget egestas mollis, lacus lacus blandit dui, id egestas quam mauris ut lacus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Quisque ut nisi. Morbi ac felis.</p> </div> <div class="container"> <div class="modal"> <div class="modal-inner"> <h2>Ut a nisl id ante</h2> <p>Proin sapien ipsum, porta a, auctor quis, euismod ut, mi. Pellentesque auctor neque nec urna. Praesent turpis. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus.</p> <img class="image" src="https://i.ytimg.com/vi/vGuQeQsoRgU/maxresdefault.jpg"> </div> </div> </div>

You can use object-fit: cover;您可以使用object-fit: cover; to fit image in the screen.使图像适合屏幕。

 const modal = document.querySelector('#modal') modal.style.width = `${screen.width}px`; modal.style.height = `${screen.height}px`;
 .container { width: max-content; height:max-content; overflow:hidden; } #modal { position: absolute; top: 0; left: 0; z-index: 1001 !important; display: flex; justify-content: center; background-color: rgba(60, 60, 60, 0.5); overflow: hidden; } img { object-fit: cover; }
 <div class="container"> <div id="modal"> <img src="https://i.ytimg.com/vi/vGuQeQsoRgU/maxresdefault.jpg"> </div> </div>

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

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