简体   繁体   English

将子 div 扩展到容器 div 之外

[英]Extend child div beyond container div

I'm trying to expand this div across with width of the browser.我正在尝试用浏览器的宽度扩展这个 div。 I've read from here that you can use {position:absolute; left: 0; right:0;}我从这里读到你可以使用{position:absolute; left: 0; right:0;} {position:absolute; left: 0; right:0;} {position:absolute; left: 0; right:0;} to achieve that as in the jsfiddle here: http://jsfiddle.net/bJbgJ/3/ {position:absolute; left: 0; right:0;}来实现这一点,就像这里的 jsfiddle 一样:http: //jsfiddle.net/bJbgJ/3/

But the problem is that my current #container has a {position:relative;} property, and hence if I apply {position:absolute} to the child div, it would only refer to #container .但问题是我当前的#container有一个{position:relative;}属性,因此如果我将{position:absolute}应用于子 div,它只会引用#container Is there a way to still extend my child div beyond the #container ?有没有办法将我的孩子 div 扩展到#container之外?

I can think of five ways to potentially accomplish your goal: 我可以想出五种可能实现目标的方法:

  1. Use negative margins on the inner element to move it outside of the parent 在内部元素上使用负边距将其移动到父元素之外

  2. Use Javascript to move the inner element outside of the parent. 使用Javascript将内部元素移动到父元素之外。

  3. Change the source code and move the inner element outside of the parent. 更改源代码并将内部元素移到父元素之外。

  4. Use position: fixed on the inner element. 使用position: fixed在内部元素上。 This will allow the inner element to break out but has the down side that the element will be fixed at the given position (never moving). 这将允许内部元素突破但具有向下的元素将被固定在给定位置(从不移动)。

  5. Remove the position: relative; 删除position: relative; from the parent element or give the parent element a position: static 来自父元素或给父元素一个position: static

您可以尝试向父div添加overflow:visible ,然后使子项比父项更宽。

None of the answers above mention the best way of doing this, using position:relative on the full width container and position:absolute;left:0;width:100%;上面的答案都没有提到这样做的最佳方法,在全宽容器上使用position:relativeposition:absolute;left:0;width:100%; on div inside that is inside any centralised/offset div, as long as no other containers have declared position:relative, this will work.在任何集中/偏移 div 内部的 div 上,只要没有其他容器声明位置:相对,这将起作用。

Parent to position:static , child to position:absolute or 父级position:static ,子级到position:absolute

Parent to position:relative , child to position:fixed (with the fixed, never moving downside) 父母position:relative ,孩子到position:fixed (固定的,永不移动的下行)

Your left: 0; right:0; 你的left: 0; right:0; left: 0; right:0; works with both of these solutions, just note that your child div will draw on top of any divs below it in code, no matter what the div's display property is set to. 同时使用这两种解决方案,只需注意您的子div将在代码中的任何div下方绘制,无论div的display属性设置为什么。 You will either have to add a padding-top value to the next div to compensate ( simple example in jsfiddle ), or another div underneath that has the same height behavior as the child ( a much more elaborate example in codepen ), which is probably not ideal, but works in simple html/css. 您将要么必须在下一个div中添加一个padding-top值来补偿( jsfiddle中的简单示例 ),或者下面的另一个div具有与子项相同的高度行为( 在codepen中更详细的示例 ),这可能是不理想,但工作在简单的HTML / CSS。

I had a case where I needed to make a carousel and have it wrap outside of the parent element. 我有一个案例,我需要制作一个旋转木马并将其包裹在父元素之外。 You can set the child element to have a width: 100vw and set the parent element to have a max-width of a specific amount of pixels...or a calculated amount. 您可以将子元素设置为width: 100vw ,并将父元素设置为具有特定像素数量的最大宽度...或计算量。 With this setup our child component extends itself beyond the parent. 通过此设置,我们的子组件可以扩展到父组件之外。 JSFiddle 的jsfiddle

    .parent {
     // our parent container has 20px margins on each side so we set its max width accordingly
      max-width: calc(100vw - 20px)
    }

    .child {
      // set the child to wrap the entire viewport width. Account for any margins from the parent and offset them with a negative margin
      width: 100vw;
      transform: translateX(-20px);
    }

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

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