Search code examples
htmlcssfluid-layout

how do I know how many pixels are being used when I use 100% width for the body tag


I am trying to make a website with a fluid layout. So to do this I am trying to use percentages as measurements. If I am not mistaken, the percentages are calculated from the parent element. Since the html tags does not have any set width, how does the body tag calculate 100% width? does 100% means the full resolution of the screen that you are viewing the page?

thanks


Solution

  • You have to read the specs to find the answer to your question:

    https://www.w3.org/TR/CSS22/visudet.html#x3 says about percentage widths:

    <percentage> Specifies a percentage width. The percentage is calculated with respect to the width of the generated box's containing block.

    About containing blocks:

    https://www.w3.org/TR/CSS22/visudet.html#containing-block-details says:

    The position and size of an element's box(es) are sometimes calculated relative to a certain rectangle, called the containing block of the element. The containing block of an element is defined as follows:

    The containing block in which the root element lives is a rectangle called the initial containing block. For continuous media, it has the dimensions of the viewport and is anchored at the canvas origin;

    (...)

    The root element is <html> (https://www.w3.org/TR/html-markup/html.html).

    The screen is considered a continuous media.

    The relationship between viewport and canvas is simple:

    https://www.w3.org/TR/CSS22/visuren.html#viewport https://www.w3.org/TR/CSS22/intro.html#canvas

    User agents for continuous media generally offer users a viewport (a window or other viewing area on the screen) through which users consult a document. User agents may change the document's layout when the viewport is resized (see the initial containing block).

    When the viewport is smaller than the area of the canvas on which the document is rendered, the user agent should offer a scrolling mechanism.

    So, trying to simplify this, the canvas size considers the content size even if it doesn't fit on the browser window.

    The browser window contains the viewport (considered without menus, scrolling bar and status bar).


    So, if <body> has 100% width, that means it would be the same width as the <html> element, which width is equal to the viewport's width.

    You can easily find the viewport width by inspecting the css for <html> element on Chrome.