Guide to using normalize.css via npm installation with webpack

Currently, I am utilizing webpack in conjunction with ReactJS and am exploring the implementation of normalize.css after installing it via npm ().

Does the normalize.css take effect immediately upon npm installation? If I were to make modifications to it, how would I go about doing so?

Answer №1

One way to incorporate the npm-installed normalize.css in React is as follows:

import React from 'react';
import ReactDOM from 'react-dom';

import 'normalize.css'; // Add this line

const root = document.getElementById('root');

ReactDOM.render(<h1>Hello, World!</h1>, root);

After implementation, the text will now display like this:

https://i.sstatic.net/pF9BH.png

Take note that the text styling is courtesy of normalize.css.

To make it function properly, you should have a setup similar to the following:


1) Insert the provided JavaScript code into index.js

2) Add normalize.css (and other dependencies) to package.json:

{
    "dependencies": {
        "normalize.css": "^5.0.0",
        "react": "^16.3.2",
        "react-dom": "^16.3.2"
    },
    "devDependencies": {
        "babel-core": "^6.26.3",
        "babel-loader": "^7.1.4",
        "babel-preset-env": "^1.7.0",
        "babel-preset-react": "^6.24.1",
        "css-loader": "^0.28.11",
        "style-loader": "^0.21.0",
        "webpack-dev-server": "^3.1.4",
        "webpack": "^4.8.3"
    }
}

3) Ensure the correct loaders are utilized in webpack.config.js:

module.exports = {
    mode: 'development',
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                options: { presets: ['env', 'react'] }
            },
            {
                test: /\.css$/,
                use: [{ loader: 'style-loader' }, { loader: 'css-loader' }]
            }
        ]
    }
};

4) Integrate an index.html file to view the changes:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <div id="root"></div>
  <script src="main.js"></script>
</body>
</html>

5) Install all dependencies by running:

npm install

6) Initiate the Webpack devserver with the command:

./node_modules/.bin/webpack-dev-server --open

NOTE: This guide is based on version 5.0.0 of normalize.css. Using version 6.0.0 or higher will result in a different font style, as the opinionated rules were removed from normalize.css in that version.


Update 17/5/2018: Updated instructions to incorporate Webpack 4 and React 16.

Answer №2

To solve the issue with WebPack 4 not importing normalize.less, try using normalize.css instead.

@import "../node_modules/normalize.css/normalize.css";

Here are the rules to include in your module:

module: {
    rules: [{
            test: /\.css$/,
            use: [MiniCssExtractPlugin.loader,
                "css-loader"
            ]
        },
        {
            test: /\.less$/,
            use: [
                MiniCssExtractPlugin.loader,
                "css-loader",
                "less-loader"
            ]
        }
    ]
};

Answer №3

Within index.css:

Importing the normalize.css file:

Answer №4

When you use the import or require function, Webpack will automatically include the file unless specified otherwise. For instance:

Please note that I am working with Webpack 2.

module: {
    rules: [ // changed from 'loaders' to 'rules'
        {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
        },
        {
            test: /\.sass$/,
            exclude: /node_modules/,
            loader: ExtractTextPlugin.extract({
              fallbackLoader: 'style-loader',
              loader: ['style-loader','sass-loader']
            })
        }
    ]
}

The use of the exclude property will handle this.

For instance:

// public/assets/style.scss
@import 'substyle1';
@import 'substyle1';

body {
  background-color: red;
}
// src/index.js -> entry file
import '../public/assets/style.scss';
// Webpack will recognize that you are importing your SCSS / SASS file

I hope this explanation is beneficial.

Answer №5

To start, you can either install or download normalize.css from GitHub. My recommendation would be to download it for easier access. Once you have it, there are two primary ways to utilize it effectively.

Method 1: Use normalize.css as a foundation for your project's base CSS, adjusting the values as needed to align with the design specifications.

Method 2: Simply include normalize.css as is and then build upon it, making changes to the defaults in your CSS as required.

For instance, all you need to do is place the downloaded files into your project folder and link to it using a link tag:

link rel="stylesheet" type="text/css" href="normalize.css"

IMPORTANT: Make sure that the href content points to the correct folder where normalize is located.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating Pop-up Dialog Boxes in WebForms Using jQuery

I have implemented a Plugin in Webforms using jQuery UI dialog. Since I have a Content Place holder on my Form, I had to modify the code as shown below: <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <link rel="stylesheet" ...

Is there a way to programmatically access the title of a TitledPane in JavaFX without using CSS?

Is there a way to set the title of a TitledPane to bold in Java code without using CSS? I created a TitledPane and I want the title's font weight to be bold. This is what I tried: TitledPane Rootpane = new TitledPane(); Rootpane.setText("Options"); ...

