Search code examples
htmlcssresponsive-designresponsiveness

Responsively align 50% of a circle on the bottom-center of a rectangle?


I have a div filling all the width and an amount of the height, and then I have an smaller circle which needs to be half on the bottom/center of this div, half right after it.

All works until I got more height or try to zoom it, then the circle will just move vertically and no longer being 50/50. Also, zooming the page will make the circle expand from its top, not its middle.

jsFiddle

    <div id="rectangle">
        <img id="circle" src="http://alloutput.com/wp-content/uploads/2013/11/black-circle-mask-to-fill-compass-outline.png">
    </div>

Css:

body {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    margin-left: 0px;
}
#rectangle {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 92%;
    background: #E5E5E5;
}
#circle {
    position: absolute;
    top: 86%;
    left: 0;
    right: 0;
    width: 100px;
    margin-right: auto;
    margin-left: auto;
}

Solution

  • Since circle is inside the rectangle, than its position is relative to rectangle. That means you need to set the circle bottom property to half of circle radius:

    body {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0px;
        left: 0px;
        margin-top: 0px;
        margin-right: 0px;
        margin-bottom: 0px;
        margin-left: 0px;
    }
    #rectangle {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 92%;
        background: #E5E5E5;
    }
    #circle {
        position: absolute;
        bottom: -50px;
        left: 0;
        right: 0;
        width: 100px;
        height: 100px;
        margin-right: auto;
        margin-left: auto;
        background: none #000;
        border-radius: 50%;
    }
    <div id="rectangle">
        <div id="circle"></div>
    </div>

    I used html element to create a circle instead of the image you provided (you may delete it).