[英]Ignore parent max-width css
I have a container on my website that keeps everything within a certain width like so.. 我的网站上有一个容器,可以将所有内容保持在一定宽度内,例如。
HTML 的HTML
<div class="container">
// content in here
</div>
CSS 的CSS
.container{
max-width: 1300px
}
now I have a div that I want to ignore the max-width and extend to the edges, but I don't want it to be positioned absolutely and taken out of the flow of the website so ive done this.. 现在我有一个div,我想忽略最大宽度并扩展到边缘,但是我不希望它被绝对定位并从网站流程中删除,所以我还是这样做了。
HTML 的HTML
<div class="extend-past">
// content here
</div>
CSS 的CSS
.extend-past{
width: 100vw;
height: 200px;
}
so this give me the correct width but its still following the max-width of the parent container and not starting from the left edge of the window, but the edge of the parent container now I can give it a negative margin but.. because the width of window changes the negative margin wont always be correct.. how can I get around this without making it an absolute element?? 所以这给了我正确的宽度,但是它仍然遵循父容器的最大宽度,而不是从窗口的左边缘开始,但是父容器的边缘现在我可以给它一个负边距,但是..因为窗口宽度变化的负余量将永远不会是正确的..如何解决这个问题而不使其成为绝对元素?
Thanks 谢谢
Child elements are left-aligned by default. 子元素默认为左对齐。
-------------
|----- |
-------------
What you could try instead is centering it: 您可以尝试将其居中:
-------------
| ----- |
-------------
But if an element is too big, it will stick to the left: 但是,如果一个元素太大,它将停留在左侧:
-------------
|-----------|---------
-------------
What you can then do, is centering it from the left (margin-left: 50%): 然后,您可以做的就是将其从左侧居中放置(左边距:50%):
-------------
| ------|--------------
-------------
If you now transform the child element's X axis 50% to the left ( x - 50 ), you get this: 如果现在将子元素的X轴向左(x-50)变换50%,则会得到以下信息:
--------------
----|------------|----
--------------
.container { background-color: green; margin: 0 auto; max-width: 50%; padding: 10px; } .container > div { width: 80%; height: 50px; margin: 5px auto; background-color: red; } .container > div.overflow { width: 150%; } .solution .overflow { transform: translateX(-50%); margin-left: 50%; }
<!-- Problem situation --> Problem situation: <div class="container"> <div> Item 1 </div> <div class="overflow"> Item 2 </div> </div> <!-- Solution --> Solution: <div class="container solution"> <div> Item 1 </div> <div class="overflow"> Item 2 </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.