Utilizing ReactJS allows the <head> HTML tag to become visible when importing CSS/Sass into a React component

As I develop my "Hello World" app using create-react-app, I encountered an issue where importing a css/sass file into my react components resulted in the <style> tag being visible and the styled text appearing on my page background. 😕😕😕😕


I attempted to resolve this by installing several npm modules, but unfortunately it did not help. Here are the modules I tried:

  • sass
  • sass-loader
  • node-sass
  • css-loader
  • style-loader

Here is a snippet of my code for reference:

index.html

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

    <title>React App</title>
  </head>

  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>

    <div id="root"></div>

  </body>

</html>

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
serviceWorker.unregister();

App.js

import React from 'react';
import SimpleForm from "./SimpleForm"

const App = () => {
  return (
    <div>

      {/* nothing changes if I comment this ↓ guy  */}

      <SimpleForm />
    </div>
  );
}

export default App;

and a piece of SimpleForm.js

import React from 'react';
import './SimpleForm.scss';
import Greetings from "./Greetings"

class SimpleForm extends React.Component {

...

    render() {
      const { firstNameError, firstName } = this.state;

      return (
        <div className="main">
            <input
                type="text"
                name="firstName"
                onChange={this.onFirstNameChange}
                onBlur={this.onFirstNameBlur}
            />
            {firstNameError && <div className="error">{firstNameError}</div>}
            <Greetings
                firstName={firstName}
            />
        </div>

  );
}

export default SimpleForm

There is nothing particularly special in SimpleForm.scss/css, so I do not believe it is causing this issue.

(I also tried import './SimpleForm.css';)


The current result looks like this:

But I would expect something more along the lines of this:



UPD_1:

SimpleForm.css (compiled from SimpleForm.scss)

* {
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  align-self: center;
  margin-top: 20px;
  display: block;
}

.main {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #1b224b;
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-content: center;
}

input {
  width: 30%;
  font-size: 40px;
  color: #77c848;
  border: none;
  border-bottom: 2px solid #77c848;
  background: none;
  outline: none;
}

/*# sourceMappingURL=SimpleForm.css.map */


UPD_2

Greetings.js

import React from "react"

const Greetings = ({ firstName, lastName }) => (
    <div>
        Hey you! {firstName} {lastName}!
    </div>
);

export default Greetings;

Answer â„–1

Feel free to share your thoughts on the CSS and remember to include the following code:

body{
 background: #F00;
}

To verify if your CSS file has been imported correctly, try using flex with absolute positioning, as it may be causing some issues.

Answer â„–2

After troubleshooting, I was able to resolve this issue on my own. The problem stemmed from the _share.scss file. Initially, it looked like this:

$primaryColor: #1b224b;
$secondaryColor: #77c848;

* {
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  align-self: center;
  margin-top: 20px;
  display: block;
}

I made the following adjustment:

$primaryColor: #1b224b;
$secondaryColor: #77c848;

* {
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  align-self: center;
}

By removing the display: block style property, everything is now working as intended. It appears that the display: block was inadvertently being applied to the <style> tag due to the use of the universal selector *.

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

Unleashed placement and drifting

Is it possible to style this layout without specifying an exact height for "Element 1"? Code Element1 { positon: relative; width: 100%; height: auto; /* Avoiding specific height */ } Inner1 { position: absolute; top: xyz px; left: xyz px; } Inner2 { po ...

What are the best methods for utilizing rating stars in PHP?

Currently, I am incorporating the star rating system from Rating Stars. I am working on creating a PHP table and dynamically populating it with records. In this table, there will be a column dedicated to displaying these stars. Each record is saved as a n ...

Ensure that a group of checkboxes is mandatory

I currently have a series of checkboxes set up like so: <label>What is your preferred movie genre?</label> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="genre1" name="genre" ...

Callback function located within directive attribute specified in separate attribute

Recently, I created a directive called mySave, and it looks like this: app.directive('mySave', function($http) { return function(scope, element, attrs) { element.bind("click", function() { $http.post('/save', scope.d ...

Using jQuery to substitute a matched word with the each method

Looking for ways to update the text using different methods? Is this correct? It seems like it's not replacing or finding the text accurately. $(function(){ var style_find = ['tab-1','tab-2','rounded-1','rounde ...

How can I customize the offset and achieve a smooth transition to each section using Jquery or JavaScript in Bootstrap scrollspy?

My goal is to customize the scrollspy offset and create a smooth transition between sections without relying on plugins or data attributes. I have a JavaScript function that sets the offset for scrollspy, but I need help adding a smooth animated scroll eff ...

Enhancing the appearance of input and autosuggestions in react-autosuggest while integrating with Material-ui

I'm currently utilizing the react-autosuggest library within my Material-UI component to generate suggestions as the user types. However, I'm encountering difficulties in customizing the styling of both the input field and the suggested text. I ...

Adding a Font Awesome icon on the fly in Vue.js

In my Vue modal, I have two inputs along with a button that adds two more inputs dynamically when clicked. Everything is functional, but I want to include a font awesome icon next to the dynamically created inputs. How can I achieve this in Vue? Here' ...

Help with Material-UI: Passing unique props to a custom TreeItem component

I am trying to pass an argument category to the component CustomTreeItem which uses TreeItemContent. Documentation: https://mui.com/ru/api/tree-item/ import TreeItem, { TreeItemProps, useTreeItem, TreeItemContentProps, } from '@material-ui/lab ...

The Reactjs application has been successfully deployed on a Windows server, but all that is showing is

I have developed a ReactJS application and now I am looking to deploy it on a hosted server. In order to do so, I have made the following changes: Added "homepage": 'https://ww.domain.com/ReactApp' in package.json Installed the react-snapshot pl ...

it results in an error when attempting to deconstruct an object

Using a style object in a component <Temp styles={{fontWeight: 'bold', fontSize: '1.6'}} ...otherprops /> Encountering an error while deconstructing the style object Cannot read property 'fontSize' of undefined. The d ...

Encountering an error with NodeJs, AngularJs, and Mongoose: TypeError - Object.keys invoked on a non-object leading to Function.keys (native)

Here is the code snippet related to the issue: server.js file: app.post('/user', function(req,res){ console.log(req.body); var user = mongoose.Schema('User',req.body); user.save(function(err,user){ if(err) console.l ...

Troubleshooting Issue: Unable to Collapse Bootstrap Responsive Navbar Button with Custom CSS Modifications

I recently created a responsive HTML page using Bootstrap, then converted it to WordPress and made some custom CSS adjustments. In the original HTML version of the page, the navbar collapse button worked perfectly when the screen was resized. However, afte ...

Unlocking new possibilities with Next Auth B2C in React and Next.js

On my journey to implement B2C login with React using Next and the Next Auth library, I encountered an issue when deploying the application. While everything works smoothly in localhost, a strange occurrence unfolds upon deployment. The cookie next-auth. ...

jquery dialog box displaying a webpage

I am looking to implement a feature where, upon clicking the edit link in the last column of my dataTable, a separate page for editing should open within a jQuery dialog box. The goal is to seamlessly submit data while providing a user-friendly experienc ...

Creating a series of tiny vertical markings along the horizontal line using the input range

I have a range input and I am looking to add small vertical lines at intervals of 10 on the horizontal line representing each default step. The range of my input is from 0 to 40 and I would like to have small vertical lines at 10, 20, and 30. I need to ac ...

Encountering error while attempting POST request in POSTMAN - "Unable to modify in restricted editor."

I'm facing a bit of a dilemma here. I can't seem to figure out how to make my editor in Postman stop being read-only. Can anyone lend a hand? Whenever I try to send a Post Request, my editor just won't cooperate and stays in Read-Only mode. ...

Retrieve the list of users playing certain games using the start.gg API

Is there a way to retrieve all users from Smash Ultimate using the start.gg API? I couldn't find any information about fetching all users, only details about tournaments or specific player sets. You can access the API here: Thank you in advance for ...

background gradient coloring and image blending

I have created a special class that includes a gradient background color: .banner { background: -moz-linear-gradient(center top , #75A319 0%, #9FCC1D 100%) repeat scroll 0 0 #9FCC1D; } Additionally, I have defined another class that adds a background ...

Using database entries to generate dynamic routes in express.js without the need for a server restart

I have created a custom Express instance for my KeystoneJS v5 app that serves data stored in a Postgres database with the following model: CREATE TABLE "Link" ( id integer DEFAULT PRIMARY KEY, customer text, slug text ); I've set up dyna ...