Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Border Position

Tags:

css

I want to try and achieve something in CSS using borders but they don't seem to have something I want. In Photoshop, when you add a Stroke (border), you select the position. Outside, Inside, or Center. Outside being where the entire border wraps around the object. Inside is where it sits on the inside (obviously), and center being half and half.

I want a 2px border that's positioned in the center. So it shows 1px outside and 1px inside. Is there anyway to do this with CSS? I imagine I can do it with a box-shadow of some kind but I'm horrendous at shadows in CSS.

There's also the issue of having to be pure CSS so I can't lay an image over it. Can someone possibly help me out with this.

Thanks.

like image 427
Spedwards Avatar asked Nov 26 '15 16:11

Spedwards


People also ask

How do you give a position to a border in CSS?

For example, if you have have a 2px left border and want it to appear as an inner border, you can just offset the whole container to the right, like this: position: relative; left: 2px; You might have to do other corrections, such as reducing the width of the container by 2px .

What is a border in CSS?

The CSS border is a shorthand property used to set the border on an element. The CSS border properties are use to specify the style, color and size of the border of an element.

What is border and margin?

Border - A border that goes around the padding and content. Margin - Clears an area outside the border. The margin is transparent.


1 Answers

There's a work around, since border represents outer stroke for you, you can make use of outline css property with outline-offset set to negative value to have the inner 1px stroke( 1 )JS Fiddle

body {
  padding-top: 10px;
}
#test {
  width: 250px;
  height: 200px;
  background-color: orange;
  margin: 0 auto;
  border: 1px navy solid;  /* outer stroke */
  outline: 1px navy solid;  /* inner stroke */
  outline-offset: -2px;  /* negative border width + outline width */
}
<div id="test"></div>

( 1 ) As the above fiddle might not demonstrate the explanation good enough, here's the same example with two colored strokes and 4px for each stroke instead of 1px Demo Fiddle

Resources:

http://caniuse.com/#search=outline

https://developer.mozilla.org/en/docs/Web/CSS/outline-offset

http://www.w3schools.com/cssref/css3_pr_outline-offset.asp

https://davidwalsh.name/outline-offset

like image 103
Mi-Creativity Avatar answered Oct 13 '22 07:10

Mi-Creativity