2

我可以将“下一个/图像”中的元素图像放入{ position: absolute; left: 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;
4

1 回答 1

3

将您的图像放在您喜欢的位置的另一个父 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;
于 2021-03-15T19:07:32.140 回答