Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the width of a webpage in inches

I have a CSS stylesheet that uses media queries to change how a page is displayed based on how many inches it is in width (for instance, if it is smaller than 4 inches or being displayed on a handheld device, it assumes a more mobile-friendly LAF).

The problem I have is with its content. On the homepage, there is a dock-style interface that dynamically changes height based on the current height and width of the window so that the text and items never leave the screen. However, my JS that determines this size does not know when the stylesheet has changed for handheld devices or small screens, so the behavior continues unpredictably in this mode. How can I use JavaScript detect when the page is less than or equal to 4 inches so that I can disable the auto-resizing of the then-reformed dock?

like image 357
Ky. Avatar asked Jan 09 '13 16:01

Ky.


People also ask

How do I find the width of a web page?

Find the content area and click to focus on it. Back in the bottom window, click Box Model on the right. The width and height will be shown. The first number is the width.

What is the width of a web page?

1280px and 1920px are the two standard widths for web design. A 1280px website will look great on laptops and mobile devices but not so great on large monitors. To ensure your site looks just as good on big screens as it does on small screens, set your max site width to 1920px or more.

What is the width and height of a Web page?

Page height, width and alignment Before smartphones and tablets became popular, web designers created fixed width pages that worked on the most common screen sizes - usually 1024 pixels wide by 768 pixels high.

What is the standard size for designing a web page?

1280×720 is considered to be the most suitable screen resolution for the desktop website version. Usually, the desktop version provides the best user experience and is supposed to be the most convenient and wide.

How do you measure width in HTML?

If you need to know the total amount of space an element occupies, including the width of the visible content, scrollbars (if any), padding, and border, you want to use the HTMLElement. offsetWidth and HTMLElement. offsetHeight properties.


2 Answers

It's a non-trivial problem.

Here's a link to a site that has a function (getUnits) to get the current computed style in a measurement unit of your choice (including inches) https://web.archive.org/web/20120427144951/http://upshots.org:80/javascript/javascript-get-current-style-as-any-unit

Using this function, you could check if (getUnits(document.body, "width").inch < 4). The way this function works, for the curious, is by creating a temporary element in the desired measurement space and reading off the ratio to pixels. In this way you let the browser respond based on its own knowledge the device's PPI. So this is sort of a polyfill for window.devicePixelRatio, however browsers mostly lie about their PPI. For these purposes, though, it doesn't matter since they will be applying the same lie to your inch-unit CSS.

like image 159
Plynx Avatar answered Oct 05 '22 10:10

Plynx


I don't test this on real browsers, but it should work:

  1. set a hidden <div id="screen" style="display:none"> in your HTML.

  2. In CSS, use media query like:

      @media screen
      { div#screen{color: blue;} }
    
      @media screen and (min-width: 13in)
      { div#screen{color: red;} }
    

Then you can read the div#screen color in JS to choose the actions.

Also, you should take windows resize event into JS consideration.

like image 26
Walkinraven Avatar answered Oct 05 '22 11:10

Walkinraven