背景元素使用tailwind css在React-router-dom中覆盖布局。

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

Background element overlays the Layout in React-router-dom with tailwind css

问题

I moved all background styles to it's own element and wrap it the whole Layout.

import { Outlet } from 'react-router-dom';
import Background from '../components/Background';
import Footer from '../components/Footer';
import NewNavbar from '../components/NewNavbar';

export const AppLayout = () => (
    <div>
        <Background>
            <nav className=''>
                <NewNavbar />
            </nav>
            <main className=''>
                <Outlet />
            </main>
            <footer>
                <Footer />
            </footer>
        </Background>
    </div>
);

So, using this brings me only visible Background without elements. If I remove the wrapper I would see the elements but lost my Background. What should I do?

I know that every small piece of code that has another functionality should move to its own element, so I did this. Am I right?

This is how the Background element looks inside:

import React from 'react';

const Background = () => {
    return (
        <div>
            <div className='bg-white'>
                <div className='flex min-h-screen justify-between flex-col absolute inset-x-0 top-[-10rem] z-10 transform-gpu overflow-hidden blur-3xl sm:top-[-20rem]'>
                    <svg
                        className='relative left-[calc(50%-11rem)] -z-10 h-[21.1875rem] max-w-none -translate-x-1/2 rotate-[30deg] sm:left-[calc(50%-30rem)] sm:h-[42.375rem]'
                        viewBox='0 0 1155 678'>
                        <path
                            fill='url(#45de2b6b-92d5-4d68-a6a0-9b9b2abad533)'
                            fillOpacity='.3'
                            d='M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z'
                        />
                        <defs>
                            <linearGradient
                                id='45de2b6b-92d5-4d68-a6a0-9b9b2abad533'
                                x1='1155.49'
                                x2='-78.208'
                                y1='.177'
                                y2='474.645'
                                gradientUnits='userSpaceOnUse'>
                                <stop stopColor='#9089FC' />
                                <stop offset='1' stopColor='#FF80B5' />
                            </linearGradient>
                        </defs>
                    </svg>
                </div>
            </div>
            <div className='inset-x-0 top-[calc(100%-13rem)] -z-10 transform-gpu overflow-hidden blur-3xl sm:top-[calc(100%-30rem)]'>
                <svg
                    className='relative left-[calc(50%+3rem)] h-[21.1875rem] max-w-none -translate-x-1/2 sm:left-[calc(50%+36rem)] sm:h-[42.375rem]'
                    viewBox='0 0 1155 678'>
                    <path
                        fill='url(#ecb5b0c9-546c-4772-8c71-4d3f06d544bc)'
                        fillOpacity='.3'
                        d='M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z'
                    />
                    <defs>
                        <linearGradient
                            id='ecb5b0c9-546c-4772-8c71-4d3f06d544bc'
                            x1='1155.49'
                            x2='-78.208'
                            y1='.177'
                            y2='474.645'
                            gradientUnits='userSpaceOnUse'>
                            <stop stopColor='#9089FC' />
                            <stop offset='1' stopColor='#FF80B5' />
                        </linearGradient>
                    </defs>
                </svg>
            </div>
        </div>
    );
};

export default Background;

Thanks for your help!

英文:

I moved all background styles to it's own element and wrap it the whole Layout.

import { Outlet } from &#39;react-router-dom&#39;;
import Background from &#39;../components/Background&#39;;
import Footer from &#39;../components/Footer&#39;;
import NewNavbar from &#39;../components/NewNavbar&#39;;

export const AppLayout = () =&gt; (
    &lt;div&gt;
        &lt;Background&gt;
            &lt;nav className=&#39;&#39;&gt;
                &lt;NewNavbar /&gt;
            &lt;/nav&gt;
            &lt;main className=&#39;&#39;&gt;
                &lt;Outlet /&gt;
            &lt;/main&gt;
            &lt;footer&gt;
                &lt;Footer /&gt;
            &lt;/footer&gt;
        &lt;/Background&gt;
    &lt;/div&gt;
);

So, using this brings me only visible Background without elements. If I remove the wrapper I would see the elements but lost my Background. What should I do?

I know that every small piece of code that has another functionality should move to its own element, so I did this. Am I right?

This is how the Background element looks inside:

import React from &#39;react&#39;;

