Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a border to a div without occupying any extra space?

Tags:

css

How can I apply a border to a div without occupying any extra space? The border must be inside the div.

like image 450
BalaKrishnan웃 Avatar asked Jan 23 '12 11:01

BalaKrishnan웃


People also ask

How do you put a border outside a div?

Usually by default, 'border:' puts the border on the outside of the width, measurement, adding to the overall dimensions, unless you use the 'inset' value: div {border: inset solid 1px black}; But 'outline:' is an extra border outside of the border, and of course still adds extra width/length to the element.

Does border take up space CSS?

We style individual sides of a border and round their edges. Borders take up space and affect the box-sizing.

How do I give a div a border?

This is a very simple thing to do. Just go to your stylesheet. css and type border: solid black 2px; in your div section.

Why is there extra space in my div?

If you try to put an image inside a <div> element that has borders, you will see an extra white space (around 3px) at the bottom of image. It happens because image is an inline-level element so browser adds some whitespace under the baseline to adjust other inline elements.


1 Answers

You have 3 choices:

  1. Inner and outer boxes(as @xpapad stated).
  2. Using outline, e.g., outline:1px #000 solid;. Read more.
  3. Using box-sizing, which is a css3 property. E.g., box-sizing:border-box;. Read more.
like image 55
Rufus Avatar answered Sep 27 '22 19:09

Rufus