12

enter image description here

This is an image in a CSS circle. I want the circle to surround the image so the image is supposed to be in the center. How can i do that?

HTML:

<div class="circletag" id="nay">
    <img src="/images/no.png">
</div>

CSS:

div.circletag {
    display: block;
    width: 40px;
    height: 40px;
    background: #E6E7ED;
    -moz-border-radius: 20px;
    -webkit-border-radius: 20px;
}
div.circletag.img {

}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Chobeat
  • 3,445
  • 6
  • 41
  • 59

6 Answers6

21

Use the image as background image.

.circletag {
    display: block;
    width: 40px;
    height: 40px;
    background: #E6E7ED;
    -moz-border-radius: 20px;
    -webkit-border-radius: 20px;
    background-image: url(no.png);
    background-position:50% 50%;
    background-repeat:no-repeat;    
}

If you don't want to have the entire outer div to be clickable, this might work:

.circletag {
    display: block;
    width: 40px;
    height: 40px;
    background: #E6E7ED;
    -moz-border-radius: 20px;
    -webkit-border-radius: 20px;    
    text-align:center;
}

.circletag img{
    margin-top:7px;
}
Zsolt Schäfer
  • 286
  • 1
  • 5
4

Keeping your HTML intact and using below CSS class should work.

HTML:

<div class="circletag" id="nay">
    <img src="/images/no.png">
</div>

CSS:

div.circletag {
    display: block;
    width: 40px;
    height: 40px;
    background: #E6E7ED;
    text-align: center;
    align-content: center;
    align-items: center;
    border-radius: 50%;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
}
div.circletag>img {
   max-height: 100%;
   max-width: 100%;
}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
2

I think this is the easiest way to make an image centered within a circle

.circletag{
    width: 100px;
    height: 100px;
    padding: 20px;
    border-radius: 50%;
    background-color: #fff;
    line-height: 100px;
    text-align: center;
} 
.circletag img{
    width: 100%;
    height: auto;
    position: relative;
    top: 50%;
    transform: translate(0%, -50%);
}
0

write this

<div class="circletag" id="nay">
    <div style="padding-top: 7px;text-align: center;"><img src="/images/no.png"></div>
</div>

and

this is div.circletag.img {}

like this div.circletag img {}

Sonal Khunt
  • 1,876
  • 12
  • 20
0

Unable to test it quickly, but I feel you should try something like this:

div.circletag {
    display: block;
    width: 40px;
    height: 40px;
    background: #E6E7ED;
    /* For now you can actually use
     * plain border-radius
     *
     * -moz-border-radius: 20px;
     * -webkit-border-radius: 20px;
     */
    border-radius: 20px;

    text-align: center;
}

div.circletag img {
    line-height: 40px;
}
Lapple
  • 3,385
  • 20
  • 20
0

at the rounded div, give text-align:center and add padding-top. and also note that if you add padding, you must recalculate the height of the div.

check the demo here http://jsfiddle.net/fwQq4/

last-thing.. you don't need to specify display:block. only use display block, if the rounded element is span or anchor.

Ariona Rian
  • 9,153
  • 3
  • 24
  • 36