简体   繁体   English

根据屏幕尺寸更改布局

[英]Change layout based on screen size

I would like the content of my website header to occupy 100% of the screen width on regular-sized desktops/laptops, but to be centered on larger displays. 我希望我的网站标题的内容在常规​​尺寸的台式机/笔记本电脑上占据屏幕宽度的100%,但要以较大的显示器为中心。 By "larger displays", I'm referring to the actual size of the display too, not just the resolution. 我所说的“更大的显示器”也是指显示器的实际尺寸,而不仅仅是分辨率。 On my 15" laptop and my 23" desktop (both having the same resolution of 1920x1080), I would like the displays to be different. 我希望在15英寸笔记本电脑和23英寸台式机(均具有1920x1080的相同分辨率)上,显示器有所不同。 Having such a wide menu on a 23" display doesn't look good as there are wide empty parts. 在23英寸的显示屏上显示如此宽的菜单效果不佳,因为那里有宽阔的空白部分。

I'm currently using a .container BootStrap class for the contents of the header, and I overrided a media query so that the container has a width of 100% when the screen width exceeds 1200px. 我当前正在使用.container BootStrap类作为标题的内容,并且我重写了媒体查询,以便当屏幕宽度超过1200px时容器的宽度为100%。 Again, this isn't really what I want : 再次,这不是我真正想要的:

  • If the screen width exceeds 1200px, the header width should be 100% 如果屏幕宽度超过1200像素,则标题宽度应为100%
  • If the screen width exceeds 1920px, the header width should be the default one, and the header should be centered 如果屏幕宽度超过1920像素,则标题宽度应为默认宽度,并且标题应居中
  • If the screen width exceeds 1200px, and the screen itself is large (anything above 19"), the header width should be the default one, and the header should be centered. 如果屏幕宽度超过1200像素,并且屏幕本身很大(大于19英寸),则标题宽度应为默认宽度,并且标题应居中。

I'm not sure if that's the best approach, but I'm open to all suggestions. 我不确定这是否是最好的方法,但是我愿意接受所有建议。

Thanks 谢谢

My solution was to use media queries based on pixel density 我的解决方案是使用基于像素密度的媒体查询

This allowed to write something like 这允许写类似

@media screen and (max-resolution: 116dpi){

116dpi is the DPI of a 19" screen with a resolution of 1920x1080. If the screen gets larger, say 23" with the same resolution, pixel density gets lower, then we have something below 116dpi. 116dpi是分辨率为1920x1080的19英寸屏幕的DPI。如果屏幕变大,例如具有相同分辨率的23英寸,则像素密度会降低,那么我们的分辨率将低于116dpi。

Try out setting media screen and limitation through min-width. 尝试通过最小宽度设置媒体屏幕和限制。 http://www.w3schools.com/cssref/css3_pr_mediaquery.asp http://www.w3schools.com/cssref/css3_pr_mediaquery.asp

@media screen and (min-width: 1200px) {
      .container {
          width: 100%;
      }
  }

@media screen and (min-width: 1920px) {
      .container {
          width: 500px;
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM