135

How to give border to any element using css without adding border-width to the whole width of element?

Like in Photoshop we can give stroke- Inside , center and outside

I think default css border properties is center like center in photoshop, am i right?

I want to give border inside the box not outside. and don't want to include border width in box width.

Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852

12 Answers12

282
outline:1px solid white;

This won't add the extra width and height.

Anne
  • 26,765
  • 9
  • 65
  • 71
abenson
  • 2,951
  • 1
  • 14
  • 6
39

Check out CSS box-sizing...

The box-sizing CSS3 property can do this. The border-box value (as opposed to the content-box default) makes the final rendered box the declared width, and any border and padding cut inside the box. You can now safely declare your element to be of 100% width, including pixel-based padding and border, and accomplish your goal perfectly.

  • -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
  • -moz-box-sizing: border-box; /* Firefox, other Gecko */
  • box-sizing: border-box; /* Opera/IE 8+ */

I'd suggest creating a mixin to handle this for you. You can find more information on box-sizing at W3c http://www.w3schools.com/cssref/css3_pr_box-sizing.asp

Chad
  • 579
  • 4
  • 9
  • 3
    This is the best answer, though I don't think you really need all the prefixes. http://caniuse.com/#feat=css3-boxsizing – Justin Dec 14 '14 at 03:45
  • 4
    ^ Justin is correct, you no longer need the prefixes. `box-sizing` will suffice. – Chad Aug 10 '17 at 22:00
36

Depending on your intended browser support you can use the box-shadow property.

You can set the blur value to 0 and the spread to what ever thickness you're after. The great thing about box shadow is that you can control whether it is drawn outside (by default) or inside (using the inset property).

Example:

box-shadow: 0 0 0 1px black; // Outside black border 1px

or

box-shadow: 0 0 0 1px white inset; // Inside white border 1px

One great advantage of using box shadow is you can get creative by using multiple box shadows:

box-shadow: 0 0 0 3px black, 0 0 0 1px white inset;

The only thing I can't say is what difference this will make rendering performance wise. I would assume it might become an issue if you had hundreds of elements using this technique on the screen at once.

James Bryant
  • 461
  • 4
  • 3
25

I ran into the same issue.

.right-border {
    position: relative;
}

.right-border:after {
    content: '';
    display: block;
    position: absolute;
        top: 0; right: 0;
    width: 1px;
    height: 100%;
    background: #e0e0e0;
}

This answer allows you to specify one single side. And would work in IE8+ - unlike using box-shadow.

Of course change your pseudo elements properties as you need to single out a specific side.

* New and Improved *

&:before {
content: '';
display: block;
position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
border: 1px solid #b7b7b7;
}

This allows ability to use border and hit multiple sides of a box.

mikevoermans
  • 3,967
  • 22
  • 27
11

Use box-sizing: border-box in order to create a border INSIDE a div without modifying div width.

Use outline to create a border OUTSIDE a div without modifying div width.

Here an example: https://jsfiddle.net/4000cae9/1/

Notes: border-box currently it is not supported by IE

Support:

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

http://caniuse.com/#search=border-box

#test, #test2 {
    width: 100px;
    height:100px;
    background-color:yellow;
}
#test {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    border: 10px dashed blue;
}
#test2 {
    outline: 10px dashed red;
}


<p>Use box-sizing: border-box to create a border INSIDE a div without modifying div width.</p>
<div id="test">border-box</div>
<p>Use outline to create a border OUTSIDE a div without modifying div width.</p>
<div id="test2">outline</div>
GibboK
  • 71,848
  • 143
  • 435
  • 658
7

As abenson said, you can use an outline but one gotcha is that Opera might draw a "non-rectangular shape". Another option that seems to work is to use negative margins, such as this css:

div {
  float:left;
  width: 50%;
  border:1px solid black;
  margin: -1px;

}

With this html:

<body>
  <div>A block</div>
  <div>Another block</div>
</body>

One other less clean option is to add extra markup to the html. For example, you set the width of an outer element and add the border to the inner one. The CSS:

.outer { width: 50%; float: left;}
.inner { border: 1px solid black; }

And the html:

<body>
  <div class="outer">
    <div class="inner">A block</div>
  </div>
  <div class="outer">
    <div class="inner">Another block</div>
  <div>
</body>
ChrisD
  • 3,378
  • 3
  • 35
  • 40
7

Use padding when there is no border. Remove padding when there is a border.

.myDiv {
    width: 100px;
    height: 100px;
    padding-left: 2px;
    padding-right: 2px;
}

.myDiv:hover {
    padding-left: 0;
    padding-right: 0;
    border-left: 2px solid red;
    border-right: 2px solid red;
}

Essentially, just replace the 2px padding with 2px borders. Div size remains the same.

Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
3

Usually, layout shifting is the problem.

If you don't need border-radius then outline: 1px solid black; works.

If you do, make the border transparent and change its color when it's supposedto show:

/* RELEVANT */
.my-div {
  border-radius: 8px;
  border: 2px solid transparent;
}

.my-div:hover {
  border: 2px solid #ffffffa8;
}



/* NOT RELEVANT */
.pretty {
  color: white;
  padding: 10px 20px;
  background: #0077b6;
  font-size: 16px;
  transition: border .15s ease-out;
  cursor:pointer;
}
<button class="pretty my-div">
  Button
</button>
akamichael
  • 113
  • 7
2

In your case can you fudge it by subtracting half the border from the padding? (-2.5 from the padding if your border is 5px wide, you can't have negative padding so to go smaller reduce the overall width of the box). You can add an extra 2.5px to the margin to keep the overall box the same size.

I really don't like this suggestion, but I don't think there is a way do handle this cleanly.

vfilby
  • 9,938
  • 9
  • 49
  • 62
1

Thus, you're trying to achieve the same as the well known IE box model bug? That's not possible. Or you want to support clients with IE on Windows only and choose a doctype which forces IE into quirksmode.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Another option, if your background color is solid:

body { background-color: #FFF; }

.myDiv {
    width: 100px;
    height: 100px;
    border: 3px solid #FFF;  // Border is essentially invisible since background is also #FFF;
}

.myDiv:hover {
    border-color: blue;  // Just change the border color
}
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
  • You can substitute `#FFF` for transparent and then you don't need to remember to change two values. Your suggestion has the benefit that if you needed a dashed border this would work where as `box-shadow` lacks in this regard. – limitlessloop Sep 24 '18 at 22:44
0

outline:3px solid black || border:3px solid black

div{
height:50px;
width:150px;
text-align:center;

}

div{    /*this is what you need ! */
outline:1px solid black
}
<div>
hello world
</div>
Matan Shushan
  • 1,204
  • 1
  • 10
  • 25