Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Border-radius and padding not playing nice

Tags:

css

padding

image

I'm having trouble trying to get a radius on images. I've simplified my problem and exaggerated the variables for demonstration purposes.

CSS:

div.wrap img {
    -moz-border-radius: 50px;
         border-radius: 50px;
}
img.pic {
    padding: 15px 25px 35px 45px;
}

HTML:

<div class="wrap">
    <img class="pic" src="http://i.imgur.com/UAef0.jpg"
         width="300" height="300" />
</div>

If I remove the padding, poof, pretty corners. If it helps, there's a reason why they're in two different classes. "wrap" can have more than one "pic" in it. Sometimes they'll be of the same class, other times they wont, sort like this:

img.left_pic  { float:left;  padding:5px 10px 5px 5px; }
img.right_pic { float:right; padding:5px 5px 5px 10px; }

Any help or insight would be appreciated.

jsFiddle: http://jsfiddle.net/NwfW6/

Edited for a solution:

This was more or less what I basically was trying to do. I think I was having a 'duh' moment. I'm sure now the declaration I needed to used was margin not padding. Another Thanx to GGJ for reminding me how to go about it the right way. And what Jan said about adding padding to an 'img' tag making no sense, It doesn't. My bad.

like image 268
Curtis Cook Avatar asked Feb 23 '12 08:02

Curtis Cook


People also ask

Why border-radius is not working?

If there are contents within the div that has the curved corners, you have to set overflow: hidden because otherwise the child div's overflow can give the impression that the border-radius isn't working. This answer worked for me.

What are the relationship of padding border and margin?

In CSS, a margin is the space around an element's border, while padding is the space between an element's border and the element's content. Put another way, the margin property controls the space outside an element, and the padding property controls the space inside an element.

What is a good border-radius?

3–4px Radius: This is best and probably the most safe choice.


3 Answers

Your border radius will be outside of the padding, try setting margins instead which will space outside of the border.

like image 54
GGJ Avatar answered Oct 12 '22 22:10

GGJ


Set the padding on "wrap" not on the image (setting paddings on images does not make much sense :)), that should fix your problem.

like image 39
Jan Hančič Avatar answered Oct 12 '22 23:10

Jan Hančič


This is a byproduct of applying both padding and a border-radius to the same element in some browsers (mostly IE and safari). The border-radius changes the curvature of the border component of the box model, which surrounds the padding component.

In addition to the other answers, another interesting thing that seems to fix the issue is adding a border line. If you don't want to see the border, you could use border: 1px solid transparent, like this:

.invisible-border {
    border: 1px solid transparent;
}

Demo in jsFiddle

screenshot

like image 22
KyleMit Avatar answered Oct 13 '22 00:10

KyleMit