What is the best way to align text in the middle on smaller devices like phones and iPads?

I have been working on a login screen and encountered a problem with the layout on smaller devices. Despite centering all the content perfectly on my computer's browser, when I inspect it using developer tools for smaller devices, everything shifts to the left of the screen. Below is the code snippet for the page:

<TypeUser/>
      <div className="center">
        <div
          className="w-100 align-self-start p-5" style={{ maxWidth: 700 }}>
          <h1
            className="text-center  text-light">
            Register / Log in
          </h1>
          <p className="text-center pb-3 mb-3 text-light">
          Discover the best events in your city.
          </p>
          <form onSubmit={requestOTP}>
            <div className="row">
              <div className="col-12">
                <div className="position-relative mb-4">
                  <label htmlFor="phoneNumberInput" className="form-label fs-base text-light">
                    Log in with your phone number
                  </label>
                  <input
                    type="tel"
                    id="phoneNumberInput"
                    value={phoneNumber}
                    onChange={(event) => {
                      setPhoneNumber(event.target.value)
                      setExpandForm(false)
                      setErrorMessage(null)
                    }}
                    className="form-control form-control-lg"
                    placeholder='Mobile phone number'
                  />
                </div>
                { expandForm === true
                  ? <>
                    <div className="position-relative mb-4">
                    <label htmlFor="otpInput" className="form-label fs-base text-light">Verification code</label>
                      <input
                        type="number"
                        id="otpInput"
                        value={OTP}
                        className="form-control form-control-lg"
                        placeholder='Verification code'
                        onChange={(event) => {
                          setOTP(event.target.value)
                        }}
                      />
                    </div>
                    <button
                      type='button'
                      disabled={OTP.length <= 5}
                      className="btn btn-lg btn-primary btn-block mb-3"
                      onClick={(event) => {
                        setErrorMessage(null)
                        verifyOTP(event)
                      }}
                      >
                        {`${isLoading ? 'Validating...' : 'Validate code'}`}
                      </button>
                  </>
                  : null
                }
                <AlertErrorForm messageError={errorMessage} />
                {
                  expandForm === false
                    ? <button type="submit" disabled={phoneNumber.length <= 12} className="btn btn-lg btn-primary btn-block mb-3">Request code</button>
                    : null
                }
                  <div id="recaptcha-container"></div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </>
  )
}

In addition, here is the CSS code that I utilized to center everything in the browser view:

.center{
  padding-top: 8%;
  padding-left: 25%;
  text-align: center;
}

Answer №1

After some trial and error, I finally found the right CSS code for my needs:

  .middle{
  display: flex;
  align-items: center;
  justify-content: center;
  }

Feel free to customize it further by adding padding or other styles.

Answer №2

It's possible that the div size is too large. To address this, consider incorporating a media query in your CSS to adjust the size of the div based on the viewport of mobile and tablet devices. Here is an example snippet you can include at the end of your CSS file:

@media only screen and (max-width: 600px) { 
  .center {
    max-width: 200px; //adjust as needed
  }
}

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

The integration between Material UI's selectField and Redux forms is currently experiencing issues

Currently, I am working with materialUi along with redux form and have the following code snippet: NumbersSelector.js import React from 'react'; import SelectField from 'material-ui/SelectField'; import MenuItem from 'material-u ...

Implement a grid layout for columns within the profile section

