简体   繁体   English

容器内需要图像以跨越页面的整个宽度,并且仍然充当内联块/块元素

[英]Need Image inside container to span full width of page and still act as a inline-block / block element

I am trying to have an image take the full width of the page while inside a div with a given width. 我正在尝试使图像在具有给定宽度的div内占据页面的整个宽度。 I managed to do this by adding it as a background image and adding the following styles: 我设法通过将其添加为背景图像并添加以下样式来做到这一点:

background-image: url(URL);
width: 100%;
height: 400px;
position: absolute;
left: 0;

However, the image is on top of all my other elements now, so I need to have the div display as a block or inline block element, but I'm not really sure the route to make this happen since when I apply those as styles nothing changes. 但是,该图像现在位于我所有其他元素的顶部,因此我需要将div显示为块或内联块元素,但是我不太确定实现此目的的途径,因为当我将其作为样式应用时没有什么变化。 Let me know if there is a good way to do what I'm trying to do. 让我知道是否有一种很好的方法可以做我想做的事情。 Thanks! 谢谢!

Here is a JSFiddle example of my problem: http://jsfiddle.net/T2t47/13/ 这是我的问题的JSFiddle示例: http : //jsfiddle.net/T2t47/13/

Look like this might be what you're looking for http://jsfiddle.net/T2t47/12/ 看起来这可能就是您正在寻找的http://jsfiddle.net/T2t47/12/

.introduction div {
    position:relative;
    height:200px;
    width:100%;
    background-position: center center;
    background-size:cover;
    left: 0;
    top:0;
    z-index: -999;
}

If I understand correctly you want the image to appear above the list in your example? 如果我理解正确,您希望图像显示在示例中的列表上方吗? If so, simply removing position: absolute; 如果是这样,只需删除position:absolute; and left: 0; 左:0; should keep the image filling the full width of the page and place it above the list. 应该使图像充满页面的整个宽度,并将其放置在列表上方。 This is because position: absolute; 这是因为position:absolute; takes the div out of the document flow. 将div移出文档流。

So your style will be: 因此,您的样式将是:

.introduction div {
    background-position: 0 -99px;
    width: 100%;
    background-size: 100% 500px;
    height: 200px;
}

http://jsfiddle.net/94Yvw/ http://jsfiddle.net/94Yvw/

So the reason your elements are falling beneath this element is because it taken out of the document flow. 因此,您的元素落在该元素之下的原因是因为它从文档流中删除。 (As you may have read earlier.) Think of this as a box surrounded by pebbles. (您可能已经阅读过。)可以将其视为一个被小卵石包围的盒子。 With the box in place, the pebbles sit finely around the box. 在盒子就位的情况下,鹅卵石可以很好地围绕盒子放置。 However if we pull the box up, some of our pebbles fall beneath the box, and we can no longer see them. 但是,如果我们拉起盒子,我们的一些鹅卵石就会掉到盒子下面,我们再也看不到它们了。 It's counted as what I like to call, a ghost element. 我想称呼它为幽灵元素。 It's undetected and untouchable by other elements. 它是其他元素无法检测和触碰的。

(Sorry I know this is getting pretty winded.) (对不起,我知道这已经变得很困难了。)

Essentially, what you're doing in your project is breaking it. 本质上,您在项目中所做的就是打破它。 If an element is 200 pixels wide, nothing inside of it should be more than 200 pixels wide. 如果元素的宽度为200像素,则其内部的宽度不应超过200像素。

Therefore the best workaround, i've provided in a JSFiddle shows you to set the parent element to no max-width. 因此,我在JSFiddle中提供的最佳解决方法是显示将父元素设置为无最大宽度。 But to add container elements, inside of this container element wrapping the necessary elements, and to set their widths to whatever set width you need to. 但是要添加容器元素,请在该容器元素内包装必要的元素,并将其宽度设置为所需的任何设置宽度。

.container{
  width: 100%;
}
.h1-container{
  width: 200px
}
.img-container{
  width: 100%;
}

This allows the image element room to breath, and other elements the containment they need. 这使图像元素空间可以呼吸,而其他元素则可以容纳它们。

Also, an excellent link on positions: http://css-tricks.com/absolute-relative-fixed-positioning-how-do-they-differ/ 此外,位置上的链接非常好: http//css-tricks.com/absolute-relative-fixed-positioning-how-do-they-differ/

Is this your desired result? 这是您想要的结果吗?

.introduction div {
    background-position: 0 -99px;
    width: 100%;
    background-size: 100% 500px;
    height: 200px;
    position: absolute;
    left: 0;
    z-index: -5; /* now it is not on top of everything ! */
}

http://jsfiddle.net/T2t47/4/ http://jsfiddle.net/T2t47/4/

it can be easily done with vw units and negative margin. 使用vw单位和负利润可以轻松完成。 And it's responsive friendly. 而且反应灵敏。

.full-width-block {
  width: 100vw;
  position: relative;
  left: 50%;  
  margin-left: -50vw;
}

See my fiddle: https://jsfiddle.net/ondrejruzicka/ghs6kmgw/ 看看我的小提琴: https : //jsfiddle.net/ondrejruzicka/ghs6kmgw/

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

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