score:96

Accepted answer

got a reply from @vjeux over at the react team:

normal html/css:

<div class="something"><span>something</span></div>
<style>
    .something::after {
    content: '';
    position: absolute;
    -webkit-filter: blur(10px) saturate(2);
}
</style>

react with inline style:

render: function() {
    return (
        <div>
          <span>something</span>
          <div style={{position: 'absolute', webkitfilter: 'blur(10px) saturate(2)'}} />
        </div>
    );
},

the trick is that instead of using ::after in css in order to create a new element, you should instead create a new element via react. if you don't want to have to add this element everywhere, then make a component that does it for you.

for special attributes like -webkit-filter, the way to encode them is by removing dashes - and capitalizing the next letter. so it turns into webkitfilter. note that doing {'-webkit-filter': ...} should also work.

score:-2

not a direct answer to the question, but this may help those who are having trouble creating style information using typescript.

i was getting an error telling me that the following was incorrect:

let iconstyle = {
    position: 'relative',
    maxheight: '90px',
    top: '25%',
}

the error told me that "types of property 'position' are incompatible". i have no idea why.

i fixed this by adding a strict typescript declaration, like so:

let iconstyle: cssproperties = {
    position: 'relative',
    maxheight: '90px',
    top: '25%',
}

this works.

score:3

i don't know if this would be considered hacky but it certainly works (using css variable):

const passedinlinestyle = { '--color':'blue'}

then in an imported css file:

background:var(--color);

score:10

you can use styled components.

install it with npm i styled-components

import react from 'react';
import styled from 'styled-components';

const youreffect = styled.div`
  height: 50px;
  position: relative;
  &:after {
    // whatever you want with normal css syntax. here, a custom orange line as example
    content: '';
    width: 60px;
    height: 4px;
    background: orange
    position: absolute;
    bottom: 0;
    left: 0;
  },

const yourcomponent = props => {
  return (
    <youreffect>...</youreffect>
  )
}

export default yourcomponent

score:16

depending if you only need a couple attributes to be styled inline you can do something like this solution (and saves you from having to install a special package or create an extra element):

https://stackoverflow.com/a/42000085

<span class="something" datacustomattribute="👋">
  hello
</span>
.something::before {
  content: attr(datascustomattribute);
  position: absolute;
}

note that the datacustomattribute must start with data and be all lowercase to satisfy react.

score:17

inline styling does not support pseudos or at-rules (e.g., @media). recommendations range from reimplement css features in javascript for css states like :hover via onmouseenter and onmouseleave to using more elements to reproduce pseudo-elements like :after and :before to just use an external stylesheet.

personally dislike all of those solutions. reimplementing css features via javascript does not scale well -- neither does adding superfluous markup.

imagine a large team wherein each developer is recreating css features like :hover. each developer will do it differently, as teams grow in size, if it can be done, it will be done. fact is with javascript there are about n ways to reimplement css features, and over time you can bet on every one of those ways being implemented with the end result being spaghetti code.

so what to do? use css. granted you asked about inline styling going to assume you're likely in the css-in-js camp (me too!). have found colocating html and css to be as valuable as colocating js and html, lots of folks just don't realise it yet (js-html colocation had lots of resistance too at first).

made a solution in this space called style it that simply lets your write plaintext css in your react components. no need to waste cycles reinventing css in js. right tool for the right job, here is an example using :after:

npm install style-it --save

functional syntax (jsfiddle)

import react from 'react';
import style from 'style-it';

class intro extends react.component {
  render() {
    return style.it(`
      #heart {
        position: relative;
        width: 100px;
        height: 90px;
      }
      #heart:before,
      #heart:after {
        position: absolute;
        content: "";
        left: 50px;
        top: 0;
        width: 50px;
        height: 80px;
        background: red;
        -moz-border-radius: 50px 50px 0 0;
        border-radius: 50px 50px 0 0;
        -webkit-transform: rotate(-45deg);
        -moz-transform: rotate(-45deg);
        -ms-transform: rotate(-45deg);
        -o-transform: rotate(-45deg);
        transform: rotate(-45deg);
        -webkit-transform-origin: 0 100%;
        -moz-transform-origin: 0 100%;
        -ms-transform-origin: 0 100%;
        -o-transform-origin: 0 100%;
        transform-origin: 0 100%;
      }
      #heart:after {
        left: 0;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        -o-transform: rotate(45deg);
        transform: rotate(45deg);
        -webkit-transform-origin: 100% 100%;
        -moz-transform-origin: 100% 100%;
        -ms-transform-origin: 100% 100%;
        -o-transform-origin: 100% 100%;
        transform-origin :100% 100%;
      }
    `,
      <div id="heart" />
    );
  }
}

export default intro;

jsx syntax (jsfiddle)

import react from 'react';
import style from 'style-it';

class intro extends react.component {
  render() {
    return (
      <style>
      {`
        #heart {
          position: relative;
          width: 100px;
          height: 90px;
        }
        #heart:before,
        #heart:after {
          position: absolute;
          content: "";
          left: 50px;
          top: 0;
          width: 50px;
          height: 80px;
          background: red;
          -moz-border-radius: 50px 50px 0 0;
          border-radius: 50px 50px 0 0;
          -webkit-transform: rotate(-45deg);
          -moz-transform: rotate(-45deg);
          -ms-transform: rotate(-45deg);
          -o-transform: rotate(-45deg);
          transform: rotate(-45deg);
          -webkit-transform-origin: 0 100%;
          -moz-transform-origin: 0 100%;
          -ms-transform-origin: 0 100%;
          -o-transform-origin: 0 100%;
          transform-origin: 0 100%;
        }
        #heart:after {
          left: 0;
          -webkit-transform: rotate(45deg);
          -moz-transform: rotate(45deg);
          -ms-transform: rotate(45deg);
          -o-transform: rotate(45deg);
          transform: rotate(45deg);
          -webkit-transform-origin: 100% 100%;
          -moz-transform-origin: 100% 100%;
          -ms-transform-origin: 100% 100%;
          -o-transform-origin: 100% 100%;
          transform-origin :100% 100%;
        }
     `}

      <div id="heart" />
    </style>
  }
}

export default intro;

heart example pulled from css-tricks

score:41

inline styles cannot be used to target pseudo-classes or pseudo-elements. you need to use a stylesheet.

if you want to generate css dynamically, then the easiest way is to create a dom element <style>.

<style dangerouslysetinnerhtml={{
  __html: [
     '.my-special-div:after {',
     '  content: "hello";',
     '  position: absolute',
     '}'
    ].join('\n')
  }}>
</style>
<div classname='my-special-div'></div>

Related Query

More Query from same tag