Skip to content

How to move around blocks of code with React and Tailwind

While working on a Next.js website I had the need to move a React component to a whole different place in my markup, depending on the size of the screen.

In particular I had a Sidebar component I wanted on the left side of the screen on a big display, but before the content on a smaller display.

Due to the way I organized the HTML markup and the CSS, it was not immediately clear to me how to perform this transition without rewriting a good portion of it.

So I looked at Tailwind to provide me a nice solution.

And this was the way: I added the component twice on the screen, assigning the class hidden xl:block to the "big screen" part, and xl:hidden to the snippet for the smaller screens:

<div className="hidden xl:block">
  <Sidebar />


<div className="xl:hidden">
  <Sidebar />

The drawback of course is that the component is rendered twice, but being this a simple presentational component without logic, it was a compromise I could live with.

→ Download my free React Handbook!



You might be interested in those things I do:

  • Learn to code in THE VALLEY OF CODE, your your web development manual
  • Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
  • I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
  • Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
  • Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
  • Find me on X

Related posts that talk about react: