skip to Main Content

I’m trying to build a basic website as my first project using React and Tailwind CSS. I have made three components namely – Header, Input and Footer in my react app. My header is placed at the top by default. Next comes my input component where I take input from user and want this component to fill the rest of the space with input fields center middle top. I want my footer to be pinned to bottom.

This is what I want to achieve exactly.

Required Layout

Below is my react code:

App.js:

import React from 'react';
import Header from "./components/Header";
import Footer from './components/Footer';
import Input from './components/Input';

export default function App() {
  return (
    <div className='flex flex-col min-h-screen'>
    <Header />
    <Input />
    <Footer />
    </div>
  )
}

Header.jsx:

import React from 'react';

function Header() {
    return (
        <div className='basis-full mt-5 mb-5'>
            <h1 className="text-5xl font-bold text-center text-blue-1">
                Grocery Tracker
            </h1>
        </div>
    );
}

export default Header;

Input.jsx:

import React from 'react';

function Input(){
  return(

    <div className='flex justify-evenly'>
      <form action="/" method="post">
      <label  for="itemName">Enter your item: </label>
      <input className="border border-gray-500 focus:border-black cursor-text px-2 ml-2 mr-2" autoFocus type="text" name="items" id="items" placeholder="Tomatoes, Milk, Atta etc."/>
      <label  for="itemQuantity">Enter item quantity: </label>
      <input className="border border-gray-500 focus:border-black cursor-text px-2 ml-2 mr-2" autoFocus type="text" name="items" id="items" placeholder="1 Kg, 2 Packets, 500 ml etc."/>
      <label  for="itemPrice">Enter thr price: </label>
      <input className="border border-gray-500 focus:border-black cursor-text px-2 ml-2 mr-2" autoFocus type="number" name="items" id="items" placeholder="Rs. 100, Rs. 56, Rs. 47 etc."/>
      </form>
      </div>
  )
}

export default Input;

Also, when i add flex-grow property to my input flex, then the justify-evenly is not working.

Footer.jsx:

import React from 'react';
import indianFlag from '../images/ind.jpg';

function Footer(){
  return(
    <div className='flex items-end pt-2 justify-center end gap-2 mb-5'>
        <div>
            <p text-color='black'>
                Copyright© 2023 | Made in Vizag, India  
            </p>
        </div>
        <div>
        <img height='30px' width='30px' src={indianFlag} alt='Indian Flag' />
        </div>
    </div>
  );
}

export default Footer;

My current Layout

2

Answers


  1. To achieve this, first add position:relative to the body.
    Update the styles in Footer.jsx like this

    
    import React from 'react';
    import indianFlag from '../images/ind.jpg';
    
    function Footer(){
      return(
        <div className='absolute bottom-0 w-full pt-2 mb-5'>
            <div>
                <p text-color='black'>
                    Copyright© 2023 | Made in Vizag, India  
                </p>
            </div>
            <div>
            <img height='30px' width='30px' src={indianFlag} alt='Indian Flag' />
            </div>
        </div>
      );
    }
    
    export default Footer;
    
    

    This will pin your footer to the very bottom of the page.

    Login or Signup to reply.
  2. You can use some "generic" CSS and grid which gives us the "holy grail" view with a header, left / main/ right and footer – just un-comment what you need/include on your page. I added some color and comments in the CSS to help with understanding what is where.

    Note: I used grid-areas: but also put in and commented out the grid-column: if that is the way you wish to say what is where.

    No header or sides: just un-comment a side for example if you need it.

    html,
    body,*{
      /* use full screen */
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .parent-container {
      min-height: 100%;
      display: grid;
      grid-template-rows: auto 1fr auto;
      grid-template-columns: auto 1fr auto;
      grid-template-areas: 
      'header header header' 
      'leftside main rightside'
      'footer footer footer';
    
      border: solid 1px green;
    }
    
    .header {
      padding: 2rem;
      grid-area: header;
      /*  grid-column: 1 / 4;*/
    }
    
    .left-side {
      grid-area: leftside;
      /*  grid-column: 1 / 2;*/
    }
    
    .main {
      grid-area: main;
      /*  grid-column: 2 / 3;*/
      display: block;
      padding: 0.25em;
      overflow: auto;
    }
    
    .right-side {
      grid-area: rightside;
      /* grid-column: 3 / 4;*/
    }
    
    .footer {
      grid-area: footer;
      /*  grid-column: 1 / 4;*/
      padding: 1em;
      /* just super center the footer */
      display: grid;
      place-items: center;
    }
    
    /* just colors below here to illustrate */
    .head-color {
      background-color: #FF000020;
    }
    
    .foot-color {
      background-color: #00FF0020;
    }
    
    .main-color {
      background-color: #FFB00020;
    }
    
    .left-color {
      background-color: #0000FF20;
    }
    
    .right-color {
      background-color: #FFB0FF20;
    }
    <div class="parent-container">
     <!--  <div class="header section head-color">Need a head I am it!</div> -->
      <div class="main section main-color">Just Main Content no sides or header</div>
      <!--
       <div class="left-side left-color">left I am not Yoda</div>
     <div class="right-side right-color">right I am the fun guy</div>
     -->
      <div class="footer foot-color section">super centered Footer still</div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search