简体   繁体   English

非全屏宽度父级的子元素的全屏宽度

[英]Full screen width for child element of a non full screen width parent

This question builds upon that one , where in order to apply full screen width to a child of a non full width parent element, the following rule is used on the child element: 这个问题建立在这个问题的基础上为了将全屏宽度应用于非全角父元素的子元素,在子元素上使用以下规则:

width: 100vw;
margin-left: calc(-50vw + 50%);

As shown in this Fiddle , this solution doesn't work in the presence of a vertical scrollbar though: 100vw doesn't take the scrollbar into account and hence the child element ends up being wider than the screen (note: works perfectly without scrollbar). 该小提琴中所示,该解决方案在垂直滚动条的情况下不起作用: 100vw不考虑滚动条,因此子元素最终比屏幕宽(注意:在没有滚动条的情况下完美地工作) 。

Is there a way to solve this problem so that the child element takes exactly full screen width? 有没有一种方法可以解决此问题,以便子元素完全占据整个屏幕宽度? If not in pure CSS then with JS? 如果不是在纯CSS中使用JS?

Note: an overflow rule on body isn't acceptable in my case as I need the child to fill the exact width of the screen. 注意:在我的情况下, body上的overflow规则是不可接受的,因为我需要孩子填充屏幕的确切宽度。

https://jsfiddle.net/k3nvkL35/4/ https://jsfiddle.net/k3nvkL35/4/

One of the issues you'll come across with the solution here is that I believe scrollbar widths are not universal, and so you may need to implement some conditional logic to affect width/margin based on that. 在此解决方案中遇到的问题之一是,我认为滚动条的宽度不是通用的,因此您可能需要实现一些条件逻辑来基于此影响宽度/边距。

That being said, you may find this useful. 话虽如此,您可能会发现这很有用。 The function below will check to see if the document has a vertical scrollbar by comparing the document's height to the window's height. 下面的功能通过将文档的高度与窗口的高度进行比较,来检查文档是否具有垂直滚动条。 Based on the existence of said scrollbar, it will modify the child's width and margins to fit the window. 基于所述滚动条的存在,它将修改孩子的宽度和边距以适合窗口。

Again, it likely requires tweaking, though it should provide a decent foundation. 同样,它可能需要进行调整,尽管它应该提供良好的基础。

 function adjustWidth() { if ($(document).height() > $(window).height()) { $(".child").css({ "width": "calc(100vw - 18px)", "margin-left": "calc(-50vw + 50% + 9px)" }); } else { $(".child").css({ "width": "", "margin-left": "" }) } } $(window).resize(adjustWidth).trigger("resize"); 
 .parent { width: 500px; height: 500px; margin: 0 auto; border: 3px solid green; } .child { height: 100px; background: red; width: 100vw; margin-left: calc(-50vw + 50%); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent"> <div class="child"> content </div> </div> 

Simple. 简单。 Since the parent isn't positioned in any way, then the child can be positioned absolutely. 由于父母没有任何位置,因此可以绝对地安置孩子。 No messing with calc or otherwise, works everywhere. 不会干扰calc或其他方面,在任何地方都有效。

.child {
  height: 100px;
  background: red;
  position:absolute;
  left:0;
  right:0;
}

Here is a working fiddle: https://jsfiddle.net/vgbr6qw2/ 这是一个工作的小提琴: https : //jsfiddle.net/vgbr6qw2/

I found this solution, which was sourced by this guy, but I don't know who originally did it. 我发现这个解决方案,这是由采购这个家伙,但我不知道是谁最初做的。 I haven't fully tested it, so it may not work in all browsers. 我尚未对其进行全面测试,因此可能无法在所有浏览器中使用。 I know the vw unit isn't supported in IE8, as if anyone cares. 我知道IE8不支持vw ,好像有人在乎。

 body { margin:0; } .wrapper { width:100%; max-width:400px; margin:0 auto; background:pink; /* margin is for display purposes on stack overflows fullscreen snippet view */ margin-top:80px; } .full-width { width:100vw; margin-left:-50vw; left:50%; background:red; position:relative; color:white; } 
 <div class="wrapper"> <span>Hi. I'm a paragraph.</span> <div class="full-width"> <p>You aint no paragraph, sucka!</p> </div> <strong>Be quiet, you weak losers.</strong> </div> 

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

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