What is the best way to ensure that my React-Tailwind navbar and hero elements are optimized for screen height?

I'm facing a challenge with my current project, which is a React project integrated with Tailwind CSS. I'm attempting to style the main elements (navbar and hero) to fit the entire screen height when the user lands on the page. If you have a solution using pure CSS, I'm open to that as well since I can later convert it to Tailwind.

Below is my existing code:

Main element (not sure if useful)

import Header from "./header";
import Hero from "./hero";

export default function OnBoardingMain() {
    return (
        <div className="flex flex-col w-full h-full min-h-full ">
            <Header />
            <Hero />
        </div>
    );
}

Hero element

/* eslint-disable @next/next/no-img-element */
"use client";
import { useState } from "react";
import { Dialog } from "@headlessui/react";
import { Bars3Icon, XMarkIcon } from "@heroicons/react/24/outline";
import Image from "next/image";
import CtaMainButton from "./CtaMainButton";

export default function Hero() {
    const [mobileMenuOpen, setMobileMenuOpen] = useState(false);

    return (
        <div className="bg-gray-900 height-full top-0 left-0">
            <div className="relative isolate overflow-hidden pt-14">
                <img
                    src="image"
                    alt=""
                    className="absolute inset-0 -z-30 h-full w-full object-cover"
                />
                <div
                    className="absolute inset-x-0 -top-40 -z-10 transform-gpu overflow-hidden blur-3xl sm:-top-80"
                    aria-hidden="true"
                ></div>
                <div className="ml-4 max-w-3xl py-32 sm:py-48 lg:py-56 animate-slide-in">
                    <div className="text-center">
                        <h1 className="text-4xl font-bold tracking-tight text-sky-800 sm:text-6xl">
                        text
                        </h1>
                        <div className="mt-10 flex items-center justify-center gap-x-6 ">
                            <CtaMainButton />
                        </div>
                    </div>
                </div>
                4
                <div className="absolute inset-0 bg-gradient-to-r from-slate-400 from-30% to-transparent opacity-50 -z-20"></div>
            </div>
        </div>
    );
}

Header:

/* eslint-disable @next/next/no-img-element */
"use client";
import { useState } from "react";
import { Dialog } from "@headlessui/react";
import { Bars3Icon, XMarkIcon } from "@heroicons/react/24/outline";
import Image from "next/image";
import CtaMainButton from "./CtaMainButton";

const navigation = [
    { name: "À propos", href: "#" }
];

export default function Header() {
    const [mobileMenuOpen, setMobileMenuOpen] = useState(false);

    return (
        <header className="bg-white ">
            <nav
                className="mx-auto flex max-w-7xl items-center justify-between p-6 lg:px-8"
                aria-label="Global"
            >
                <div className="flex lg:flex-1">
                    <a href="#" className="-m-1.5 p-1.5">
                        <span className="sr-only">Your Company</span>
                        <img
                            className="h-8 w-auto"
                            src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=600"
                            alt=""
                        />
                    </a>
                </div>
                <div className="flex lg:hidden">
                    <button
                        type="button"
                        className="-m-2.5 inline-flex items-center justify-center rounded-md p-2.5 text-gray-700"
                        onClick={() => setMobileMenuOpen(true)}
                    >
                        <span className="sr-only">Open main menu</span>
                        <Bars3Icon className="h-6 w-6" aria-hidden="true" />
                    </button>
                </div>
                <div className="hidden lg:flex lg:gap-x-12">
                    {navigation.map((item) => (
                        <a
                            key={item.name}
                            href={item.href}
                            className="text-sm font-semibold leading-6 text-gray-900"
                        >
                            {item.name}
                        </a>
                    ))}
                </div>
                <div className="hidden lg:flex lg:flex-1 lg:justify-end">
                    <CtaMainButton />
                </div>
            </nav>
            <Dialog
                as="div"
                className="lg:hidden"
                open={mobileMenuOpen}
                onClose={setMobileMenuOpen}
            >
                <div className="fixed inset-0 z-10" />
                <Dialog.Panel className="fixed inset-y-0 right-0 z-10 w-full overflow-y-auto bg-white px-6 py-6 sm:max-w-sm sm:ring-1 sm:ring-gray-900/10">
                    <div className="flex items-center justify-between">
                        <a href="#" className="-m-1.5 p-1.5">
                            <span className="sr-only">Your Company</span>
                            <img
                                className="h-8 w-auto"
                                src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=600"
                                alt=""
                            />
                        </a>
                        <button
                            type="button"
                            className="-m-2.5 rounded-md p-2.5 text-gray-700"
                            onClick={() => setMobileMenuOpen(false)}
                        >
                            <span className="sr-only">Close menu</span>
                            <XMarkIcon className="h-6 w-6" aria-hidden="true" />
                        </button>
                    </div>
                    <div className="mt-6 flow-root">
                        <div className="-my-6 divide-y divide-gray-500/10">
                            <div className="space-y-2 py-6">
                                {navigation.map((item) => (
                                    <a
                                        key={item.name}
                                        href={item.href}
                                        className="-mx-3 block rounded-lg px-3 py-2 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50"
                                    >
                                        {item.name}
                                    </a>
                                ))}
                            </div>
                            <div className="py-6">
                                <CtaMainButton />
                            </div>
                        </div>
                    </div>
                </Dialog.Panel>
            </Dialog>
        </header>
    );
}


I've experimented with various Tailwind classes like "flex flex-col h-screen" or "h-full" on different elements, but unfortunately, none of them seemed to work as intended.

