Struggling with the alignment of divs in a vertical manner

Looking for a way to align multiple divs inside another div vertically with even spacing between them? They should all start at the top.

Currently, my solution has the child-divs positioned at the top-left corner of the parent div (see the first picture):

<div className="flex-col items-center justify-center h-screen w-full">
    <div>
        <input type='text' placeholder='Game Code' onChange={(event) => setRoomCode(event.target.value)} />
        <button className="text-white ml-3" type="submit" onClick={handleEnterCode}>JOIN GAME</button>
    </div>
    <div>
        <h1 className="text-white">OR</h1>
    </div>
    <div>
        <button className="text-white" type="submit" onClick={e => setShowForm(true)}>CREATE GAME</button>
    </div>
    <div>
        <form onSubmit={handleLogout}>
            <button className="text-white" type="submit">logout</button>
        </form>
    </div>
</div>

However, when I switch to using "flex," the children are placed much further down towards the bottom (as shown in the second picture).

First picture

<div className="flex-col flex items-center justify-center h-screen w-full">
    <div>
        <input type='text' placeholder='Game Code' onChange={(event) => setRoomCode(event.target.value)} />
        <button className="text-white ml-3" type="submit" onClick={handleEnterCode}>JOIN GAME</button>
    </div>
    <div>
        <h1 className="text-white">OR</h1>
    </div>
    <div>
        <button className="text-white" type="submit" onClick={e => setShowForm(true)}>CREATE GAME</button>
    </div>
    <div>
        <form onSubmit={handleLogout}>
            <button className="text-white" type="submit">logout</button>
        </form>
    </div>
</div>

Second picture

Answer №1

To create a parent container class, include the following properties:

.parent-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}
<div class="parent-container" className="flex-col items-center justify-center h-screen w-full">
<div>
    <input type='text' placeholder='Game Code' />
    <button className="text-white ml-3" type="submit" onClick={handleEnterCode}>JOIN GAME</button>
</div>
<div>
    <h1 className="text-white">OR</h1>
</div>
<div>
    <button className="text-white" type="submit">CREATE GAME</button>
</div>
<div>
    <form onSubmit={handleLogout}>
        <button className="text-white" type="submit">logout</button>
    </form>
</div>
</div>

Answer №2

My ideal scenario would look something like this:

-------------------------
|          div          |
|                       |
|          div          |
|                       |
|          div          |
-------------------------

I prefer not to use CSS files and instead rely on tailwind for styling:

This is how it appears currently:

<div className="flex-col flex items-center justify-center h-full w-full">
                    <div>
                        <input type='text' placeholder='Game Code' onChange={(event) => setRoomCode(event.target.value)} />
                        <button className="text-white ml-3" type="submit" onClick={handleEnterCode}>JOIN GAME</button>
                    </div>
                    <div>
                        <h1 className="text-white">OR</h1>
                    </div>
                    <div>
                        <button className="text-white" type="submit" onClick={e => setShowForm(true)}>CREATE GAME</button>
                    </div>
                    <div>
                        <form onSubmit={handleLogout}>
                            <button className="text-white" type="submit">logout</button>
                        </form>
                    </div>
                </div>

... transitioning into this final design:

view current output

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

Using a junction table for many-to-many relationships in React-admin

I have created a schema and utilized it in react-admin. Can you provide feedback on whether this is the most effective method for retrieving data from a junction table? const Media = () => <ReferenceManyField label="Kiosks" source=& ...

What are the possible reasons for a checkbox not being checked in React JS?

I've been working with react final form and I'm encountering an issue where I can't seem to get the checkbox to be properly checked. I'm not sure what mistake I might be making. Take a look at my code on CodeSandbox const AppWithIconTo ...

Is it possible to convert PDF documents to HTML using languages such as C, C++, or Java?

My current project involves annotating HTML files using Javascript, and I need to convert PDF files into HTML files specifically for the IOS platform. While I have had some success in annotating HTML pages, I am unsure how to go about converting PDF to H ...

Troubleshooting the functionality of the Material-UI LinearProgress bar

I've recently implemented a simple file upload feature using the react-dropzone library in my project. To enhance this functionality, I decided to incorporate the material-ui LinearProgress bar to visually represent the upload progress. Below is the ...