Each user on my website has a profile tab that displays their submitted posts. To showcase these posts, I utilize the following code: <?php while ($ultimatemember->shortcodes->loop->have_posts()) { $ultimatemember->shortcodes->loop-> ...

Encountering an issue when trying to set up a new React project. Installation has been canceled due to an error with code number -4094, labeled as 'UNKNOWN', related to the system call

Encountering an error while attempting to create a React app E:\React>create-react-app ecomerce Setting up a new React application in E:\React\ecomerce. Installing necessary packages. This process may take a few minutes. Installing reac ...

The Stylish Choice: Materialize CSS Dropdown Selector

I am currently integrating Materialize CSS into my application and have used media queries to ensure that the layout is responsive. However, I am facing an issue with a select dropdown element. It works fine on laptops but does not allow for selecting any ...

Configuring the text box to utilize jQuery's datepicker feature

Currently, I am facing an issue with setting up a datepicker in a dynamic control creation scenario. The control is being created on the fly, causing inconsistencies with the id, which in turn is preventing the datepicker from functioning properly. Here is ...

The server encountered an error: TypeError - It is not possible to convert undefined or null into an

Check out this Code import { getProviders, signIn as SignIntoProvider } from "next-auth/react" function handleSignIn({ providers }) { return ( <> {Object.values(providers).map((provider) => ( < ...

Encountering an unanticipated reference error while attempting to paste the data

// Modify the layout function document.addEventListener('DOMContentLoaded', function() { function modifyLayout(productId) { // Referencing the original card and detailed card const originalCard = document.querySelector(&ap ...

What are the steps to developing a responsive tile using HTML and CSS?

I am exploring the functionalities of CSS and responsive design. To better understand how it works, I created a simple example which can be accessed here. This demonstration showcases a basic tile template. My goal is to display my tiles in 2, 3, 4, or m ...

Strategies for passing tag ID to onClick function in React

I am trying to pass the id of a <th id='1' onClick={this.click.bind(this)}> tag to an onclick function. The goal is to print the id of the <th> in the console.log within the click() function. Here is the code snippet: <thead> ...

Streamlining the process of formatting URLs?

I was pondering if there is a more efficient method to accomplish this task. I extract links from a webpage, but many of them are relative. for example /index.html for instance /home.index.html Currently, my approach involves adding the home URL to compe ...

Interactive image grid with adjustable description field per image upon selection

My goal is to create a grid of images with a single text field below the grid. This text field should display the description of the image that was last clicked. The grid is implemented using floating divs within a main div, as shown in the code snippet be ...

Incorporating a hyperlink into an HTML submit button

Recently, I started working with MySQL and decided to create a basic registration page. However, upon clicking the submit button, I would like to be redirected to another page, possibly a login page as seen on many websites. I am struggling with figuring ...

The background color is showing the wrong shade

So, I recently created a simple HTML5 website and decided to set the background-color of the header div to #3D7D99. However, when I view it in the browser, it appears as #35728E. Can anyone explain what might be causing this discrepancy? UPDATE: Just to c ...

Utilizing PHP to create an interactive website

As a novice PHP developer, I took to Google in search of tutorials on creating dynamic PHP websites. What I found was that many tutorials used the $_GET variable to manipulate URLs and make them appear like this: example.com/?page=home example.com/?page= ...

Which CSS properties can I implement to ensure that the text remains legible regardless of the background behind it?

Looking ahead, I am displaying an issue where the colors are almost perfect, but when different percentages are applied, some or all of the text becomes obscured. My goal is to assign the font color based on the background difference. I vaguely remember s ...

Removing the Shadow from Material UI's Dialog Box

I am currently struggling with the Material UI dialog component that displays information about a location marker. My issue is with disabling the unattractive box shadow that surrounds the component. Despite setting box-shadow: none and trying different so ...

What would be the optimal type for the second argument of the `simulate` method?

When using the simulate function, I am familiar with code like this: simulate("change", { target: { value: '7' } }); However, if my onChange function requires an object as a parameter, what should I pass in the second argument? interface myObj ...

Ways to showcase the contents of a React JavaScript document, or more specifically, a file designed for React interpretation

After trying multiple solutions, I found a conceptual guide on How to call and form a React.js function from HTML. However, the HTML generated doesn't seem to be calling from the JavaScript file as expected. It appears identical to what is mentioned i ...

What is the best way to verify the presence of an element in an array within NextJS?

Recently, I started diving into the world of React/Next JS. However, my learning journey has been a bit inconsistent due to landing a new job. Now, I'm faced with a challenge - I need to create an array of numbers and then implement an input field to ...

Designing a website using HTML and CSS with the goal of optimizing it to fill

My website includes all the recommended elements in the head area: <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta name="viewport" content="width=device-width, ...