search

Home  >  Q&A  >  body text

Implement dynamic media query settings using Tailwind CSS

<p>In my React JS application I have a component and I want to set a mobile breakpoint based on a variable: </p> <pre class="brush:php;toolbar:false;"><Component size={'500px'}/> const Component = ({size}) => { return ( <div md:flex>hello</div> ) }</pre> <p>I want to add something like <code>[size]:flex</code> where <code>md:flex</code> will set a breakpoint based on that property. (eg: if the size is 500px or 100px or 900px, set it to display flex) </p><p> Question: Is it possible to set media queries the way I described above? </p>
P粉141911244P粉141911244543 days ago697

reply all(1)I'll reply

  • P粉710478990

    P粉7104789902023-08-27 10:09:52

    You can use classnames npm package:

    const rootClassName = cn(
         // 首先添加任何tailwind classNames
         "h-full flex flex-col",
         // 这是条件部分。根据size属性,定义className
        {
          [md:flex-1]: size === "A",
          [sm:flex-1]: size === "B",
        }
      )

    reply
    0
  • Cancelreply