Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

box-sizing + border + absolute child

Tags:

html

css

I recently discovered this problem.

Check for demo on jsfiddle

As for me, the outline behaves wrong, doesn’t it? Shouldn’t the box model of the .outside element include its borders, so that the absolute positioned child element outlines those borders aswell?

Is this a known problem? Is this really correct behavior? If so, could someone explain to me why it is?

What I expected to have as result

Please do not comment on how to solve the problem. But please explain why the problem occurs.

Thanks in advance :)

Regards phlips

like image 264
phlipsgeisler Avatar asked May 25 '16 09:05

phlipsgeisler


People also ask

Does box sizing get inherited?

No they won't. Even if we consider that it's the only CSS applied to your document, the example B will also target pseudo element to make them inherit the box-sizing and if we follow the parent-child structure they will logically get box-sizing:border-box since html is having box-sizing:border-box .

Should I always use box sizing border-box?

CSS border-box is the most popular choice for setting box-sizing . It guarantees that the content box shrinks to make space for the padding and borders. Therefore, if you set your element width to 200 pixels, border-box makes sure that the content, padding, and borders fit in this number.

What is box sizing border?

The box-sizing property allows us to include the padding and border in an element's total width and height. If you set box-sizing: border-box; on an element, padding and border are included in the width and height: Both divs are the same size now!

What's the difference between box sizing content box and border-box?

Note :- when using box-sizing : content-box; the content size remain same while border-box size grows as padding and border grow.


2 Answers

Just box-sizing: border-box is not enough, you need to inherit width and height from parent:

.inside {
  position: absolute;
  outline: #00f solid 2px;
  width: inherit;
  height: inherit;
}

Check here https://jsfiddle.net/2mytb43a/3/

like image 104
Jakob Avatar answered Oct 10 '22 15:10

Jakob


In this case when using position: absolute ..

.. the containing block is formed by the padding edge of the ancestor.

See CSS Specifications - 10.1 Definition of "containing block"

For whatever reason..

like image 26
101 Coding und Design Avatar answered Oct 10 '22 17:10

101 Coding und Design