Set window size in html

Window sizes and scrolling

How do we find the width and height of the browser window? How do we get the full width and height of the document, including the scrolled out part? How do we scroll the page using JavaScript?

For this type of information, we can use the root document element document.documentElement , that corresponds to the tag. But there are additional methods and peculiarities to consider.

Width/height of the window

To get window width and height, we can use the clientWidth/clientHeight of document.documentElement :

For instance, this button shows the height of your window:

Browsers also support properties like window.innerWidth/innerHeight . They look like what we want, so why not to use them instead?

If there exists a scrollbar, and it occupies some space, clientWidth/clientHeight provide the width/height without it (subtract it). In other words, they return the width/height of the visible part of the document, available for the content.

window.innerWidth/innerHeight includes the scrollbar.

If there’s a scrollbar, and it occupies some space, then these two lines show different values:

alert( window.innerWidth ); // full window width alert( document.documentElement.clientWidth ); // window width minus the scrollbar

In most cases, we need the available window width in order to draw or position something within scrollbars (if there are any), so we should use documentElement.clientHeight/clientWidth .

Please note: top-level geometry properties may work a little bit differently when there’s no in HTML. Odd things are possible.

In modern HTML we should always write DOCTYPE .

Width/height of the document

Theoretically, as the root document element is document.documentElement , and it encloses all the content, we could measure the document’s full size as document.documentElement.scrollWidth/scrollHeight .

But on that element, for the whole page, these properties do not work as intended. In Chrome/Safari/Opera, if there’s no scroll, then documentElement.scrollHeight may be even less than documentElement.clientHeight ! Weird, right?

To reliably obtain the full document height, we should take the maximum of these properties:

let scrollHeight = Math.max( document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.body.clientHeight, document.documentElement.clientHeight ); alert('Full document height, with scrolled out part: ' + scrollHeight);

Why so? Better don’t ask. These inconsistencies come from ancient times, not a “smart” logic.

Get the current scroll

DOM elements have their current scroll state in their scrollLeft/scrollTop properties.

For document scroll, document.documentElement.scrollLeft/scrollTop works in most browsers, except older WebKit-based ones, like Safari (bug 5991), where we should use document.body instead of document.documentElement .

Luckily, we don’t have to remember these peculiarities at all, because the scroll is available in the special properties, window.pageXOffset/pageYOffset :

alert('Current scroll from the top: ' + window.pageYOffset); alert('Current scroll from the left: ' + window.pageXOffset);

These properties are read-only.

Читайте также:  Удалить всю сессию php

For historical reasons, both properties exist, but they are the same:

  • window.pageXOffset is an alias of window.scrollX .
  • window.pageYOffset is an alias of window.scrollY .

Scrolling: scrollTo, scrollBy, scrollIntoView

To scroll the page with JavaScript, its DOM must be fully built.

For instance, if we try to scroll the page with a script in , it won’t work.

Regular elements can be scrolled by changing scrollTop/scrollLeft .

We can do the same for the page using document.documentElement.scrollTop/scrollLeft (except Safari, where document.body.scrollTop/Left should be used instead).

Alternatively, there’s a simpler, universal solution: special methods window.scrollBy(x,y) and window.scrollTo(pageX,pageY).

  • The method scrollBy(x,y) scrolls the page relative to its current position. For instance, scrollBy(0,10) scrolls the page 10px down. The button below demonstrates this: window.scrollBy(0,10)
  • The method scrollTo(pageX,pageY) scrolls the page to absolute coordinates, so that the top-left corner of the visible part has coordinates (pageX, pageY) relative to the document’s top-left corner. It’s like setting scrollLeft/scrollTop . To scroll to the very beginning, we can use scrollTo(0,0) . window.scrollTo(0,0)

These methods work for all browsers the same way.

scrollIntoView

For completeness, let’s cover one more method: elem.scrollIntoView(top).

The call to elem.scrollIntoView(top) scrolls the page to make elem visible. It has one argument:

  • If top=true (that’s the default), then the page will be scrolled to make elem appear on the top of the window. The upper edge of the element will be aligned with the window top.
  • If top=false , then the page scrolls to make elem appear at the bottom. The bottom edge of the element will be aligned with the window bottom.

The button below scrolls the page to position itself at the window top:

And this button scrolls the page to position itself at the bottom:

Forbid the scrolling

Sometimes we need to make the document “unscrollable”. For instance, when we need to cover the page with a large message requiring immediate attention, and we want the visitor to interact with that message, not with the document.

To make the document unscrollable, it’s enough to set document.body.style.overflow = «hidden» . The page will “freeze” at its current scroll position.

The first button freezes the scroll, while the second one releases it.

We can use the same technique to freeze the scroll for other elements, not just for document.body .

The drawback of the method is that the scrollbar disappears. If it occupied some space, then that space is now free and the content “jumps” to fill it.

That looks a bit odd, but can be worked around if we compare clientWidth before and after the freeze. If it increased (the scrollbar disappeared), then add padding to document.body in place of the scrollbar to keep the content width the same.

Summary

  • Width/height of the visible part of the document (content area width/height): document.documentElement.clientWidth/clientHeight
  • Width/height of the whole document, with the scrolled out part:
