Position badge over corner of image automatically

前端 未结 4 1036
独厮守ぢ
独厮守ぢ 2020-12-04 15:55

I have a layout where images \"float\" within a certain area. The layout looks like this:

\"Layout

4条回答
  •  余生分开走
    2020-12-04 16:43

    It took some tryouts, but here it is: the size independent image badge positioner.

    HTML:

    10

    Houston

    T6 | Conduit | Infrastructure

    CSS:

    .tile {
        float: left;
        width: 176px;
        height: 206px;
        margin: 0 20px 20px 0;
     }
    .photo {
        display: block;
        width: 176px;
        height: 158px;
        text-align: center;
        line-height: 158px;
        margin-bottom: 10px;
    }
    a {
        display: inline-block;
        position: relative;
        line-height: 0;
    }
    img {
        border: none;
        vertical-align: middle;
    }
    ins {
        background: url('/images/badge.png') no-repeat 0 0;
        position: absolute;
        left: 0;
        top: 0;
        width: 20px;
        height: 20px;
    }
    

    Example:

    In previous less successful attempts (see edit history), the problem was getting the image vertically centered ánd to get its parent the same size (in order to position the badge in the top-left of that parent). As inline element that parent doesn't care about the height of its contents and thus remains to small, but as block element it stretches to hís parent's size and thus got to high, see demonstration fiddle. The trick seems to be to give that parent a very small line-height (e.g. 0) and display it as an inline-block. That way the parent will grow according to its childs.

    Tested in Opera 11, Chrome 11, IE8, IE9, FF4 and Safari 5 with all DTD's. IE7 fails, but a center-top alignment of the photo with badge at the right position isn't that bad at all. Works also for IE7 now because I deleted the spaces in the markup within the a tag. Haha, how weird!

提交回复
热议问题