Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

body tag overflow (auto, visible?)

Tags:

css

How come the body element doesn't use overflow-y: auto yet it still behaves the same way?

I believe it defaults to visible, but how does it actually work?

like image 955
user1164937 Avatar asked Apr 22 '16 12:04

user1164937


People also ask

How do I fix the overflow in CSS?

To change this, set the min-width or min-height property.” This means that a flex item with a long word won't shrink below its minimum content size. To fix this, we can either use an overflow value other than visible , or we can set min-width: 0 on the flex item.

Is overflow AUTO the default?

overflow is always defaulted to visible on every element on which it is not specified. The overflow property specifies whether to clip content, render scrollbars or just display content when it overflows its block level container.

What does tag overflow mean?

The overflow CSS shorthand property sets the desired behavior for an element's overflow — i.e. when an element's content is too big to fit in its block formatting context — in both directions.


2 Answers

The body (and html) tag are special cases, being at the root of the DOM hierarchy, and browsers must render these as if they were set to auto.

This is outlined in the overflow documentation on W3 back in CSS 2.1:

UAs must apply the 'overflow' property set on the root element to the viewport. When the root element is an HTML "HTML" element or an XHTML "html" element, and that element has an HTML "BODY" element or an XHTML "body" element as a child, user agents must instead apply the 'overflow' property from the first such child element to the viewport, if the value on the root element is 'visible'. The 'visible' value when used for the viewport must be interpreted as 'auto'. The element from which the value is propagated must have a used value for 'overflow' of 'visible'.

like image 87
Jeff Jenkins Avatar answered Oct 19 '22 04:10

Jeff Jenkins


Had to look into some white papers for this one. The body element is a special element in the DOM and has some "pseudo-immutable" properties that I'll get to in the answer.

First off, W3C position documentation points out the following:

The BODY element defines a special implicit container having the following properties:

  • Its position, width, height and clipping region are determined by the User Agent, and may not be modified.
  • It establishes a coordinate system for child elements, with the origin at the document's logical top, left corner.

Knowing this, we dig into what the defaults for these properties are for the body. The position is set to static which ends up making its height and width properties inherited from the parent html element.

I found this documentation a little bit strange in that I could change the value of the height on the body element and set a border around it:

body {
  border: 1px solid black;
  background-color: #1db3e7;
  height: 100px;
  width: 30em;
  margin: 0 auto;
  border-top: none;
  padding: 1em;
}
<body>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis dolor eget risus ultrices mattis. Maecenas dolor est, malesuada ac efficitur sed, cursus quis nibh. Sed vulputate arcu molestie ipsum pharetra hendrerit vitae ac mauris. Duis quis purus quis elit varius convallis. Proin dictum nec purus eget accumsan. Suspendisse dignissim sollicitudin risus. Praesent nec quam in nisl dictum lobortis. Maecenas ultricies purus nec turpis egestas, ultrices elementum arcu pretium. Vestibulum id diam eu arcu placerat ultrices. Donec porta augue magna, eu tristique dui sodales in. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In hac habitasse platea dictumst.

</body>

However, no matter what position and overflow I set, the text always has the capability to go beyond this border. This speaks to the immutable-ness of the position property, but it did make me question that statement for height.

Finally, the html element is what is actually making the scrollbar appear, based on the body's width and height dependency on the html element and its default position value. The first entry to controlling the page's scroll would be on the first child element of the body element.

like image 25
Jonathan Michalik Avatar answered Oct 19 '22 04:10

Jonathan Michalik