let scrollHeight = Math.max( document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.body.clientHeight, document.documentElement.clientHeight );
  • Read the current scroll: window.pageYOffset/pageXOffset .
  • Change the current scroll:
    • window.scrollTo(pageX,pageY) – absolute coordinates,
    • window.scrollBy(x,y) – scroll relative the current place,
    • elem.scrollIntoView(top) – scroll to make elem visible (align with the top/bottom of the window).

    Источник

    HTML vs Body: How to Set Width and Height for Full Page Size

    Dave Gray

    Dave Gray

    HTML vs Body: How to Set Width and Height for Full Page Size

    CSS is difficult but also forgiving. And this forgiveness allows us to haphazardly throw styles into our CSS.

    Our page still loads. There is no «crash».

    When it comes to page width and height, do you know what to set on the HTML element? How about the body element?

    Do you just slap the styles into both elements and hope for the best?

    If you do, you’re not alone.

    The answers to those questions are not intuitive.

    I’m 100% guilty of applying styles to both elements in the past without considering exactly which property should be applied to which element. 🤦‍♂️

    It is not uncommon to see CSS properties applied to both the HTML and body elements like this:

    Does It Matter?

    The above style definition creates a problem:

    Setting min-height to 100% on both elements does not allow the body element to fill the page like you might expect. If you check the computed style values in dev tools, the body element has a height of zero.

    Meanwhile, the HTML element has a height equal to the visible part of the page in the browser.

    Look at the following screenshot from Chrome Dev Tools:

    empty_body

    Why Does This Happen?

    Using a percentage as a size value requires the element to reference a parent to base that percentage on.

    The HTML element references the viewport which has a height value equal to the visible viewport height. However, we only set a min-height on the HTML element. NOT a height property value.

    Therefore, the body element has no parent height value to reference when deciding what 100% is equal to.

    And The Problem May Be Hidden

    If you started out with enough content to fill the body of the page, you might not have noticed this issue.

    And to make it more difficult to notice, if you set a background-color on both elements or even on just one of them, the viewport is full of that color. This gives the impression the body element is as tall as the viewport.

    It’s not. It’s still at zero.

    The image above is taken from a page with the following CSS:

    Reverse-inheritance?

    In a strange twist, the HTML element assumes the background-color of the body element if you don’t set a separate background-color on the html element.

    So What is the Ideal Height Setting for a Full Responsive Page?

    For years, the answer was the following:

    This allows the HTML element to reference the parent viewport and have a height value equal to 100% of the viewport value.

    With the HTML element receiving a height value, the min-height value assigned to the body element gives it an initial height that matches the HTML element.

    This also allows the body to to grow taller if the content outgrows the visible page.

    The only drawback is the HTML element does not grow beyond the height of the visible viewport. However, allowing the body element to outgrow the HTML element has been considered acceptable.

    The Modern Solution is Simplified

    This example uses vh (viewport height) units to allow the body to set a minimum height value based upon the full height of the viewport.

    Like the previously discussed background-color, if we do not set a height value for the HTML element, it will assume the same value for height that is given to the body element.

    Therefore, this solution avoids the HTML element overflow present in the previous solution and both elements grow with your content!

    The use of vh units did cause some mobile browser issues in the past, but it appears that Chrome and Safari are consistent with viewport units now.

    Page Height May Cause a Horizontal Scrollbar

    Shouldn’t this say «Page Width»?

    In another strange series of events, your page height may activate the horizontal scrollbar in your browser.

    When your page content grows taller than the viewport height, the vertical scrollbar on the right is activated. This can cause your page to instantly have a horizontal scrollbar as well.

    So What is the Fix?

    You may sleep better knowing it starts with a page width setting.

    This problem arises when any element — not just the HTML or body element — is set to 100vw (viewport width) units.

    The viewport units do not account for the approximate 10 pixels that the vertical scrollbar takes up.

    Therefore, when the vertical scrollbar activates you also get a horizontal scrollbar.

    How to Set the Page for Full Width

    Not setting a width on the HTML and body elements will default to the full size of the screen. If you do set a width value other than auto, consider utilizing a CSS reset first.

    Remember, by default the body element has 8px of margin on all sides.

    A CSS reset removes this. Otherwise, setting the width to 100% before removing the margins will cause the body element to overflow. Here’s the CSS reset I use:

    How to Set Width to Your Preference

    While it may not always be necessary to set a width, I usually do.

    If you set the width to 100% on the body element you will have a full page width. This is essentially equivalent to not setting a width value and allowing the default.

    If you want to use the body element as a smaller container and let the HTML element fill the page, you could set a max-width value on the body.

    Conclusion

    With no height value provided for the HTML element, setting the height and/or min-height of the body element to 100% results in no height (before you add content).

    However, with no width value provided for the HTML element, setting the width of the body element to 100% results in full page width.

    This can be counterintuitive and confusing.

    For a responsive full page height, set the body element min-height to 100vh.

    If you set a page width, choose 100% over 100vw to avoid surprise horizontal scrollbars.

    I’ll leave you with a tutorial from my YouTube channel demonstrating the CSS height and width settings for an HTML page that is full screen size and grows with the content it contains:

    Do you have a different way of setting the CSS width and height that you prefer?

    Источник

Оцените статью