英文:
how do i put a splatter image in between mdx content like a mdx blog?
问题
pages/index.tsx
<main className="flex min-h-screen dark:bg-black">
<div className="wrapper mb-24 gap-8 px-8 lg:grid lg:grid-cols-[1fr,70px,min(70ch,calc(100%-64px)),70px,1fr]">
<div className="mdx-wrapper prose relative col-[2/5] max-w-full dark:prose-light lg:grid lg:grid-cols-[70px_1fr_70px]">
<MDXLayoutRenderer mdxSource={props.code as never} />
</div>
</div>
</main>
index.mdx
this is a title
import { Splatter } from '../../components/Splatter.tsx'
this is a block
some gibberish
some gibberish
some gibberish
some gibberish
<Splatter />
yet another block
some gibberish
some gibberish
some gibberish
some gibberish
components/Splatter.tsx
export const Splatter = () => (
<div className="relative inset-0 h-64 w-64 bg-[url('/splatter.jpg')]">
</div>
)
public/splatter.jpg
for some reason, the splatter either overlaps the text in index.mdx
or it acts like a div
, i.e, it pushes down the yet another block
i want the splatter to be in the background like a background image.
i'm not sure how to do it? i did try using an img
but it didn't give the desired result.
my current method of bg-[url('/splatter.jpg')]
is just a background image in tailwind. it doesn't work either.
i tried using z-index & different positions absolute & relative but it doesn't go in the background.
what's the fix to this?
英文:
i have an mdx blog that renders a blog post.
pages/index.tsx
<main className="flex min-h-screen dark:bg-black">
<div className="wrapper mb-24 gap-8 px-8 lg:grid lg:grid-cols-[1fr,70px,min(70ch,calc(100%-64px)),70px,1fr]">
<div className="mdx-wrapper prose relative col-[2/5] max-w-full dark:prose-light lg:grid lg:grid-cols-[70px_1fr_70px]">
<MDXLayoutRenderer mdxSource={props.code as never} />
</div>
</div>
</main>
the MDXLayoutRenderer
is where the mdx is inserted in the page.
index.mdx
this is a title
import { Splatter } from '../../components/Splatter.tsx'
this is a block
some giberrish
some giberrish
some giberrish
some giberrish
<Splatter />
yet another block
some giberrish
some giberrish
some giberrish
some giberrish
components/Splatter.tsx
export const Splatter = () => (
<div className="relative inset-0 h-64 w-64 bg-[url('/splatter.jpg')]">
</div>
)
public/splatter.jpg
for some reason, the splatter either overlaps the text in index.mdx
or it acts like a div
, i.e, it pushes down the yet another block
i want the splatter to be in the background like a background image.
i'm not sure how to do it? i did try using an img
but it didn't give the desired result.
my current method of bg-[url('/splatter.jpg')]
is just a background image in tailwind. it doesn't work either.
i tried using z-index & different positions absolute & relative but it doesn't go in the background.
what's the fix to this?
答案1
得分: 1
你可以尝试使用两个图像,将它们设置为绝对定位(absolute
),并且使用相对定位(relative
)的父级位置来分别设置left
和right
,同时使用负的z-index
。类似这样的代码:
export const Splatter = () => (
<div className="relative -z-10">
<div className="absolute bg-cover left-[100%] h-96 w-[500px] bg-[url('https://i.stack.imgur.com/J0ik2.jpg')]"></div>
<div className="absolute bg-cover right-[100%] h-96 w-[500px] bg-[url('https://i.stack.imgur.com/J0ik2.jpg')]"></div>
</div>
);
可以调整height
和width
来适应你的需求。对于较小的屏幕,我认为你应该隐藏这部分。
这里是CodeSandbox示例链接。
英文:
You can try with two images, position them absolute
with left
and right
with parent position relative
and negative z-index
.
Something like this:
export const Splatter = () => (
<div className="relative -z-10">
<div className="absolute bg-cover left-[100%] h-96 w-[500px] bg-[url('https://i.stack.imgur.com/J0ik2.jpg')]"></div>
<div className="absolute bg-cover right-[100%] h-96 w-[500px] bg-[url('https://i.stack.imgur.com/J0ik2.jpg')]"></div>
</div>
);
Play around with height
and width
to get what suits you. For smaller screen I think you should hide this.
Here is the codesandbox example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论