简体   繁体   English

裁剪背景图像并缩放为 window 调整大小

[英]crop background image and scale as window resizes

I am currently attempting to create a hero image on the home page of my react application.我目前正在尝试在我的反应应用程序的主页上创建一个英雄图像。 The issue I am running into is how the image currently sits.我遇到的问题是图像当前的位置。
在此处输入图像描述

currently the image sits a bit more centered than I would like, I am trying to duplicate something like this(please ignore the card in front of the background image):目前图像比我想要的更居中,我试图复制这样的东西(请忽略背景图像前面的卡片):

在此处输入图像描述

I am unsure of how to position the background image further to the right and down a bit without there being white-space or it looking wonky on larger screens.我不确定如何将背景图像进一步向右和向下一点,而没有空白或在更大的屏幕上看起来很不稳定。 I would also like the image to translate to the left as a screen is resized.我还希望图像在调整屏幕大小时向左平移。 The code I have attempted is as follows:我尝试的代码如下:

JSX JSX

import React from 'react'

const Home = () => {
  return (
    <div className='h-o'>
      <div className='h-i'></div>
    </div>
  )
}

export default Home

CSS: CSS:

.h-o {
  top: 181px;
  left: -44px;
  width: 100%;
  height: 644px;
  background: transparent url('../../Assets/Images/miami-courthouse.jpg') 0% 0%
    no-repeat padding-box;
  opacity: 1;
}

attached is a code sandbox https://codesandbox.io/s/keen-montalcini-plwgc?file=/src/styles.css附件是一个代码沙箱https://codesandbox.io/s/keen-montalcini-plwgc?file=/src/styles.css

To achieve what you want, you need to set transform-origin: top left for scale top left corner and set parent div as overflow: hidden;要实现您想要的,您需要设置transform-origin: top left以缩放左上角并将父 div 设置为overflow: hidden; to keep it size保持它的大小

 .img-wrapper { display: inline-block; overflow: hidden; } img.resize { transform-origin: top left; transform:scale(1.1); -ms-transform:scale(1.1); /* IE 9 */ -moz-transform:scale(1.1); /* Firefox */ -webkit-transform:scale(1.1); /* Safari and Chrome */ -o-transform:scale(1.1); /* Opera */ }
 <div class="img-wrapper"> <img src="https://i.imgur.com/aDExJLE.png" /> </div> <div class="img-wrapper"> <img class="resize" src="https://i.imgur.com/aDExJLE.png" /> </div>

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

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