Why does it seem like only one div is being added?

I am facing an issue with dynamically appending multiple div elements. Despite my efforts, only one div element is showing up on the browser when I try to test the code. I have searched for similar problems but could not find any solutions. Any assistanc ...

Add CSS styling to input text that is not empty

Is it possible to achieve this using CSS3 alone? var inputs = document.getElementsByTagName("INPUT"); for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == "text") { if (inputs[i].value != "") { inputs[i].s ...

How can we use Jquery to add animation effects to our styling

I am a beginner with JQuery and struggling to make the code below work correctly. $('.announcement_panel_button').click(function(e){ $('#site_logo').animate({ 'margin-top': '5px' }, 5000); e.pre ...

Creating a new tab with the same html template binding in Angular 4

Could a button be created that, when clicked, opens a new browser tab featuring the same component and variables declared previously? <label>Please Enter Your Email Below</label> <input name="userEmail" type="text" class="form-control" re ...

Begin the Material UI Datepicker from the first day of the upcoming month

I'm trying to see if it's possible to set the default current date of a material-UI picker to the first day of next month, but I haven't been able to find any information on how to do this. Can anyone confirm if there is an option for that? ...

The React component designed to consistently render video frames onto a canvas is unfortunately incompatible with iOS devices

I'm facing an issue with my code snippet that is supposed to draw video frames on a canvas every 42 milliseconds. It's working perfectly on all platforms and browsers except for iOS. The video frames seem unable to be drawn on canvas in any brows ...

Begin by positioning flex elements outside of the screen and then animate them into their final

I'm struggling to find the right approach to make this animation work effectively. Currently, I have two elements centered vertically within a flexbox layout. My goal is to animate these elements into their final positions by sliding in from off-scre ...

Switch navigation - always display the menu on the existing page

I have a toggle menu implemented. Please check out the code and functionality on JsFiddle When clicking on a h3 tag like Category 1 (which is an a tag), the menu opens and remains open on the current page. However, if you click on the h3 tag (Category1) ...

Having trouble getting the carousel slider to function properly on meteor?

I am currently working on my portfolio page using Meteor, and I want to include a carousel slider gallery. The problem I'm facing is that while the first gallery works fine, the second one does not load correctly and just displays a list of pictures. ...

Activate the Keypress event to update the input value in React upon pressing the Enter

I am facing an issue where I need to reset the value of an input using a method triggered by onPressEnter. Here is the input code: <Input type="text" placeholder="new account" onPressEnter={(event) => this.onCreateAccount(event)}> < ...

Which HTML tag should I choose to apply color to text in Twitter Bootstrap?

I'm looking to enhance the appearance of a specific word in my text. For example: This is my text, <span class="red">this</span> should be red. Would using the span tag be the appropriate method for achieving this effect? Or are there ot ...

Deleting a property once the page has finished loading

My issue is a bit tricky to describe, but essentially I have noticed a CSS attribute being added to my div tag that seems to come from a node module. The problem is, I can't seem to find where this attribute is coming from in my files. This attribute ...

Add the specified HTML tag to the existing document. An error has occurred: HierarchyRequestError - The action would result in an invalid node

During my testing of a React/TypeScript project using Jest + Enzyme, I encountered an issue when trying to append an HTML tag. The error occurred with the following unit test code: const htmlTag: HTMLElement = document.createElement('html'); htm ...

How can I programmatically adjust the center of a Google Maps v3 map?

I have a table with 2 columns. The first column contains a div with a Google map, while the second column has a panel that changes its width after a few seconds. On the map, there is a marker placed. Issue: When I click on a button that triggers setCente ...

Does the JavaScript Map Function Operate Asynchronously?

I've been making changes to the state element properties within a map computePiePercentages(){ var denominator = 1600 if (this.state.total < 1600){ denominator = this.state.total } return this.state.pieChartData.map((item, ...

Having trouble with the production deployment of a Dockerized React application

As I work on containerizing my create-react-app for production using a Dockerfile and Docker-compose.yml, I am facing a challenge in creating the production build. The issue arises from the fact that the scripts required to run the build command are listed ...