reactjs - 如何将 next/image 中的 Image 组件放在位置 : absolue

标签 reactjs typescript next.js styled-components nextjs-image

我可以在 { position: absolute; 中放置来自 'next/image' 的元素图像吗?左:50% ? 好像不受影响。

例子:

import React from 'react'
import Image from 'next/image'
import styled from 'styled-components'

const Container = styled.div`
  position: relative;
`

const Test = styled(Image)`
 position: absolute;
 left: 50%;
`

const Page: React.FC = () => {
  return (
    <Container>
      <Test src{someImg.webp} width={500} height={500} objectFit="none" />
    </Container>
  )
}

export default Page;

最佳答案

将您的图像放在您喜欢的位置的另一个父 div 中。 在你的情况下:

import React from 'react'
import Image from 'next/image'
import styled from 'styled-components'

const Container = styled.div`
  position: relative;
`
 const ImageContainer = styled.div`
  position: absolute;
  left: 50%;
`


const Page: React.FC = () => {
  return (
    <Container>
     <ImageContainer>
        <Image src{someImg.webp} width={500} height={500} objectFit="none"/>
     </ImageContainer>
    </Container>
  )
}

export default Page;

关于reactjs - 如何将 next/image 中的 Image 组件放在位置 : absolue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66643887/

相关文章:

javascript - react : Getting runtime error/warning in browser when a component is rendered

javascript - React Native FlatList keyboardShouldPersistTaps 不持久

css - 背景隐藏行边框?

typescript - 如何使用 typescript 将对象插入数组

typescript - 值更改时的条件对象类型

angular - IntelliJ 错误地建议在 Angular 应用程序中将方法设为静态

reactjs - 在 nextjs 中,对象类型从服务器端更改为客户端

iframe - 将 iframe 插入到 React 组件中

single-page-application - Next.JS单页应用程序?

javascript - 如何使用 react-responsive-carousel 和 react-image-magnifiers 映射自定义缩略图的值?