Struggling to align a div in the middle using react and tailwindcss

Struggling to center a div in React with Tailwind, no matter what method I try. Here is my code:

"use client";
import Image from "next/image";
import React from "react";

const LoginPage = () => {
  return (
    <div className="flex h-screen">
      <div className="flex-shrink-0 p-4">
        <Image
          src="/big-logo-black-ai.svg"
          alt="Logo"
          className="w-12 h-12"
          width={400}
          height={40}
        />
      </div>

      <div className=" flex justify-center items-center ">
        <div className="w-full max-w-md">
          <h1 className="text-2xl font-bold mb-4">Login</h1>
          <form>
            <div className="mb-4">
              <label
                htmlFor="email"
                className="block text-sm font-medium text-gray-700"
              >
                Email
              </label>
              <input
                type="email"
                id="email"
                name="email"
                className="w-full border border-gray-300 rounded-md px-3 py-2 focus:outline-none focus:ring focus:border-blue-500"
                placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8df4e2f8cde8f5ece0fde1e8a3eee2e0">[email protected]</a>"
              />
            </div>
            <div className="mb-4">
              <button
                type="submit"
                className="w-full bg-blue-500 text-white rounded-md py-2 font-semibold hover:bg-blue-600"
              >
                Submit
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  );
};

export default LoginPage;

Seeking assistance on how to properly center the div - Every attempt has resulted in it staying locked on the left side.

Answer №1

If you want to make the element take up the full width, consider adding the "w-full" class.

"use client";
import Image from "next/image";
import React from "react";

const LoginPage = () => {
  return (
    <div className="flex h-screen w-full">
     ...
    </div>
  )
}

Answer №2

If you are looking to center the "div" horizontally only, there is a simple solution for it in your code snippet:

const LoginPage = () => {
  return (
    <div className="flex h-screen">
     ...
    </div>
  )
}

The issue here is that the parent div's width is not set to full screen width, causing its children div not to be centered properly. To fix this, just like mentioned by another user earlier, set the parent div to occupy the full width of the screen:

const LoginPage = () => {
 return (
   <div className="flex w-full h-screen">
    ...
   </div>
 )
}

I hope this explanation clears things up for you!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to keep my fixed button at a consistent size while zooming on mobile devices?

There are no definitive answers to the questions regarding this issue. Some suggest stopping zoom altogether, while others recommend setting the width, which may not always solve the problem. I am working on a web application designed for mobile use with ...

Determine the altered props within the componentWillReceiveProps lifecycle method

During the process of debugging React code, it is common to come across situations where componentWillReceiveProps gets triggered unexpectedly, but identifying which prop change is causing this issue can be challenging. Is there a method available to easi ...

The image is failing to load - local resources are prohibited from rendering

I am facing a challenge while trying to render an image as it keeps showing the error "Not allowed to load local resource:" Here is the code snippet: fileUpload.js const multer = require("multer"); const path = require("path"); const ...

Styling Process Steps in CSS

Just starting out with CSS! I'm looking to replicate the Process Step design shown in the image. https://i.stack.imgur.com/Cq0jY.png Here's the code I've experimented with so far: .inline-div{ padding: 1rem; border: 0.04rem gray ...

What could be causing the Universal Code for Disqus to not function properly?

Struggling to get this code to work on my website. I've tried everything, from clearing caches and cookies to disabling plugins and extensions, but still no luck. Check out the code below: import React, { Component } from 'react' import { ...

What is causing the failure of the state to be inherited by the child component in this scenario (TypeScript/React/SPFX)?

For this scenario, I have a Parent class component called Dibf and a Child class component named Header. While I can successfully pass props from the Parent to the child, I am encountering difficulties when trying to pass state down by implementing the fo ...

Tips on how to eliminate the default button classes in material UI and implement your own custom CSS class

I am completely new to React and material UI. I have just started using the material UI button component, and I am looking to customize it by removing the default button classes that are applied (MuiButtonBase-root MuiButton-root MuiButton-text makeStyles- ...

Enhance your Next.js application with language-based metadata using next-intl

Before rendering the page, you can define metadata utilizing next-intl. This allows you to obtain the locale from the parameters passed to the renderer function. Is there a way to set metadata based on the locale? I attempted the following: en.json: { ...

What is the best way to incorporate a transition on transform using Styled-components?

I attempted to add a transition on the transform property, but unfortunately, it did not produce any visible changes. I tested other properties such as: background-color, color... and they worked perfectly fine. live code: source code: // styled-compo ...

Can a File Object be transmitted to an AWS Lambda function via API Gateway using JavaScript?

Can a JavaScript File Object be passed from a browser to AWS Lambda via API Gateway (ultimately to S3)? I am working with TypeScript and React. Environment Frontend TypeScript React AWS Amplify (for API module) Backend(AWS Lambda) Node.js Expecta ...

Enhance Navbar functionality in Next.js to dynamically update when switching to a different route

Objective: Customizing the Navbar accent based on the active page Current Issue: The Navbar accent is not updating in real-time when navigating between pages. It only updates to the last visited page instead of dynamically changing as per the current page ...

Guide to dividing a URL in reactjs/nextjs

Here is the complete URL: /search-results?query=home+floor&categories=All+Categories. I am looking to separate it into two sections - /search-results and query=home+floor&categories=All+Categories. My objective is to extract the second part of t ...

Issues with hover menu arise following relocation of primary menu

#header { height: 108px; width: 1140px; background-color: #faa220; position: relative; } #Logo2 { height: 108px; float: left; } #header links { height: 50px; float: left; list-style: none; padding-top: 10px; } #container { overflo ...

"Exclusive Choice" feature in the JSON Schema Form (Retool) for selecting multiple options

While working in Retool with JSON Schema Form, I attempted to create a multiple select input that would not allow me to select options that had already been selected (a "unique select"). However, the result was a multiple select input that did allow me to ...

What steps do I need to take to modify the MUI Badge component and insert custom text inside?

Is there a way to replace the number with a label next to a new row added to my table using MUI Badge? For example, instead of displaying a number like 4, I want it to show the word "New" as shown in this image: enter image description here This is the co ...

Struggling to remove the translucent effect when hovering over the share button

I have been struggling with a seemingly simple issue for a few hours now. On my website, I have some share buttons located at . Although I want these buttons to remain completely black when hovered over, they are inexplicably becoming slightly transparen ...

Step-by-step guide to creating a transition effect when the input changes

I'm looking to add a unique effect to my dropdown menu My goal is to create an effect in which the placeholder moves up and the new value seamlessly takes its place, using JS, jQuery, CSS, and HTML. View before transition View after transition ...

The RSuite Cascader search field is malfunctioning and not responding to input

I have integrated several RSuite components into a mat-ui app and everything seems to be functioning well. However, I am facing an issue with the Cascader component, which is crucial for my project. Despite fixing the popup display issue by adjusting the z ...

Mobile page sliding mechanism

My website contains a div that is mostly off the page, but on hover it translates onto the main page. You can check out my website. However, this method doesn't work well on mobile devices. Hovering is not effective and I often have to click multipl ...

Start up a server-side JavaScript instance utilizing Express

My journey into web programming has led me to learning JavaScript, Node.js, and Express.js. My ultimate goal is to execute a server-side JavaScript function (specifically a function that searches for something in a MySQL database) when a button is pressed ...