gpt-3.5-turbo-0125· openai
Suzhen/CodeChat
can you pass media queries inside React component's style prop?
- User
can you pass media queries inside React component's style prop?
- Assistant
Yes, you can pass media queries inside a React component's style prop by using template literals and string interpolation. Here's an example of how you can do it:
import React from 'react'; const MyComponent = () => { const styles = { color: 'blue', fontSize: '16px', // Media query for screens smaller than 768px '@media (max-width: 768px)': { fontSize: '14px', }, }; return ( <div style={styles}> Hello World </div> ); }; export default MyComponent;In the example above, we create an object called
stylesthat contains the base styles for the component. We then add a media query using the@mediakey and specify the conditions for the media query inside the string. Finally, we pass thestylesobject to thestyleprop of thedivelement.