How can you retrieve the property value from an object stored in a Set?

Consider this scenario: SomeItem represents the model for an object (which could be modeled as an interface in Typescript or as an imaginary item with the form of SomeItem in untyped land). Let's say we have a Set: mySet = new Set([{item: SomeItem, s ...

Is there a way for me to extract information from the subsequent pages of the posts I've already collected?

My journey into programming started nearly a year ago, and I consider myself a novice in the field. Currently, I am engrossed in developing a Django web application that scrapes posts and then navigates through the hyperlink of each post to extract the bod ...

Data input not populating in email

After filling out the form, Gmail pops up with no data entered. How can I ensure that the information I input in the form is transferred to the email? <form class="" action="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_em ...

Utilizing useEffect to retrieve and display an empty array in a React component

I am currently developing a React application that leverages Fetch to retrieve data from an API using SQLite. Strangely, when I check the console, it only displays a length of 3 and Array[0]. This means I can't access data based on specific IDs like I ...

What could be the reason for the malfunctioning of the Yarn package manager installation?

After successfully installing yarn with the command npm install --global yarn, I encountered an issue when trying to use it. The system displayed the message: The term 'yarn' is not recognized as the name of a cmdlet, function, script file, o ...

At what point is it appropriate for me to delete the token?

Seeking Answers: Token Dilemmas Is it best to create the token upon user login and registration, or just on login? Should the token be saved in local storage? Do I need to send the token after every user request? Should the token o ...

Tips for connecting a Django API project with a nodejs and react frontend

I'm currently working on a Django API project and I am considering incorporating Node.js into the mix. Additionally, I am interested in using React for the frontend of the application. Is this combination of technologies feasible? Would it be advisabl ...

The side modal features a full-height design with headers, bodies, and footers that are

I have created a side modal that extends to full height. The content in the modal body is quite lengthy, but I am facing an issue where the header, body, and footer are not equal in size. I am considering using scrollspy on the HTML body tag to potentially ...

Acquiring inner HTML content with PHP

Looking to optimize meta tags for search and open graph. My goal is to dynamically generate the meta description tag by combining content from two specific elements in the HTML. For instance, let's say we have this snippet of code: <div class="r ...

Ways to divide the header column of a bootstrap 4 table?

Currently, I am delving into the realm of frontend development and facing a challenge with designing a table. My goal is to merge multiple columns into a single column by dividing them into cells. I am aware of the bootstrap classes colspan and rowspan, ho ...

What is the CSS method for altering the color of a slider's runnable track upon selection?

Seeking assistance in changing the slider track color upon selection. Struggling to achieve the desired outcome of altering the color as it slides. CSS: /* Custom Styles */ .text-size-slider { line-height: 100%; font-size: 14px; position: relative ...

"Step-by-Step Guide: Implementing a Modal Popup Form in Angular with NgBootstrap and FormsModule

I am seeking assistance with my Angular project. I am attempting to implement a Modal Popup Form using NgBootstrap and FormsModule, but encountering issues with the NgbModule not being imported. Here are some details of my setup: node 16.15.1 cli 15.1.5 co ...

What's the best way to update the fill color of an SVG dynamically?

Is it possible to change the color of an SVG image dynamically without using inline SVG tags? I want to create a code that allows users to specify the source for the SVG tag and a hexadecimal color value, enabling them to customize any SVG image with their ...

Tips for transferring an array from a php page to a javascript page

In my PHP code, I have an array structured like this: 1) <body onload="addMarkers(<?php print_r($myArray) ?>)"> 2) <body onload="addMarkers(<?php echo json_encode($myArray) ?>)"> Unfortunately, these attempts were unsuccessful. U ...

A Typescript Function for Generating Scalable and Unique Identifiers

How can a unique ID be generated to reduce the likelihood of overlap? for(let i = 0; i < <Arbitrary Limit>; i++) generateID(); There are several existing solutions, but they all seem like indirect ways to address this issue. Potential Solu ...

CSS flexbox completely collapses all child elements that are empty

Is there a way to collapse the empty "col" divs in this html structure while keeping the content divs equally sized (two columns in this case)? * { box-sizing: border-box; } .container { border: 1px dashed rgba(255, 0, 0, .5); background-color: b ...

Refreshing page using jQuery following an AJAX request

My script is functioning properly, however the use of location.reload(); does not seem to be effective. I am aiming to refresh the page after deselecting the last checkbox. $(".catcf, .coursetype, .yearcf, .manthcf, .venucf").on('click' ...

The issue with ng-bind-html causing the disappearance of paragraph spaces

Is there a way to preserve paragraph breaks when using ng-bind-html to display text on the screen? I am having an issue where all the text appears as one big chunk if there are paragraph breaks in the data pulled from the database. Not sure what is causing ...