将一个溅斑图像放在MDX内容中间,就像MDX博客一样?

huangapple go评论79阅读模式
英文:

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

将一个溅斑图像放在MDX内容中间,就像MDX博客一样?


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

将一个溅斑图像放在MDX内容中间,就像MDX博客一样?


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)的父级位置来分别设置leftright,同时使用负的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>
);

可以调整heightwidth来适应你的需求。对于较小的屏幕,我认为你应该隐藏这部分。

这里是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 = () =&gt; (
  &lt;div className=&quot;relative -z-10&quot;&gt;
    &lt;div className=&quot;absolute bg-cover left-[100%] h-96 w-[500px] bg-[url(&#39;https://i.stack.imgur.com/J0ik2.jpg&#39;)]&quot;&gt;&lt;/div&gt;
    &lt;div className=&quot;absolute bg-cover right-[100%] h-96 w-[500px] bg-[url(&#39;https://i.stack.imgur.com/J0ik2.jpg&#39;)]&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
);

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.

huangapple
  • 本文由 发表于 2023年5月10日 13:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定