English 中文(简体)
Make a relative-positioned block element the same size as its contained absolute-positioned element
原标题:
  • 时间:2009-12-04 20:39:55
  •  标签:
  • css
  • position

Ok, I m trying to take several divs and all their content, and stack them on top of each other and make the containing div (#stack) appear in the normal flow of things via the following method:

CSS:

#stack
{
    position:relative;
    display:block;
}
#stack div
{
    position:absolute;
    left:0;
}

HTML:

<div id="stack">
    <div>
        <img src="images/1.jpg">
        <p>
            First Image
        </p>
    </div>
    <div>
        <img src="images/2.jpg">
        <p>
            Second Image
        </p>
    </div>
    <div>
        <img src="images/3.jpg">
        <p>
            Third Image
        </p>
    </div>
    <div>
        <img src="images/4.jpg">
        <p>
            Fourth Image
        </p>
    </div>
</div>

Which works, except now div#stack has a width/height of 0. I would like it to be the same size as its largest child.

Thanks in advance for the help!

最佳回答

As you are taking the inner divs out of the document s flow with position: absolute;, the outer div cannot in any case stretch to the dimensions of the biggest child. You have to use javascript to achieve that effect. If you re familiar with jQuery, you could use something like this:

var w = false, h = false;

$( #stack div ).each(function() {
    w = $(this).width() > w || !w ? $(this).width() : w;
    h = $(this).height() > h || !h ? $(this).height() : h;
});

$( #stack ).css({
    width: w,
    height: h
});
问题回答

Setting position:absolute takes the node out of the flow, which is why the parent, not having any flowed content, gets dimensions of 0x0.

If your server code (e.g., PHP) knows about these images, you could calculate the maximum width and the maximum height among them and set the height/width of the parent appropriately.

Or, as Tatu says, you can use JavaScript and do it client-side.

Do you want the width of the #stack to shrink-wrap its children as well, or are you just concerned about its height? I m assuming the latter, since you explicitly put display: block on it.

Because in that case, it is possible to do it just with CSS:

#stack {
    overflow: hidden;
    _zoom: 1;
}       
#stack div {
    float: left;
    margin-right: -100%;
}

I added the _zoom: 1 to trigger hasLayout in IE6. You can change that to your preferred method (e.g. in a separate IE6 stylesheet), or leave it out altogether if you don t need to support IE6.

In Opera, you can make the #stack shrink-wrap its children entirely (i.e. in width as well) when you float the stack (or display: inline-block) and add width: 100% to the child divs. I don t know if that s because of a bug in Opera, a bug in the other browsers, or just a difference due to a vague spec.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签