score:96
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>
Source: stackoverflow.com
Related Query
- CSS pseudo elements in React
- Add CSS to react created elements like data-reactroot
- css pseudo code "li::before" in react inline style
- React native - pseudo elements equivalent
- How to get Font Awesome Icon in react app for a pseudo element in css
- How to dynamically add css style to pseudo classes in React
- Prevent global css to affect my react npm package elements
- Elements doesn't survive state change for css transitions in React
- Inline CSS in React - how to style multiple li elements
- Conditional rendering of CSS style in elements React
- Beginner: Styling ReactJS Checkbox using CSS Pseudo Elements
- Elements are going inside the nav bar css react
- React onClick issue while using CSS Active Pseudo Class
- Can i modify CSS pseudo elements in React?
- React Fontawesome CSS pseudo element not rendering inside div but works in Codepen
- Component Not Rendering Properly in React when using CSS hover pseudo selector
- React JS collapsable elements Pure CSS
- Styling some parts of my React JSX rendered elements with CSS
- How to position my elements horizontally in react with css
- Restrict external CSS impact on local react components or HTML elements
- For CSS Pseudo classes ,why React developers have to go for state changes?
- react component css space aligning on elements not working
- Add css class onClick to idividual elements generated by mapping an array in React
- How to import a CSS file in a React Component
- Extending HTML elements in React and TypeScript while preserving props
- SyntaxError with Jest and React and importing CSS files
- How to apply global styles with CSS modules in a react app?
- How to define css variables in style attribute in React and typescript
- How to make React CSS import component-scoped?
- CSS pseudo selectors with MUI
More Query from same tag
- Using useState hooks within an async callback
- How to use layers with react-fontawesome
- How can I have my button link to another js file using React?
- Reactjs / Material-UI: className does not work on the ToggleButton component
- How can I dynamically tie my form into Formik, in React functional component
- React: Cannot read property 'method()' of undefined when method is bound
- Webpack-dev-server serves a directory list instead of the app page
- Redux Toolkit createSlice in React
- Is there a way to get the qqfilename and qquuid that was given to the server?
- Connecting remote client to Phoenix channel
- How to access the data within object fetch from firestore
- Fetching two type of data from same API
- Tabs not loading pane information
- TypeError: _reactDom.default.createElement is not a function
- In React.js : Is it possible to only access parts of a children component?
- Why the ref button will be clicked 3 times?
- How do I pass a run-time variable into a react/webpack module
- Typescript error "Cannot read property 'data' of undefined" in movie api-app:
- Crossfade between Gatsby Images
- ReferenceError: window is not defined in getServerSideProps for Next.js App And I Need to Store A Variable Persistantly in The getServerSideProps Func
- Objects are not valid as a React child using react router v4
- DraftJS editor create from HTML is not working
- How can I update a value in mapped list without updating the list's siblings?
- ReactJS: always transfer props to child component
- React App : Start API server with React server
- preventDefault not working when form submit react js
- How to update state if I have props coming from parent component in React Hooks
- getting jquery object after bind(this) in react componentDidMount
- facing some issue to set type on sweetalert2
- Why does react turn my state into [object object]