最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change webpage zoom based on screen size? - Stack Overflow

programmeradmin15浏览0评论

Hi so I created a web page that looks fine on my desktop monitors. When I viewed the page using a laptop, the page looks abnormal. However, when I zoom the page to 67% it appears fine. Both my monitors and my laptop has the same resolution, 1920 x 1080, but my monitor is bigger than my laptop's one. I know that i can change zoom in CSS but it would look bad on my pc. I was wondering is there a way to change zoom based on screen size in javascript or CSS.

Hi so I created a web page that looks fine on my desktop monitors. When I viewed the page using a laptop, the page looks abnormal. However, when I zoom the page to 67% it appears fine. Both my monitors and my laptop has the same resolution, 1920 x 1080, but my monitor is bigger than my laptop's one. I know that i can change zoom in CSS but it would look bad on my pc. I was wondering is there a way to change zoom based on screen size in javascript or CSS.

Share Improve this question asked May 17, 2020 at 2:09 TransformerTransformer 311 gold badge1 silver badge2 bronze badges 1
  • This is not an easy question. Maybe the devicePixelRatio can help you: developer.mozilla/en-US/docs/Web/API/Window/… Best, Paul – Pauloco Commented May 17, 2020 at 2:55
Add a ment  | 

2 Answers 2

Reset to default 1

This can be done with JavaScript if you include the viewport meta tag in your HTML.

if (document.body.clientWidth < /*screen width pixels*/) {
  viewport = document.querySelector("meta[name=viewport]");
  viewport.setAttribute('content', 'width=device-width, initial-scale=0.67, user-scalable=0');
}

You can substitute the properties below with different values to test out what works for you.

initial-scale property controls the initial zoom level (possible range between 10%-1000% or 0.1-10.0).

user-scalable=0 prevents users from zooming

One option could be using media queries in your CSS to change the styles that get applied depending on the size of your screen. You could have one set of styles that works for larger screens like your PC, and another that applies for smaller screens like your laptop.

发布评论

评论列表(0)

  1. 暂无评论