const Background = () =&gt; {
    return (
        &lt;div&gt;
            &lt;div className=&#39;bg-white&#39;&gt;
                &lt;div className=&#39;flex min-h-screen justify-between flex-col absolute inset-x-0 top-[-10rem] z-10 transform-gpu overflow-hidden blur-3xl sm:top-[-20rem]&#39;&gt;
                    &lt;svg
                        className=&#39;relative left-[calc(50%-11rem)] -z-10 h-[21.1875rem] max-w-none -translate-x-1/2 rotate-[30deg] sm:left-[calc(50%-30rem)] sm:h-[42.375rem]&#39;
                        viewBox=&#39;0 0 1155 678&#39;&gt;
                        &lt;path
                            fill=&#39;url(#45de2b6b-92d5-4d68-a6a0-9b9b2abad533)&#39;
                            fillOpacity=&#39;.3&#39;
                            d=&#39;M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z&#39;
                        /&gt;
                        &lt;defs&gt;
                            &lt;linearGradient
                                id=&#39;45de2b6b-92d5-4d68-a6a0-9b9b2abad533&#39;
                                x1=&#39;1155.49&#39;
                                x2=&#39;-78.208&#39;
                                y1=&#39;.177&#39;
                                y2=&#39;474.645&#39;
                                gradientUnits=&#39;userSpaceOnUse&#39;&gt;
                                &lt;stop stopColor=&#39;#9089FC&#39; /&gt;
                                &lt;stop offset={1} stopColor=&#39;#FF80B5&#39; /&gt;
                            &lt;/linearGradient&gt;
                        &lt;/defs&gt;
                    &lt;/svg&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div className=&#39;inset-x-0 top-[calc(100%-13rem)] -z-10 transform-gpu overflow-hidden blur-3xl sm:top-[calc(100%-30rem)]&#39;&gt;
                &lt;svg
                    className=&#39;relative left-[calc(50%+3rem)] h-[21.1875rem] max-w-none -translate-x-1/2 sm:left-[calc(50%+36rem)] sm:h-[42.375rem]&#39;
                    viewBox=&#39;0 0 1155 678&#39;&gt;
                    &lt;path
                        fill=&#39;url(#ecb5b0c9-546c-4772-8c71-4d3f06d544bc)&#39;
                        fillOpacity=&#39;.3&#39;
                        d=&#39;M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z&#39;
                    /&gt;
                    &lt;defs&gt;
                        &lt;linearGradient
                            id=&#39;ecb5b0c9-546c-4772-8c71-4d3f06d544bc&#39;
                            x1=&#39;1155.49&#39;
                            x2=&#39;-78.208&#39;
                            y1=&#39;.177&#39;
                            y2=&#39;474.645&#39;
                            gradientUnits=&#39;userSpaceOnUse&#39;&gt;
                            &lt;stop stopColor=&#39;#9089FC&#39; /&gt;
                            &lt;stop offset={1} stopColor=&#39;#FF80B5&#39; /&gt;
                        &lt;/linearGradient&gt;
                    &lt;/defs&gt;
                &lt;/svg&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    );
};

export default Background;

Thanks for your help!

答案1

得分: 1

你需要将Background组件的子元素作为props传递,并适当地渲染它们。否则,React不会对它们做任何处理。

类似以下的代码将起作用:

const Background = ({ children }) => {
  return (
    <div className="relative">
      <div className="absolute top-0 left-0 h-full w-full">
        <div className="bg-white">
          <div className="flex min-h-screen justify-between flex-col absolute inset-x-0 top-[-10rem] z-10 transform-gpu overflow-hidden blur-3xl sm:top-[-20rem]">
            <svg
              className="relative left-[calc(50%-11rem)] -z-10 h-[21.1875rem] max-w-none -translate-x-1/2 rotate-[30deg] sm:left-[calc(50%-30rem)] sm:h-[42.375rem]"
              viewBox="0 0 1155 678"
            >
              <path
                fill="url(#45de2b6b-92d5-4d68-a6a0-9b9b2abad533)"
                fillOpacity=".3"
                d="M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z"
              />
              <defs>
                <linearGradient
                  id="45de2b6b-92d5-4d68-a6a0-9b9b2abad533"
                  x1="1155.49"
                  x2="-78.208"
                  y1=".177"
                  y2="474.645"
                  gradientUnits="userSpaceOnUse"
                >
                  <stop stopColor="#9089FC" />
                  <stop offset="1" stopColor="#FF80B5" />
                </linearGradient>
              </defs>
            </svg>
          </div>
        </div>
        <div className="inset-x-0 top-[calc(100%-13rem)] -z-10 transform-gpu overflow-hidden blur-3xl sm:top-[calc(100%-30rem)]">
          <svg
            className="relative left-[calc(50%+3rem)] h-[21.1875rem] max-w-none -translate-x-1/2 sm:left-[calc(50%+36rem)] sm:h-[42.375rem]"
            viewBox="0 0 1155 678"
          >
            <path
              fill="url(#ecb5b0c9-546c-4772-8c71-4d3f06d544bc)"
              fillOpacity=".3"
              d="M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z"
            />
            <defs>
              <linearGradient
                id="ecb5b0c9-546c-4772-8c71-4d3f06d544bc"
                x1="1155.49"
                x2="-78.208"
                y1=".177"
                y2="474.645"
                gradientUnits="userSpaceOnUse"
              >
                <stop stopColor="#9089FC" />
                <stop offset="1" stopColor="#FF80B5" />
              </linearGradient>
            </defs>
          </svg>
        </div>
      </div>
      {children}
    </div>
  );
};

