I know about this method of injecting CSS:
import Head from 'next/head'
export default function Home() {
return (
<div className="container">
<style jsx global>{`
body {
margin: 0;
}
`}</style>
<Head>
<title>audiom</title>
<link rel="icon" href="/favicon.ico" />
</Head>
</div>
)
}
But is there a way to quickly insert a css file (css reset in my case)?
You can import reset.css in _app.jsx file, it will apply css reset styles for all pages.
_app.tsx
import '../styles/reset.css';
Related
When I import my css file between style tags, it changes everything.
Why it is happening? How can I fix this problem?
I already used 'scoped 'feature but it is not working.
for instance
// in styles.css file
h1 { color: red; }
// in component file
<template> ... </template>
<script> ... </script>
<style scoped>
import './styles.css';
<style>
When I run server, it changes every h1 tags in project. I am using 'scoped' how can it happen.
Try with SCSS instead of CSS:
<style lang="scss" scoped>
#import './styles.css';
</script>
Say i have the following css file:
.testClass{
color:red;
}
And the following index.html file:
<html>
<head>
<meta charset="utf-8">
<title>Portal Demo</title>
<link rel="stylesheet" href="test.css">
<script type="module" src="../build/portal-app.js"></script>
<style>
p {
border: solid 1px blue;
padding: 8px;
}
</style>
</head>
<body>
<i class="fa fa-times"></i>
<portal-app>
<p>This is child cosntent</p>
</portal-app>
</body>
</html>
And inside of the portal-app i have the following code:
import 'sdk-button'
#customElement('portal-app')
export class PortalApp extends LitElement {
render() {
return html`
${this.renderWidget()}
`;
}
renderWidget() {
import("./views/my-test-element");
return html`
<i class="fa fa-times"></i>
<sdk-icon-button text="hello marc"><p slot="icon" class="testClass">hello</p></sdk-icon-button>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'portal-app': PortalApp;
}
}
When i run this the color of the .p tag is not red and i can't see the style. however i just add a normal p tag to html the color correctly turns red.
But it seems that inside my slot i am unable to pass the css class that is defined in the index.html can anyone tell me why?
This is expected behaviour as per Web Components Shadow DOM specifications.
In your code
<p slot="icon" class="testClass">hello</p>
This template is part of PortalApp Web Component. Which means this markup is in the Shadow DOM of Portal App web component. No css styles from outside (index.html) in you case can get applied to this markup. vice- versa is also true, i.e, no CSS leak from the component to outside of component will happen
I have a very simple project in NextJs. I want to serve the files via NginX
These are my dependencies.
"dependencies": {
"isomorphic-unfetch": "^2.0.0",
"next": "^7.0.2",
"next-routes": "^1.4.2",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"semantic-ui-react": "^0.80.2"
}
My routes js
const routes = require('next-routes')();
module.exports = routes;
I have a common layout for entire application as shown here. Note the style jsx
import React from 'react';
import { Container } from 'semantic-ui-react';
import Header from '../components/header';
import Head from 'next/head';
const Layout = (props) => {
return(
<Container style={{margin:'30px', 'backgroundColor':'#fff','borderRadius': '5px'}}>
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.12/semantic.min.css"></link>
<meta charSet="utf-8" />
<style jsx global>{`
body {
background: #202020;
font: 11px;
}
div.container {
margin: 30px;
padding-bottom: 2px;
}
`}</style>
</Head>
<Header />
<div className='container'>
{props.children}
</div>
</Container>
);
};
export default Layout;
All other pages would use this layout.
render() {
return(
<Layout>
<div>
my app
</div>
</Layout>
);
}
With Dev server, layout's styled jsx is getting applied properly. everything works just great.
If i do, npm run build & export and use the out content for static hosting, styled jsx is completely missing.
I also found that the out directory has index.html which is NOT serving the css. If use the index/index.html css is fine.
Again what is the right approach?
You need to put the <style jsx> tag outside <Head>. Basically all styled-jsx styles need to be a children element of the root.
Here's the working example
Simply download it (by clicking at the middle-top-left - there is a button to download).
<Container>
<Head>
...
</Head>
<div className="container">{props.children}</div>
<style jsx global>{`
body {
background: #202020;
font: 11px;
}
container {
margin: 30px;
padding-bottom: 2px;
}
`}</style>
</Container>
Here's some other useful info about styled-jsx that might help you better manage the styles:
Dynamic Styles
External Styles
Styles outside components
Also take into account that styled-jsx is not the only CSS-in-JS library out there. I personally prefer styled-components, you should check it out. Here's an example on how to implement it with Next.js.
Cheers
I am using webcomponents-lite (Polymer) to create webcomponents.
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="styles.css">
<script type="text/javascript" src="/static/js/util.js"></script>
</head>
<body class="dark">
<main>
<edsp-login-form></edsp-login-form>
</main>
</body>
</html>
<edsp-login-form> is defined in js file where it uses lit-html. The definition as follows:
#Define('edsp-login-form', {
style: ``
})
export class Login extends LitComponent {
render() {
return html`
<link rel="import" type="css" href="styles.css">`
<div></div>
}
}
In this code, how do apply css classes from styles.css to a component <edsp-login-form> ?
Option 1: If you're using Shadow DOM, you can't apply external CSS to your component. A solution is to deactivate the Shadow DOM by implementing this in your component:
createRenderRoot() {
return this;
}
Option 2: If you still want to apply external CSS and use Shadow DOM, you can use CSS variables. In your external CSS you can instance the variable using something like:
name-of-your-component-tag {
--variable-name-color: #FFF;
}
and then in your component, you can use the CSS variable:
.my-class {
color: var(--variable-name-color);
}
Option 3: And if you're trying to apply a dark theme, you can use the CSS inside of your component considering the context. In your component you can use CSS like:
:host-context(.dark) .bg {
background: #000;
}
My index.js for React, I used a create-react-app starter project.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App />,
document.getElementById('root')
);
My app.js includes:
import './App.css';
and I use the CSS:
.body {
background-color: "green"
}
and the body does not change to green.
How do I modify CSS? I also tried placing app.css into public/app.css and loading with link rel="stylesheet" Element inspector still showed no effect.
Element inspector shows margin of 8px on body element from "user agent stylesheet".. nothing from my loaded css.
EDIT:
I also try inline style tag:
<!doctype html>
<html lang="en">
<script src="web3.js"></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>CryptoClicker</title>
</head>
<body>
<div id="root"></div>
</body>
<style>
.body {
background: "green";
}
</style>
</html>
no effect.
You're using .body which is looking for elements with the class 'body' - as there's a . Change that to body (with no .) and it'll style the body element rather than the class.
Also, named colours in CSS are not variable strings - so remove the quotes:
body {
background: green;
}
It'd be worth putting your styles back into App.css and importing it in JS rather than using the <style> tags.