question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Order of CSS injection

See original GitHub issue

Just hit a problem:

const SVG = styled.svg`
  line {
    stroke-width: 1px;
  }
`

const Horizontal = styled.g`
  line {
    shape-rendering:crispEdges;
  }
`

const Vertical = styled.g`
  line {
    stroke-width: 1.5px;
  }
`

export default () => (
  <SVG>
    <Horizontal>
      <line x1={0} y1={0} x2={width} y2={0}/>
    </Horizontal>
    <Vertical>
      <line x1={0} y1={0} x2={width} y2={height}/>
    </Vertical>
  </SVG>
)

Because Horizontal and Vertical get rendered before SVG, and I’m only injecting the styles on render, ._compile_from_Vertical line {} appears before ._compiled_from_SVG line {}. They have the same specificity of course and so SVG wins.

Injected source should be in the same order it is written, not in the order it’s rendered.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
geelencommented, Sep 26, 2016

Woo, found a manual escape hatch:

const Vertical = styled.g`
  && line {
    stroke-width: 1px;
  }
`

This generates .xxx.xxx line which lifts its specificity above .yyy line (from SVG above), so tbh I think this is “good enough” to consider this option closed.

In fact you can actually use && { some: thing; } to target the element itself (not just descendants). In case you have some greedy rules outside (like html p) which would override a styled.p with a single class name. This is great, since we don’t have to design good JS to cover bad CSS, we can just give folks a way to generate even worse CSS 👍

0reactions
geelencommented, Sep 28, 2016

So this didn’t stay solved for long. I found that server-side rendering wasn’t firing the componentDidMount, so we can’t use that to inject.

Read more comments on GitHub >

github_iconTop Results From Across the Web

material ui - How to specify the order in which the style sheets ...
The CSS injected by Material-UI to style a component has the highest specificity possible as the <link> is injected at the bottom of...
Read more >
Style library interoperability - Material UI - MUI
CSS injection order ⚠️. Note: Most CSS-in-JS solutions inject their styles at the bottom of the HTML <head> , which gives MUI precedence...
Read more >
Material UI v5 CSS injection order - CodeSandbox
TemplateMaterial UI v5 CSS injection order (theme); Environmentcreate-react-app. Files. App.js. ButtonBox.js. index.html. index.js. package.json.
Read more >
Testing for CSS Injection - WSTG - v4.1 | OWASP Foundation
Summary. A CSS Injection vulnerability involves the ability to inject arbitrary CSS code in the context of a trusted web site which is...
Read more >
CSS injection (stored) - PortSwigger
CSS injection vulnerabilities arise when an application imports a style sheet from a user-supplied URL, or embeds user input in CSS blocks without...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found