简体   繁体   English

CSS视口问题:vh与100%

[英]CSS Viewport issue: vh vs. 100%

I code a responsive Website with HTML & CSS. 我使用HTML和CSS编写了一个响应式网站。 I use the Bootstrapper framework. 我使用Bootstrapper框架。

My issue: when I use 100% for a background image, the image will not reach the end of the page on Desktop screens (because the image scaled with 100% height is smaller than the monitor resultion). 我的问题:当我将100%用作背景图像时,该图像将不会到达桌面屏幕上的页面末尾(因为以100%高度缩放的图像小于监视器结果)。 On iPhone (Safari) it will look nice. 在iPhone(Safari)上看起来不错。 The footer will be underneath the image. 页脚将在图像下方。

When I use the Viewport-value 100vh the result on the Desktop Screen will look nice (Image will fill the background), but on mobile Devices (iPhone), the text will overlap the footer. 当我使用视口值为100vh时,桌面屏幕上的结果看起来不错(图像将填充背景),但是在移动设备(iPhone)上,文本将与页脚重叠。 Looks horrible. 看起来很恐怖。

I looking for a solution like: on Desktop use 100vh, on Mobile use 100%. 我正在寻找一种解决方案,例如:在台式机上使用100vh,在移动台上使用100%。 Is that possible? 那可能吗?

HTML-Code: HTML代码:

  <section id="myid"> <div class="myclass"> <div class="container"> <div class="row"> <div class="col-md-8 opaline"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <p>Great Content</p> </div> </div> </div> </div> </div> </div> </div> </section> <footer> <div class="container"> <div class="row"> <div class="col-md-10"> <p>Great Footer Content</p> </div> </div> </div> </footer> 

CSS: (OK-Result on Mobile Browser) CSS :(移动浏览器上的OK结果)

 .myclass { /* The image used */ background: linear-gradient( rgba(33, 37, 43, 0.6), rgba(33, 37, 43, 0.1) ), url(/images/image.jpg); background-position: center; background-repeat: no-repeat; background-size: cover; height: 100%; padding-top: 36px; } 

CSS: (OK-Result on Desktop Browsers) CSS :(桌面浏览器上的OK结果)

 .myclass { /* The image used */ background: linear-gradient( rgba(33, 37, 43, 0.6), rgba(33, 37, 43, 0.1) ), url(/images/image.jpg); background-position: center; background-repeat: no-repeat; background-size: cover; height: 100vh; padding-top: 36px; } 

I also played with the calc function - with no success: 我也玩过calc函数-没有成功:

 height: calc(100vh - 000px); 
000 = height of the footer 000 =页脚高度

I looking for a solution like: on Desktop use 100vh, on Mobile use 100%. 我正在寻找一种解决方案,例如:在台式机上使用100vh,在移动台上使用100%。 Is that possible? 那可能吗?

if thats what you looking for, you can use the - @media CSS at-rule - 如果这就是您要寻找的内容,则可以使用-@ media CSS规则 -

The @media CSS at-rule can be used to apply styles based on the result of one or more media queries, in your case the screen size. @media CSS规则可用于基于一个或多个媒体查询的结果来应用样式(在您的情况下为屏幕尺寸)。

read more mdn doc 阅读更多mdn doc

w3schools exemples are nice w3schools示例很好

edit : found something that might be relevant css-tricks 编辑:发现可能与css技巧有关的东西

I applied what was on css-trick on the code you provided code 我在您提供的代码上应用了css-trick上的内容

 @media only screen and (min-device-width: 1000px) and (max-device-width: 1600px) and (-webkit-min-device-pixel-ratio: 1) { .myclass { /* The image used */ background: linear-gradient( rgba(33, 37, 43, 0.6), rgba(33, 37, 43, 0.1) ); background-position: center; background-repeat: no-repeat; background-size: cover; height: 100vh; padding-top: 36px; } } @media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2) { .myclass { /* The image used */ background: linear-gradient(rgba(33, 37, 43, 0.6), rgba(33, 37, 43, 0.1)); background-position: center; background-repeat: no-repeat; background-size: cover; height: 100%; padding-top: 36px; } } 
 <section id="myid"> <div class="myclass"> <div class="container"> <div class="row"> <div class="col-md-8 opaline"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <p>Great Content</p> </div> </div> </div> </div> </div> </div> </section> <footer> <div class="container"> <div class="row"> <div class="col-md-10"> <p>Great Footer Content</p> </div> </div> </div> </footer> 



Edit -2- : from the answer you replayed : 编辑-2-:根据您重放的答案:

Desktop Screen (1920x1200px resultion) 桌面萤幕(1920x1200px像素)

and in the code you posted you used 并在您发布的代码中使用了

 @media only screen 
 and (min-device-width: 1000px) 
 and (max-device-width: 1600px)`

try to change the 'max device width' query value to the wanted resultion - 1920px (and up) 尝试将“最大设备宽度”查询值更改为所需的结果-1920像素(及更高)


Edit -3- : 编辑-3-:

as you just replied in your solution, because the desktop view is the default one, removing the resolution pixel range completely from the desktop media query will might do the trick 正如您刚在解决方案中回答的那样,因为桌面视图是默认视图,所以从桌面媒体查询中完全删除分辨率像素范围可能会解决问题

Do you talk about that this works well only for iPhone or to all mobiles? 您是否说这仅适用于iPhone或所有手机?

Because if this works in all mobiles views, you can use @media to do it. 因为如果这在所有手机视图中都有效,则可以使用@media来实现。

Maybe these links can help you: 也许这些链接可以帮助您:

Using media queries 使用媒体查询

Media Queries for Standard Devices 标准设备的媒体查询

Using the viewport meta tag to control layout on mobile browsers 使用视口元标记控制移动浏览器上的布局

Responsive Web Design - Media Queries 响应式网页设计-媒体查询

Thanks for the hint to @media 感谢@media的提示

So I build this "switch". 所以我建立了这个“开关”。 It works well on an iPhone 6S, but on an Desktop Screen (1920x1200px resultion) the style will not appear at all. 它在iPhone 6S上运行良好,但在桌面屏幕(1920x1200px像素)上,该样式完全不会出现。 Where is my mistake? 我的错误在哪里?

 /* Mobile Device */ @media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait) { .myclass { /* The image used */ background: linear-gradient( rgba(33, 37, 43, 0.6), rgba(33, 37, 43, 0.1) ), url(/images/image.jpg); background-position: center; background-repeat: no-repeat; background-size: cover; height: 100%; padding-top: 36px; } } /* Desktop Screen */ @media only screen and (min-device-width: 1000px) and (max-device-width: 1600px) and (-webkit-min-device-pixel-ratio: 1) { .myclass { /* The image used */ background: linear-gradient( rgba(33, 37, 43, 0.6), rgba(33, 37, 43, 0.1) ), url(/images/image.jpg); background-position: center; background-repeat: no-repeat; background-size: cover; height: 100vh; padding-top: 36px; } } 

Solved it like this: 像这样解决它:

 /* Mobile Devices */ @media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2) { .myclass { /* The image used */ background: linear-gradient( rgba(33, 37, 43, 0.6), rgba(33, 37, 43, 0.1) ), url(/images/image.jpg); background-position: center; background-repeat: no-repeat; background-size: cover; height: 100%; padding-top: 36px; } } /* Desktop Screen */ /* Mobile Devices */ @media only screen { .myclass { /* The image used */ background: linear-gradient( rgba(33, 37, 43, 0.6), rgba(33, 37, 43, 0.1) ), url(/images/image.jpg); background-position: center; background-repeat: no-repeat; background-size: cover; height: 100vh; padding-top: 36px; } } 

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

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