What is the current best practice to set global/reset CSS if I'm using Lit-element?
I have tried 1) Inline them in <style> in my document root, 2) Construction stylesheet like this answer
<script>
var css = new CSSStyleSheet()
css.replace('#import url("./style/static/reset.css")')
document.adoptedStyleSheets = [css]
</script>
but nothing works...
EDIT
My reset.css:
blockquote,
body,
dd,
dl,
fieldset,
figure,
h1,
h2,
h3,
h4,
h5,
h6,
hr,
legend,
p,
pre,
button,
ul {
margin: 0;
padding: 0;
}
img {
max-width: 100%;
}
I'm building on top of folder structure scaffolded from https://open-wc.org/guide/#quickstart
This won't work as you expected because LitElement by default uses Shadow DOM which is designed to prevent external CSS from affecting the component's inner tree (and vice versa)
The only way to affect the styles inside a web component is if the component uses CSS variables or if properties that inherit styles are undefined inside the web component (for more info check this guide)
However, if this is a project fully based on LitElement, you can share styles between components quite easily and use that to do this reset:
First create a js file for your shared css (e.g. reset-css.js)
import { css } from 'lit-element';
export default css `
blockquote,
dd,
dl,
fieldset,
figure,
h1,
h2,
h3,
h4,
h5,
h6,
hr,
legend,
p,
pre,
button,
ul {
margin: 0;
padding: 0;
}
img {
max-width: 100%;
}
`;
Then import your style in your components
import {LitElement, html, css} from 'lit-element';
// include your reset styles
import resetCSS from './reset-css.js';
class MyComponent extends LitElement {
static get styles() {
// this is the important part, this array includes our resetted styles and this components styles
return [
resetCSS,
css`
h1 {
color: blue;
}
`
];
}
render() {
html`<h1>Something Blue</h1>`
}
}
And just like that, any component which includes the shared reset styles will use them
Related
As of now the styling happens in two places: global.css + index.js via styled-components
Problem
After start or restart of local server + manual page refresh, all styles are applied as intended. Also style changes are applied promptly.
Now, if another manual page refresh is performed, only styles from global.css are applied, but styles from index.js file don't initially load.
Styles other than global.css only take effect in 2 ways:
click one of the links + click on retun button (2nd screenshot)
restart local server + page refresh
I recorded the steps I make. Sorry for the breathing noise. 😌
https://www.youtube.com/watch?v=cp-97ZYsQhw
I added the code snippets for a better code overview only. I'm trying to setup a working example on codesandbox.
Question
Why does next.js only load the global.css, but ignore the index.js with more css until either the local server is restarted or some links are clicked?
import Link from "next/link";
import styled from 'styled-components';
const StyledMain = styled.main`
width: 100vw;
height: 100vh;
display: grid;
justify-content: center;
align-content: center;
`;
const StyledLi = styled.li`
font-size: var(--fontsize-profileButtons);
text-align: center;
list-style: none;
margin: 50px 0;
`;
export default function Home() {
return (
<>
<StyledMain>
<ul>
<StyledLi>
<Link href="/about">about</Link>
</StyledLi>
<StyledLi>
<Link href="/projects">projects</Link>
</StyledLi>
<StyledLi>
<Link href="/blog">blog</Link>
</StyledLi>
</ul>
</StyledMain>
</>
)
}
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
body {
width: 100vw;
height: 100vh;
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
What index.js looks like after page refresh + what it's supposed to look like
Procedure to also load css code inside index.js
I think that if you have more than one global.css, it might cause this. Just a suggestion from a similar experience.
Is there a way to limit the styles that emotion injects into the global scope to a narrower scope?
I've seen this method of using a stylis plugin to add more narrowly-scoped styles, but that only adds scoped styles. What I'd like to do is limit all styles added by emotion to a particular selector.
The SCSS equivalent of what I'm looking for is something like this, where the styles would only apply to elements where div#my-styled-div is an ancestor:
div#my-styled-div {
h1, h2, h3, h4, h5, h6 {
// emotion header styles
}
button {
// emotion button styles
}
}
My use case is a Docusaurus site with a demo page that uses Chakra UI (specifically the Chakra UI demo page for react-querybuilder) and I don't want the emotion global styles to affect the Docusaurus theme styles.
(This question is similar, but it was never answered and I don't think it's exactly the same issue anyway.)
Emotion supports SCSS-like nesting, so you can define styles which only apply inside of a specific element like this:
import { css } from "#emotion/react";
const myCss = css`
h1,
h2,
h3,
h4,
h5,
h6 {
color: red;
}
button {
/* ... */
}
`;
export default function App() {
return (
<div>
<h2>Emotion styles don't apply to this h2</h2>
<div css={myCss}>
<h2>Emotion styles apply to this h2</h2>
{/* Put the stuff you want to style with Emotion here */}
</div>
</div>
);
}
CodeSandbox
I am in a conundrum: the stylelint rule "selector-max-universal": 0 is required and, at the same time, I need to provide a default font family to text elements within a certain class.
Therefore I am not able to use this:
* { font-family: Somefont; }
And, at the same time, code review requested me not to use these kind of selectors (SCSS mixin):
#mixin setGlobalFontFamily($family: Somefont) {
button,
div,
h1,
h2,
h3,
h4,
h5,
h6,
label,
p {
font-family: $family, sans-serif;
}
}
// fonts are specific to certain classes
.theme-a {
#include setGlobalFontFamily;
}
.theme-b {
#include setGlobalFontFamily(Roboto);
}
//.theme-...
Theme classes are conditionally applied through JS to a container element, e.g.:
<body>
<section class="theme-b">
</section>
</body>
Additionnaly, these fonts families should be set globally in one file and only once per each theme class, guaranteeing that other theme font families are not shown...
Can anyone see a way to workaround this problem?
If I understood correctly you can just set the font families directly to .theme-a and .theme-b e.g.:
.theme-a {
font-family: 'Some Font', sans-serif;
}
.theme-b {
font-family: 'Roboto', sans-serif;
}
The children of those elements should inherit the fonts automatically if something doesn't overwrite them. There's no need of setting each element manually.
We are using this to load fonts async with typekit:
<script src="https://use.typekit.net/whatever.js"></script>
<script>try{Typekit.load({async:true});}catch(e){}</script>
But then we had issues with fonts being styled as arial for a split second before the page loads so we hide elements like this (adobe ads this class to elements):
.wf-loading{
h1, h2, h3, h4, h5, p, a{
visibility: hidden; //hide stuff until adobe gives us the font
}
}
But what happens if adobe's servers are down, which has happened twice last month for London. Will the elements be unhidden? How do other people manage this issue with typekit?
No information here: https://helpx.adobe.com/typekit/using/font-events.html
Here is a fix to ensure that browser default fonts are displayed if Typekit servers are down.
<script>
(function(d) {
loadFonts = 1;
if(window.sessionStorage){
if(sessionStorage.getItem('useTypekit')==='false'){
loadFonts = 0;
}
}
if (loadFonts == 1) {
var config = {
kitId: 'XXXXXXXX',
scriptTimeout: 3000,
async: true
},
h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";if(window.sessionStorage){sessionStorage.setItem("useTypekit","false")}},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+="wf-loading";tk.src='https://use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s);
}
}
)(document);
</script>
<style>
.wf-loading p, .wf-loading h1, .wf-loading h2, .wf-loading h3, .wf-loading h4 {
visibility: hidden;
}
.wf-active p, .wf-active h1, .wf-active h2, .wf-active h3, .wf-active h4 {
visibility: visible;
}
.wf-inactive p, .wf-inactive h1, .wf-inactive h2, .wf-inactive h3, .wf-inactive h4 {
visibility: visible;
}
</style>
You can read more about the full solution in this blog post.
I've run into this before but can't remember the exact details.
I'm pretty sure it's either:
a) Typekit removes the .wf-loading class after a certain time as a safety measure.
b) Add your own timeout script that removes the class from body.
I am completely new with either SASS and LESS (I know the concept, but never used them), but I know CSS. This question is about is the idea or direction is good, or should I look for other?
I am facing the the following task: I need to have create .CSS files as usual except all selectors must be selective within a single div (with its id). So the normal reset should be like:
/* ...*/
div#mydivid h1, div#mydivid h2, div#mydivid h3, div#mydivid h4, div#mydivid div#mydivid h5 {
/* ...*/
}
instead of:
/* ...*/
h1, h2, h3, h4, h5 {
/* ...*/
}
/* ...*/
Obviously I could manually maintain this, and rewrite my (and other's) standard CCS-s, but I am hoping that I can still write my CSS (and use existing ones) in the standard way, and a build time a preprocessor generates the transformed ones, what I will actually link to my pages.
Is this possible? Has anybody better idea?
(I do not think it should matter but this is VS 2013, and C# / ASP.NET)
Using Less or SASS/SCSS you may wrap the entire stylesheet into a div. But SASS has a feature to break out of any wrappers called #at-root.
You may even wrap the includes to get a wrapper around any stylesheets (SCSS):
div#mydivid {
#include _layout.scss
#include _module1.scss
}
/* or */
div#mydivid {
#{headings(1,6)} {
color: #333;
}
}
The latter results in:
div#mydivid h1,
div#mydivid h2,
div#mydivid h3,
div#mydivid h4,
div#mydivid h5,
div#mydivid h6 {
color: #333;
}