not the screen I want to apply this if the component is of width 600px I'm trying to change some styling if the component is in a Modal I only want to change flex-direction from column to row-reverse so I can't see a point in doing another component my code
const wrapper = styled.div`
display: flex;
flex-direction: column;
@media screen and (max-width: 600px) {
flex-direction: row-reverse;
}
}
`
This obviously doesn't since it is checking the screen
Using CSS
@containerqueriesUsing container queries requires creating a parent container element that the
Wrappercomponent can be rendered into. This is supported instyled-components@6.Example:
Usage:
Or a bit more succinct using an anonymous inline function:
Usage:
Using Javascript to measure the DOM
If your project or environment doesn't support container queries then you will need to measure the
divHTML node to check its width to see if it is less than or equal to 600 pixels or not.Here's an example implementation using a wrapper component to do the measuring of the styled
divelement:Custom hook to measure DOMNode by React ref and return its width.
Wrapper to attach ref and set CSS classname for when the "media query" condition is met.
The original
divstyled component.Element under 600px
Element over 600px
Element in parent element with max-width of 700px