javascript - 接下来的js覆盖主div

标签 javascript css reactjs sass next.js

我正在学习 React 并使用 Next js 作为框架。

我的元素结构只是遵循教程并使用 next-sass 和 next.config.js

//-- next.config.js
module.exports = withSass({
  sassLoaderOptions: {
    includePaths: ["absolute/path/a", "absolute/path/b"]
  }
})

我想要的是删除页面所有 Angular 上的默认边距。假设我想用 #F5F5F5 覆盖整个页面。我怎样才能实现这个目标?

enter image description here

最佳答案

您需要做两件事:

  1. 如果要将其应用于每个页面,您需要将其设为 a custom App file ,这样您就不必手动将主 .scss 导入到每个页面中。 (重要:请阅读上面链接中有关在 _app.js 文件中定义 getInitialProps 的注释)
  2. 创建一个针对 body 元素的 .scss 文件。默认情况下,它应用了 margin: 8px; 样式 - 您需要覆盖它。然后将此.scss 文件导入_app.js 文件中。

工作代码和框:

Edit NextJS Sass Example


pages/_app.js

import React from "react";
import App from "next/app";
import Head from "next/head";
import "../styles/index.scss";

class MyApp extends App {
  // remove this 'getInitialProps' if you don't plan on render blocking
  // for example, signing a user in before the requested page is loaded
  static async getInitialProps({ Component, ctx }) {
    return {
      pageProps: {
        ...(Component.getInitialProps
          ? await Component.getInitialProps(ctx)
          : {})
      }
    };
  }

  render() {
    const { Component, pageProps } = this.props;
    return (
      <>
        <Head>
          <link rel="icon" href="/favicon.ico" />
        </Head>
        <Component {...pageProps} />
      </>
    );
  }
}

export default MyApp;

styles/index.scss

$foreground: #007ec5;
$background: #ebebeb;

body {
  background: $background;
  color: $foreground;
  padding: 20px;
  margin: 0;
  min-height: 100vh;
}

关于javascript - 接下来的js覆盖主div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59905470/

相关文章:

javascript - 错误处理不适用于 Chrome 中的 HTML5 地理定位

javascript - FB.getLoginStatus() 在控制台调用 FB.init() 错误之前调用

html - CSS Accordion Nav - 如果没有其他人打开,则需要默认树打开

css - 获取2个 float div以覆盖带边框的页面的整个宽度

html - 如何在无滚动 iframe 中重新定位内容?

javascript - 对于通用 JS 和 React,是否有 '@font-face' 的替代方案

javascript - React Mobile 打开时带有按钮,但不是可点击的组件

javascript - 如果当前索引属于特定数组( Angular Js),如何隐藏 div

javascript - 如何在设置为显示 : none 后更改显示属性的值

javascript - Chrome 控制台从变量返回 8 Nan