Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I setup a border inside the div

I was just wondering if there's a way to create a div with the "border" inside the div. What I mean is: I have a div of 200px for example and I want the border to be inside that 200 pixels, without exceeding.

I need to achieve the effect of a div with a border not on the edge of the shape, but 5px more inside. An image can talk more than hundreds words

I want this:

circle inside-borded

Here is my code:

http://jsfiddle.net/hpLYD/1/

The CSS:

.circle {
    border-radius: 50%;
    width: 200px;
    height: 200px;
    background: red;
    border: 3px solid blue;
}

Padding property is expanding the whole div including the border.

How can I achieve that effect using only css? is it possible?

like image 357
pwnjack Avatar asked Jun 03 '13 13:06

pwnjack


2 Answers

You can do this using the CSS3 property box-shadow. Add the following to your CSS:

box-shadow: 0px 0px 0px 5px #f00;

jsFiddle example

like image 170
j08691 Avatar answered Oct 30 '22 00:10

j08691


While box-shadow is most likely the best way to go, people seem to forget that the question required that the border didn't exceed 200px. In order to actually achieve this you can use the inset parameter on the box-shadow attribute (which will make an inner shadow).

You will also need to change the box-sizing to border-boxsuch that the size is proportional to the border and not the content.

Here's an JSFiddle with the result

.circle {
    border-radius: 50%;
    width: 200px;
    height: 200px;
    background: red;
    border: 3px solid red;
    box-shadow: 0px 0px 0px 5px blue inset;
    box-sizing: border-box;
}
like image 21
Daniel Perván Avatar answered Oct 29 '22 23:10

Daniel Perván