I am trying to use my custom CSS library for my components in NextJS. In my components, I want to import my custom CSS file but it's not working.
import React from 'react'
import '../../style/custom.module.css'
function Footer() {
return (
<div className="a b">
</div>
)
}
export default Footer
My custom CSS file is inside the
style/custom.module.css
I have seen the nextJS documentation their they mentioned that in the NextJS version the custom CSS style is supported by default
You are using css module you have to import diffrently
import styles from '../../style/custom.module.css'
function Footer() {
return (
<div className={styles.yourcssclassname}>
</div>
)
}
export default Footer
Firstly,
-import your css module to the main module.
and then pass it to the page that requires the css styling.
You can make a custom Styled component by importing a styled component from #emotion/styled and use this styled component to give styling by making custom components for a particular tag.
You can do this in the same file also outside of your class or in another component also.
To make in the same file, you can do so as:-
import styled from "#emotion/styled";
const CustomHeading=styled.h1`
color:yellow;
`
Use this Custom heading component in place of the h1 tag.
To define Custom component in the different file you will define with the same method, but you need to import that Custom component in the file in which you need to import it as:
import CustomHeading from "File path"
After that, you can use this component in place of your h1 tag.
Related
I'm trying to import a component-level CSS module (xx.module.css), but it is not being applied. I am using Nextjs and using a custom server.js, _app.js, and _document.js. I am suspicious that I need to do something in one of those files to make the CSS load, but I do not know what it is. Is anyone able to guide me?
Styled code:
import styles from "./NavigationButtons.module.css";
...
<div className={styles.test}>
<>Some text here I want colored</>
</div>
NavigationButtons.module.css:
.test {
color: "red";
}
I am angular user and i got a new project in my hands that i need to learn react. I am in very beggining, and my doubt is very very simple i think.
So, in angular you can add your css in the file angular.json (styles) right? And that css are available for all components that you will create.
So, in react i am looking and looking and i can't find an answer. The only way is import the file in all components?
I have a css file that i need for all components and i don't want import that file in all components.
Is there other way on react?
Thank you for the answers.
Suppose this is the code in your styles.css file.
.container{
background: goldenrod;
height: 80vh;
}
Then you can link this file in any React Component like this:
import React from 'react';
import classes from './styles.css';
class Cont extends Container{
render(){
return (
<div className={classes.container}>
<p>Hi</p>
</div>
)
}
}
I have a simple front-end React app created using npx create-react-app. The app is using react-router-dom routes. When I directly change the URI in the browser from say, localhost:3000/ to localhost:3000/search it will navigate to the <Route>but with no CSS rendered; just HTML from the component.
How can I make sure CSS is rendered in the new route when directly navigating in the browser? My future goal is to be able to copy and paste a route in a new tab and navigate to the correct page and display results from an API.
Css style sheets will need to be imported either at the root level or within the file itself.
style sheets need to be imported when used and then the corresponding classname will need to be used within the component or tag.
Another useful way to set react css without style sheets is by using in line styles
e.g
<div style ={{float: "right", textAlign : "center"}}> </div>
EDIT
A really easy way to get styles going within a react project is install bootstrap.
then buttons and stuff can be assigned classNames such as
<div className = "jumbotron"></>
this will leave a grey box around the items.
<div className = "btn btn-primary"></>
this will give you a blue styled button.
Any more information or help within your application let me know and provide some code snippets.
You can use styled-components. styled-components are widely used in ReactJs and React Native and it's a perfect choice.
styled-component : https://www.npmjs.com/package/styled-components
I realized that react apps created using npx create-react-app allow you to import a css module for components.
Given the component, Button.jsx, you can simply create a css module with the convention, [module-name].module.css. For the case of Button.jsx, create a file named Button.module.css, import "styles" from the module. Styles will be an object containing all the CSS styles.
I if I had a folder named "components" with all my components, I could make a folder within "components", say called "compStyles", and create all the [module-name].module.css files in there.
Button.module.css:
/* class names must be camelCased */
.myButton {
margin: 0 auto;
}
span {
fontSize: 20px;
}
If I had the above mentioned file structure, I could import and use like so:
import React from 'react';
import styles from './styles/Button.module.css';
const Button = () => {
return (
<div className={styles.myButton}>
<button><span>Some Button</span></button>
</div>
)
}
export default Button;
Styles for the span will be automatically applied, and any other class will be referenced by styles.className. Create one file for every component, and each component's CSS will take care of itself and not break like it would if it was in the public folder.
I need my css stylesheet split into many files, one per each Next.js page. How to implement this?
I tried to use next-css and just import a css-file into each page. It almost works. However the css-file is not loaded on Link navigation. Authors of Next say it's not implemented:
https://github.com/zeit/next-plugins/issues/282#issuecomment-523128645
I also tried using styled-jsx. It has several problems for me. It has many bugs on its Issues page. I also failed to make styles visible throughout child components with this approach.
You can import a module.css (import style from 'root/styles/MyStyle.module.css) and use as follows.
This is your component:
import style from '../../styles/components/Header.module.css'
export default function Header() {
return (
<h1 className={style.header}>
Hello World
</h1>
);
}
This is your CSS:
.header{
color: #fff;
width: 100%;
height: 80px;
}
Note that the CSS class is used as the components className;
Or, inside the react component, you can try adding the tag <style jsx>{ your_CSS_here }</style>.
According to Next documentation each page uses the styled-jsx library. But you can still use other CSS-in-JS libraries such as Styled Components and Emotion.
Next apps has also support for using sass that allow you to import .css and .scss files.
For more information you can check the Vercel Docs. I also recommend you to check the Learning area.
You can create your seperate css files and on each component that you need specific styling you import the css file that is unique to that component. so for example say you have a Component in file file1.js then you can import styles specific to this component in the file file1.css same happens for another file file2 with css file2.css
import './file1.css'; //importing the style specific only for this component
function File1(){
const [processing, setProcessing] = useState(false)
return (
<> </>
)
}
export default File1
Similarly for the second file
import './file2.css'; //css file specific to this component
function File2(){
const [processing, setProcessing] = useState(false)
return (
<> </>
)
}
export default File2
Basically, I'm trying to build a microservice that uses a standard UI library we built internally to generate HTML templates for use in email and we want to automatically inline all our CSS into each tag.
Both our standard UI library and the email templates I've created import CSS like so:
// someComponent.jsx
import React from 'react';
import Button from '#shared-ui/Button';
import 'app/modules/someComponent.scss';
class someComponent extends React.Component {
render() {
return (
<div className="someComponent">
<Button>
Click here to learn more.
</Button>
</div>
)
}
}
export default someComponent;
And the SCSS file:
// someComponent.scss
.someComponent {
font-size: 42px;
}
We import the Button component from our UI library (which already has its own CSS).
For building our web app, this sort of approach works out fine. But when it comes to building out HTML that is suitable for email, it's lacking, since we need to provide inline tags.
Ultimately, when I call renderToStaticMarkup() in our entry file, I want the rendered HTML to come out looking something like this (including the components we imported from our shared UI library):
<div style="font-size:42px">
<button style="background-color: #fff; border: 1px">
Click here to learn more.
</button>
</div>
I'm having a tough time with this. Short of writing out the inline styles by hand, I can't figure out a way to get this to work (and that doesn't even include how I would go about getting inline styles for our shared UI library).