Answer №1

To adjust the main component to fit the height of the screen, make the following change:

<div className="flex flex-col w-full h-screen">

For the hero component, ensure it fills the remaining space in the vertical layout after the header component. Remove any unnecessary parent elements for positioning:

<div className="bg-gray-900 grow relative pt-14">
  <img
    src="image"
    alt=""
    className="absolute inset-0 -z-30 h-full w-full object-cover"
  />
  …

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

Error: The 'book' property is undefined and cannot be read in the BookDetails.render function

I am currently working on retrieving data from renderList and implementing it in render(). Upon using console.log this.renderList() https://i.stack.imgur.com/IwOzw.png The retrieved data is displayed above. While working on the render(), I attempted ...

I encountered an issue where my style.css file was not properly linking to my HTML code. Whenever I saved the file, it would open

I'm a beginner in the world of HTML/CSS. When I save my style.css file, it shows up as a Notepad+ settings file and doesn't seem to work with my HTML. Can anyone advise me on what steps I should take? ...

Marquee is experiencing issues with incomplete scrolling

Hello everyone, I have been trying to implement a stock market ticker on my website. Initially, I used the marquee tag and it worked fine. Then I tried using simple scroll within the PHP UserCake user management system, but unfortunately, it is not working ...

Having trouble with the functionality of the Office-UI-fabric DocumentCardActions

I've integrated the office fabric ui DocumentCardActions component into my app, but I'm facing an issue where clicking the action button does not trigger the desired functionality. <DocumentCard styles={cardStyles} > <div className={ ...

How can I assign a background image to a specific state or template using AngularJS?

While attempting to style the entire template with a CSS div tag, I encountered an issue. The code snippet used was: <div ng-style="{'background-image': 'url(/images/food.png)', 'background-repeat': 'repeat-y'}"& ...

Obtain the following image with every click

I have a div with images inside. I created two arrows (previous, next) within a larger div using the src of one of the images as the background URL for each arrow. My goal is to make the next arrow change the large image to the src of the following image w ...

Extract the variables from the while loop

Is there a way to store the variables outside of the while loop? For instance, I have a slideshow displaying images for each category and a button that when clicked leads to the image gallery related to that category. However, due to graphical reasons, the ...

How to eliminate the external white border from a Java applet when it is integrated into an HTML webpage

As someone experienced in dotnet, I decided to venture into creating a Java applet for my application. After successfully developing and signing the applet, it functioned perfectly within my application. However, one issue perplexed me - when I attempted ...

Send the 'key' parameter to the React component

Attempting to pass a parameter named "key" to a react component is resulting in it not functioning properly. However, when I use "keyy" instead of "key," it works as intended. It appears that using "key" as a parameter name may be causing issues due to it ...

Creating a React child component with conditional rendering is a useful technique to dynamically display

Typically, we create child components in React like this: const component =(<button>Bla Bla</button>) But what if you want to create it using a conditional statement? You might try something like this: const component =(()=>{ if(true){ re ...

Reactjs encountering issues with function of Cookies section

Currently, I am working on a login module in Reactjs using Nextjs. Upon successful login, I am storing the user email in a cookie. The functionality is working as expected, but I want to restrict access to the login page for users who are already logged in ...

What is the best way to present both paragraphs and images from a div in a sequential order

Currently, I am developing a compact web tool to assist editors in creating content by utilizing buttons to insert paragraphs and images. The elements are saved with an ID (beginning at 0 and increasing for each new element) and can be previewed in a div n ...

Multiple dropdowns/popovers causing trouble in NextUI interface

I am encountering a problem with NextUI where I have to double-click to open a new popover or dropdown because the previous one needs to be closed first. Is there a way to resolve this issue? Here is the code snippet: <Popover className="bg-white ...

Showing headings in the table vertically

I have a header1 and header2 with corresponding data1 and data2 that I want to display differently. h h e e a a d d e e r r 1 2 data1 data2 To enhance the presentation, I wish to add borders around the head ...

Ways to turn off JavaScript when reaching a certain breakpoint, similar to a media query

I am facing an issue with my <header> and <nav> blocks which are impacted by JavaScript. Is there a way to create a JavaScript solution similar to a media query that would deactivate this JavaScript code if the window width is less than or equa ...

Bring div button on top of the contenteditable field

I am working on an Angular app for a client and need to implement a clickable button at the bottom right of a contenteditable element, similar to the image shown below : https://i.sstatic.net/J6XdW.png The challenge is that the content needs to be scroll ...

What is the best way to simulate axios API calls in Jest for mocking purposes?

I am currently testing a Vuex action async function that calls an API using axios. However, I am facing an issue where I am getting an error message that says: "TypeError: Cannot destructure property data of 'undefined' or 'null'." 3 ...

Incorporate a center line into the Bootstrap grid without disrupting any of the current layouts

I am attempting to achieve a similar effect to the one displayed here, with a white line and circles running down the center of a single-page layout, using bootstrap 3. My initial thought was to have a column with a right border spanning the entire layout ...

Is it possible for a destructed assignment to yield multiple objects?

I am faced with a collection of Storybook stories and a function that produces a ComponentStory object. I am aiming to find a concise method for duplicating multiple stories without resorting to repetitive code like this: export const Default = bindStory(T ...

What is the best method to implement real-time data updates in a single page application

Looking for a solution to update the data in my SPA web app that utilizes next js and express js. How can I ensure the application on computer 1 reflects changes made by computer 2 without requiring a manual refresh? ...