Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· openaiHugging Face Suzhen/CodeChat

can you pass media queries inside React component's style prop?

  1. User

    can you pass media queries inside React component's style prop?

  2. 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 styles that contains the base styles for the component. We then add a media query using the @media key and specify the conditions for the media query inside the string. Finally, we pass the styles object to the style prop of the div element.