Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include .css file in .tsx typescript?

How do i include "css" file in "tsx" and how to use it? i.e how do i render static files?

import * as React from "react";
import { Header } from "./header";

//import "./home.css";


export class Home extends React.Component<{}, {}> {
    render() {
        return (
            <div id="page-top" className="landing-page">
                <Header />
            </div>
        );
    }
}
like image 202
Ankit Raonka Avatar asked Jan 13 '17 05:01

Ankit Raonka


People also ask

Can we add CSS in JS file?

The CSS file is used to describe how HTML elements will be displayed. There are various ways to add CSS file in the HTML document. JavaScript can also be used to load a CSS file in the HTML document.


1 Answers

See this answer:

https://stackoverflow.com/a/37144690/3850405

If you only need css for a class in your component you could do it like below. I like this solution for when inline css does not work and only small changes are needed.

import * as React from "react";
import { Header } from "./header";

export class Home extends React.Component<{}, {}> {
    render() {
        const css = `
        .landing-page {
            background-color: orange;
        }
        `
        return (
            <div>
                <style>
                    {css}
                </style>
                <div id="page-top" className="landing-page">
                    <Header />
                </div>
            </div>  
        );
    }
}
like image 175
Ogglas Avatar answered Sep 21 '22 04:09

Ogglas