<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sabin Baniya</title>
    <description>The latest articles on DEV Community by Sabin Baniya (@sabinbaniya).</description>
    <link>https://dev.to/sabinbaniya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F739640%2Fa19319eb-59ca-41bf-95f7-2294ac968ec5.png</url>
      <title>DEV Community: Sabin Baniya</title>
      <link>https://dev.to/sabinbaniya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sabinbaniya"/>
    <language>en</language>
    <item>
      <title>Let's create Pinterest's home feed layout using tailwind CSS &amp; a Simple CSS Trick</title>
      <dc:creator>Sabin Baniya</dc:creator>
      <pubDate>Mon, 28 Nov 2022 13:44:56 +0000</pubDate>
      <link>https://dev.to/sabinbaniya/lets-create-pinterests-home-feed-layout-using-tailwind-css-a-simple-css-trick-3l38</link>
      <guid>https://dev.to/sabinbaniya/lets-create-pinterests-home-feed-layout-using-tailwind-css-a-simple-css-trick-3l38</guid>
      <description>&lt;p&gt;Hey guys, Today we are going to be making Pinterest's masonry grid layout of their home feed. We will be using Next.JS &amp;amp; tailwind CSS with Unsplash API for this project.&lt;/p&gt;

&lt;p&gt;First, let's have a look at what we're going to be building.&lt;/p&gt;

&lt;p&gt;Here's what the home feed of Pinterest looks like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d7qb0mPh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghe1b7rxn85nureubued.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d7qb0mPh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghe1b7rxn85nureubued.png" alt="Creating Pinterest layout with tailwind CSS" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upon inspecting with Chrome Devtools, We can see that they have implemented the layout using translate and absolute position, etc. for their layout.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4vxrBvEK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtzs7iy1kibeiwsddkn1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4vxrBvEK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtzs7iy1kibeiwsddkn1.png" alt="Creating Pinterest layout with tailwind CSS" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But we are going to be implementing the same layout in a much simpler way, using Columns. Columns is a CSS shorthand for column count &amp;amp; column width property. I first saw this in the tailwind docs as class and later realized that it's a CSS property. So, what this property does is it helps in dividing an element into columns that you specify. Kinda like the columns in a newspaper article.&lt;/p&gt;

&lt;p&gt;You can checkout the tailwind docs for columns &lt;a href="https://tailwindcss.com/docs/columns"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's go ahead and get access to Unsplash API. Go to &lt;a href="https://unsplash.com/developers"&gt;Unsplash developer&lt;/a&gt; , register your application &amp;amp; you will be able to get access &amp;amp; secret key for that project.&lt;/p&gt;

&lt;p&gt;Now let's use create-next-app to create a next.js application for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app pinterest-layout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's install our dependencies for this project. First, Tailwind CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the two commands, your root directory shall now have a tailwind.config.js file.&lt;/p&gt;

&lt;p&gt;In that file remove all existing code &amp;amp; add the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now under &lt;code&gt;/styles/globals.css&lt;/code&gt;. Remove all existing code &amp;amp; add the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, Let's go to &lt;code&gt;/pages/index.js&lt;/code&gt; now. We will remove all the existing code and add the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Head from "next/head"
const Home = () =&amp;gt; {
  return &amp;lt;&amp;gt;
    &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;Pinterest&amp;lt;/title&amp;gt;
    &amp;lt;/Head&amp;gt;
  &amp;lt;/&amp;gt;;
};

export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now with this much done, we can start to set up our unsplash api, first create a &lt;code&gt;.env.local&lt;/code&gt; file in your root directory.&lt;/p&gt;

&lt;p&gt;Add in your access key provided in the Unsplash developer portal.&lt;/p&gt;

&lt;p&gt;Now let's install the Unsplash library&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i unsplash-js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that let's initialize the instance to make request to unsplash api as said in their &lt;a href="https://github.com/unsplash/unsplash-js#creating-an-instance"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will be using &lt;code&gt;getServerSideProps&lt;/code&gt; to make request from our server to the unsplash server &amp;amp; we will return the response as props to the page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createApi } from "unsplash-js";

export const getServerSideProps = async () =&amp;gt; {
  const serverApi = createApi({
    accessKey: process.env.ACCESS_KEY,
  });

  const photos = await serverApi.photos.list({perPage: 30});
  return {
    props: {
      photos: photos.response.results,
    },
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if everything goes well till here, then we will get photos props in our page and we will be able to access it via props.&lt;/p&gt;

&lt;p&gt;Now lets map over the photos prop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Home = ({ photos }) =&amp;gt; {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;Pinterest&amp;lt;/title&amp;gt;
      &amp;lt;/Head&amp;gt;
      &amp;lt;section&amp;gt;
             {photos.map((el) =&amp;gt; (
                &amp;lt;div key={el.id}&amp;gt;
                  &amp;lt;img
                    src={el.urls.regular}
                    alt={el.description}
                    height={300}
                    width={200}
                  /&amp;gt;
                &amp;lt;/div&amp;gt;
              ))}     
      &amp;lt;/section&amp;gt;
    &amp;lt;/&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now after we map the images they should lay normally like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nVE6gxA7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2k6f82py8brgy0mpduz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nVE6gxA7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2k6f82py8brgy0mpduz.png" alt="Creating Pinterest layout with tailwind CSS" width="800" height="1347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And after we add the class of columns to the containing section of whole images, we will use a 7 column layout for this one, and after changing the code to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;section className='columns-7 max-w-7xl mx-auto space-y-4'&amp;gt;
        {photos.map((el) =&amp;gt; (
          &amp;lt;div key={el.id} className='rounded-md overflow-hidden'&amp;gt;
            &amp;lt;img
              src={el.urls.regular}
              alt={el.description}
              height={300}
              width={200}
            /&amp;gt;
          &amp;lt;/div&amp;gt;
        ))}
&amp;lt;/section&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will see the layout work like magic with no extra work, we get a beautiful layout&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GMAmGl8d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbssxgwxpf98nqdzwlk9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GMAmGl8d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbssxgwxpf98nqdzwlk9.png" alt="Creating Pinterest layout with tailwind CSS" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All right that's it, that is how we can achieve Pinterest like layout in a simple way using CSS columns, in next.js with Tailwindcss.&lt;/p&gt;

&lt;p&gt;If you read all the way, thanks for reading the whole blog.&lt;/p&gt;

&lt;p&gt;Any comments would be much appreciated.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;About Me: My name is Sabin. I'm a full stack web developer and designer based in Pokhara, Nepal. I'm new to writing blogs and will continue to write hopefully 🤞. I focus primarily on the frontend with Next.js &amp;amp; React.js&lt;/em&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>css</category>
      <category>tailwindcss</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
