简体   繁体   English

React - 如何在平板电脑上显示一半背景图像,在手机上显示1/4

[英]React - how to only show half of a background image on tablet and 1/4 on phone

Goal I have a background image that on a computer or laptop browser I want it to show 100% width, but I only want it to show 1/2 of the image if you cut it vertically on tablet and if it's phone I want it to show 1/4 of the image if you cut it vertically. 目标我有一个背景图像,在计算机或笔记本电脑浏览器上,我希望它显示100%的宽度,但我只希望它显示图像的1/2,如果你在平板电脑上垂直切割它,如果它是手机我希望它如果垂直切割,则显示图像的1/4。

What I've Tried Tried using clip-path in CSS but that makes it horizontal scroll enabled which I don't want. 我尝试过的尝试在CSS中使用剪辑路径但是这使得它启用了水平滚动,这是我不想要的。

On browser should look like this 在浏览器上应该是这样的

On Tablet 在平板电脑上

On Phone 在电话上

On tablet and phone the rest of the image should not be visible via scroll. 在平板电脑和手机上,图像的其余部分不应通过滚动显示。 It should only allow vertical scroll and that's simply the background. 它应该只允许垂直滚动,这只是背景。

You can achieve this with media queries and the background-size property. 您可以使用媒体查询和background-size属性来实现此目的。

/* Default (mobile) */
body {
  background: url(https://i.stack.imgur.com/np7c0.png) no-repeat;
  background-size: 400%;
}

/* Tablet and up */
@media only screen and (min-width: 600px) {
  body {
    background-size: 200%;
  }
}

/* Desktop and up */
@media only screen and (min-width: 768px) {
  body {
    background-size: 100%;
  }
}

Here's a demo: https://jsfiddle.net/zxqcgkLp/ 这是一个演示: https//jsfiddle.net/zxqcgkLp/


Note: you may need to play around with your breakpoints, depending on your specific needs. 注意:根据您的具体需求,您可能需要使用断点。 Here's a useful guide from CSS-Tricks that shows common device breakpoints: 这是一个有用的CSS-Tricks指南,它显示了常见的设备断点:

https://css-tricks.com/snippets/css/media-queries-for-standard-devices/ https://css-tricks.com/snippets/css/media-queries-for-standard-devices/

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

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