我添加了一个最外层的div,设置了position: relative,这样子div可以使用position: absolute; top: 0; left: 0; height: 100%; width: 100%;,以便它保持在内容的后面。

英文:

You need to pass the children elements of the Background component as props and render them appropriately. Otherwise React will not do anything with them.

Something like this would work:

const Background = ({ children }) =&gt; {
  return (
    &lt;div className=&quot;relative&quot;&gt;
      &lt;div className=&quot;absolute top-0 left-0 h-full w-full&quot;&gt;
        &lt;div className=&quot;bg-white&quot;&gt;
          &lt;div className=&quot;flex min-h-screen justify-between flex-col absolute inset-x-0 top-[-10rem] z-10 transform-gpu overflow-hidden blur-3xl sm:top-[-20rem]&quot;&gt;
            &lt;svg
              className=&quot;relative left-[calc(50%-11rem)] -z-10 h-[21.1875rem] max-w-none -translate-x-1/2 rotate-[30deg] sm:left-[calc(50%-30rem)] sm:h-[42.375rem]&quot;
              viewBox=&quot;0 0 1155 678&quot;
            &gt;
              &lt;path
                fill=&quot;url(#45de2b6b-92d5-4d68-a6a0-9b9b2abad533)&quot;
                fillOpacity=&quot;.3&quot;
                d=&quot;M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z&quot;
              /&gt;
              &lt;defs&gt;
                &lt;linearGradient
                  id=&quot;45de2b6b-92d5-4d68-a6a0-9b9b2abad533&quot;
                  x1=&quot;1155.49&quot;
                  x2=&quot;-78.208&quot;
                  y1=&quot;.177&quot;
                  y2=&quot;474.645&quot;
                  gradientUnits=&quot;userSpaceOnUse&quot;
                &gt;
                  &lt;stop stopColor=&quot;#9089FC&quot; /&gt;
                  &lt;stop offset={1} stopColor=&quot;#FF80B5&quot; /&gt;
                &lt;/linearGradient&gt;
              &lt;/defs&gt;
            &lt;/svg&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div className=&quot;inset-x-0 top-[calc(100%-13rem)] -z-10 transform-gpu overflow-hidden blur-3xl sm:top-[calc(100%-30rem)]&quot;&gt;
          &lt;svg
            className=&quot;relative left-[calc(50%+3rem)] h-[21.1875rem] max-w-none -translate-x-1/2 sm:left-[calc(50%+36rem)] sm:h-[42.375rem]&quot;
            viewBox=&quot;0 0 1155 678&quot;
          &gt;
            &lt;path
              fill=&quot;url(#ecb5b0c9-546c-4772-8c71-4d3f06d544bc)&quot;
              fillOpacity=&quot;.3&quot;
              d=&quot;M317.219 518.975L203.852 678 0 438.341l317.219 80.634 204.172-286.402c1.307 132.337 45.083 346.658 209.733 145.248C936.936 126.058 882.053-94.234 1031.02 41.331c119.18 108.451 130.68 295.337 121.53 375.223L855 299l21.173 362.054-558.954-142.079z&quot;
            /&gt;
            &lt;defs&gt;
              &lt;linearGradient
                id=&quot;ecb5b0c9-546c-4772-8c71-4d3f06d544bc&quot;
                x1=&quot;1155.49&quot;
                x2=&quot;-78.208&quot;
                y1=&quot;.177&quot;
                y2=&quot;474.645&quot;
                gradientUnits=&quot;userSpaceOnUse&quot;
              &gt;
                &lt;stop stopColor=&quot;#9089FC&quot; /&gt;
                &lt;stop offset={1} stopColor=&quot;#FF80B5&quot; /&gt;
              &lt;/linearGradient&gt;
            &lt;/defs&gt;
          &lt;/svg&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      {children}
    &lt;/div&gt;
  );
};

I added an outermost div with position: relative, so the child div can be position: absolute; top: 0; left: 0; height: 100%; width: 100%; so it stays behind the content.

huangapple
  • 本文由 发表于 2023年2月16日 18:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471212.html
匿名

发表评论

匿名网友

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

确定