htmlcssresponsive-designcss-shapes

Placing an image centrally over another image with responsive width and height


How to place an image centrally over another image? I tried the answers from some similar questions, but none of them work for me.

Basically I need the 2 images to become 1 and

  1. it must be responsive (so the size changes automatically when different screen size devices access the web page.)

  2. The heart and ring should remain the same position to each other when user resize his or her screen (or web page window size etc.)

I am trying to use CSS to draw both the ring and the heart, but it is okay if you really need the picture to replace the ring or heart.

Here is my code: http://jsfiddle.net/4u6tfacw/

Here is my code:

<div id="logo">
    <div id="heart-container">
    </div>
    <div id="heart">
    </div>
</div>

#logo {
    width: 50%;
    height: 50%;
}

#heart {
    display: block;
    position: absolute;
    top: 70px;
    left: 30px;
    z-index: 1;
    width: 70%;
    height: 70%;

}

#heart-container {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    /*bottom:0;
        right:0;*/
    z-index: 1;
    width: 70%;
    height: 70%;
}

#heart-container {
    border-radius: 50%;
    behavior: url(PIE.htc);

    width: 220px;
    height: 220px;
    padding: 8px;

    background: #fff;
    border: 2px solid #666;
    color: #666;
    text-align: center;

    font: 32px Arial, sans-serif;
}

#heart:before,
#heart:after {
    position: absolute;
    content: "";
    left: 90px;
    top: 0;
    width: 90px;
    height: 130px;
    background: red;
    -moz-border-radius: 50px 50px 0 0;
    border-radius: 50px 50px 0 0;
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    transform: rotate(-45deg);
    -webkit-transform-origin: 0 100%;
    -moz-transform-origin: 0 100%;
    -ms-transform-origin: 0 100%;
    -o-transform-origin: 0 100%;
    transform-origin: 0 100%;
}

#heart:after {
    left: 0;
    box-shadow: 10px 10px 100px #6d0019;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
    -webkit-transform-origin: 100% 100%;
    -moz-transform-origin: 100% 100%;
    -ms-transform-origin: 100% 100%;
    -o-transform-origin: 100% 100%;
    transform-origin: 100% 100%;
}

Solution

  • Well, here is my attempt to satisfy the requirements of the question — which is not only about putting an image/element over another one, but about achieving that in a responsive manner.

    Key points

    1. Using a percentage value on bottom padding to make elements' heights respect their width1.
    2. Using percentage values on top, right, left, bottom offsets as well as width and height properties2.
    3. Using a high value in pixels on border-radius instead of percentage — for instance 1000px.
    4. And number four... well, the last step is trial and error!

    Example on JSFiddle.

    *, *:before, *:after {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }
    
    html, body {
        margin: 0;
        height: 100%;
        width: 100%;
    }
    
    #logo {
        width: 50%;
        /* height: 50%; */
        position: relative;
    }
    
    #logo:after {
        content: "";
        display: block;
        padding-bottom: 70%;
    }
    
    #heart {
        position: absolute;
        top: 26%;
        left: 35%;
        z-index: 1;
        width: 70%;
        height: 100%;
    }
    
    #heart-container {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 1;
        width: 70%;
        /* height: 70%; */
        border-radius: 50%;
        behavior: url(PIE.htc);
        background: #fff;
        border: 2px solid #666;
        
        color: #666;
        text-align: center;
        font: 32px Arial, sans-serif;
    }
    
    #heart-container:after {
        content: "";
        display: block;
        padding-bottom: 100%;
    }
    
    #heart:before,
    #heart:after {
        position: absolute;
        content: "";
        left: 0;
        top: 0;
        width: 39.130434782608695652173913043478%;
        height: 56.521739130434782608695652173913%;
        background: red;
        -moz-border-radius: 1000px 1000px 0 0;
        border-radius: 1000px 1000px 0 0;
        -webkit-transform: rotate(-45deg);
        -moz-transform: rotate(-45deg);
        -ms-transform: rotate(-45deg);
        -o-transform: rotate(-45deg);
        transform: rotate(-45deg);
        -webkit-transform-origin: 0 100%;
        -moz-transform-origin: 0 100%;
        -ms-transform-origin: 0 100%;
        -o-transform-origin: 0 100%;
        transform-origin: 0 100%;
    }
    
    #heart:after {
        left: -38.9%;
        box-shadow: 10px 10px 100px #6d0019;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        -o-transform: rotate(45deg);
        transform: rotate(45deg);
        -webkit-transform-origin: 100% 100%;
        -moz-transform-origin: 100% 100%;
        -ms-transform-origin: 100% 100%;
        -o-transform-origin: 100% 100%;
        transform-origin: 100% 100%;
    }
    <div id="logo">
        <div id="heart-container"></div>
        <div id="heart"></div>
    </div>


    1 Have a look at Responsive Container section of this topic.

    2 To find exact values, we can position/size things in an absolute length — like px — and then just measure things relative to each other.