简体   繁体   English

Next.JS 中使用 SASS 的背景图像

[英]Background image in Next.JS using SASS

I've been trying to get this background image into a project but it never shows.我一直在尝试将此背景图像放入一个项目中,但它从未显示过。 No errors indicating there's a problem.没有错误表明存在问题。 I'm using Next.JS and all the other images through out the project load fine.我在整个项目加载过程中使用 Next.JS 和所有其他图像。 To test I changed the color of the background and it worked fine.为了测试,我改变了背景的颜色,效果很好。

My SASS file looks like:我的 SASS 文件看起来像:

@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@900&display=swap");
$primary-color: #24b4a8;
$secondary-color: #b3b5b5;
$background-color: #2d2f31;
$third-color: #222426;
$main-font: Montserrat;
$secondary-font: Raleway;

body {
  width: 100%;
  overflow-x: hidden;
  margin: 0;

  a {
    text-decoration: none;
  }
}

html .container {
  scroll-snap-type: y mandatory;
  overflow-y: scroll;
  scroll-behavior: smooth;
}

section {
  padding: 7em;
  width: calc(100% - 4em);
  height: calc(100vh - 14em);
  scroll-snap-align: center;

  &:nth-of-type(1) {
    background-image: url("/public/background.svg");
    display: grid;
    grid-template-columns: repeat(6, 15%);
    grid-template-rows: repeat(6, 15%);

    .intro {
      display: flex;
      flex-direction: column;
      align-items: center;
      grid-column-start: 1;
      grid-column-end: 4;
      padding-top: 3em;

      h2 {
        font-size: 2em;
        font-family: "Courier New", Courier, monospace;
        color: $secondary-color;
      }

      h3 {
        font-size: 2.75em;
        color: $secondary-color;
        font-family: "Montserrat", sans-serif;
        font-weight: 900;
      }
    }

    h1 {
      font-family: "Montserrat", sans-serif;
      font-weight: 900;
      grid-column-start: 1;
      grid-row-start: 3;
      font-size: 20.25em;
      width: 100%;
      margin-left: -50%;
      padding-top: 17%;
    }
    .profile {
      display: flex;
      flex-direction: column;
      grid-column-start: 5;
      grid-row-start: 2;
      button {
        color: $primary-color;
      }

      img {
        size: 2em;
      }
    }
  }
  &:nth-of-type(2) {
    background: $secondary-color;

    .test-title {
      font-family: "Montserrat", sans-serif;
      font-weight: 900;
      font-size: 3em;
      margin: 90px auto;
    }

    .quotes {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin: 90px auto;
      max-width: 700px;

      p {
        text-align: center;
        margin-bottom: 20px;
        color: #45454d;
        font-size: 50em;
        margin-top: 25px;
        font-size: 2em;
      }

      blockquote {
        font-size: 1.75em;
        font-family: "raleway", sans-serif;
        font-weight: 900;
        color: $primary-color;
      }
    }

    .quote {
      display: flex;

      span {
        height: 20px;
        width: 20px;
        margin: 30 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
      }
      span::before {
        content: "";
        height: 6px;
        width: 6px;
        background-color: #d4d4d4;
        border-radius: 50%;
        transition: background-color 0.3s ease;
      }
      span:hover::before {
        background-color: #45454d;
      }

      span[data-quote="${active}"]::before {
        background-color: #45454d;
      }
    }

    blockquote::before {
      content: "\201c";
      font-size: 4em;
      display: block;
      margin-bottom: -40px;
      margin-top: -40px;
    }
  }
  &:nth-of-type(3) {
    background: $background-color;
  }
}

My next.config.js file looks like:我的 next.config.js 文件如下所示:

const withImages = require('next-images')
const withSass = require('@zeit/next-sass')
module.exports = withSass(withImages({
  webpack (config, options) {
    config.module.rules.push({
      test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
      use: {
        loader: 'url-loader',
        options: {
          limit: 100000
        }
      }
    })
    return config
  }
}))

I've tried using background: URL("background.svg");我试过使用背景: URL("background.svg"); background: url("background.png");背景: url("background.png"); background: url(/public/background.png);背景:网址(/public/background.png); none of these return the image or an error.这些都不返回图像或错误。

Any help is greatly appreciated, and thanks in advance!非常感谢任何帮助,并提前致谢!

An official support had been provided by NEXT.JS Two steps are required. NEXT.JS 已经提供官方支持 需要两个步骤。

1- Wrap the Image component with div which has styles (will provide example). 1- 用具有样式的 div 包裹 Image 组件(将提供示例)。

2- Provide the Image component with the attributes as mentioned in the Example. 2- 为 Image 组件提供示例中提到的属性。

Demo: https://image-component.nextjs.gallery/background演示: https : //image-component.nextjs.gallery/background

Documentation: https://nextjs.org/docs/api-reference/next/image文档: https : //nextjs.org/docs/api-reference/next/image

Code: https://github.dev/vercel/next.js/blob/canary/examples/image-component/pages/background.js代码: https : //github.dev/vercel/next.js/blob/canary/examples/image-component/pages/background.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM