Arthur
Arthur

Reputation: 1548

Rectangular image in circle with see-through background

I have to implement a popup window like this:

It can appear anywhere on the screen. I need to put a 50x50 rectangular Facebook profile picture inside a circle with a thick white border. So I need to trim the profile image while keeping the background showing through. The problem is that the background is an image itself, so a mask image with a solid color outside the circle wouldn't work.

Is this possible somehow with HTML/CSS/jQuery?

Upvotes: 2

Views: 5948

Answers (3)

Frank
Frank

Reputation: 1056

enter image description here

Here is a sample with a glowing photograph like OSX lion.

Fidde: http://jsfiddle.net/QbULk/

HTML

    <div class="userprofile">
    <div style="background-image: url('http://images.nationalgeographic.com/wpf/media-live/photos/000/007/cache/spider-monkey_719_600x450.jpg');" class="mypic" id="mypic">
        <span class="glow">&nbsp;</span>
    </div>
    <span class="username">Warren G</span>
</div>

CSS

   body {background:black; margin:100px}
.userprofile {
    padding: 20px 0px 0px 0px;
    text-align: center;
    list-style-type: none;
}

.mypic {
    border-radius: 50px;
    position: relative;
    display: block;
    margin: 10px auto;
    box-shadow: 0px 0px 0px 4px #4b4b4b, 0px 0px 10px 10px rgba(0, 0, 0, 0.3);
    width: 100px;
    height: 100px;
    behavior: url(/resources/css/PIE.htc);
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
}
.glow {
    display: block;
    width: 109px;
    height: 107px;
    position: absolute;
    top: 0px;
    left: 50%;
    margin-left: -55px;
    box-shadow: inset 90px 110px 0px -90px rgba(255,255,255,.15);
    border-radius: 50%;
}

.username {color:#efefef}

PS. this is not supported in older versions if MSIE.

Upvotes: 1

maxedison
maxedison

Reputation: 17553

I'd just use border-radius to create the circle, in combination with overflow:hidden to make it crop the image. Here's an example: http://jsfiddle.net/6LHNy/

The markup:

<div class="wrapper">
    <img src="http://www.petscarecenter.com/wp-content/uploads/2011/05/dog.jpg" />
</div>

​The relevant CSS:

.wrapper {height:100px; width:100px; border-radius:100px; border:4px solid white; overflow:hidden}​

Upvotes: 8

JasonWoof
JasonWoof

Reputation: 4196

Sure, put the image as a background (with css) and use border-radius to round the corners of the containing element.

Something like this:

<div style="border: 3px solid white; border-radius: 25px;
height: 50px; width: 50px;
background: #aaa url(facebook-head.png)"></div>

Note: IE 8 and below won't round the corners: http://caniuse.com/#search=border-radius

Upvotes: 3

Related Questions