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

When the CSS grid is equipped with a sticky left column, it will horizontally scroll if the grid's width exceeds 100% of its containing element

I am facing an issue with a CSS grid layout that has sticky left and right columns as shown below: .grid { display: grid; grid-template-columns: repeat(10, 200px); } .grid > div { border: 1px solid black; background-color: white; } .sticky- ...

Discover how to shallow test for the presence of a wrapped component using Jest and Enzyme

Currently, I am in the process of testing a component that dynamically renders another wrapped component based on certain conditions. My testing tools include enzyme and jest, with the root component being rendered using the shallow() method. The main chal ...

Is it possible to transfer items from one list within a component to another list inside a different component with Vue draggable?

I am having trouble implementing Vue Draggable to move items between lists within a component setup. The issue I'm facing is that the items can be moved within a list, but not from one list to another. The structure involves a Board component that ho ...

Explore the source code of your React app directly in your browser with React View

I noticed a strange behavior on my React website. When I try to view the source in Chrome, it doesn't display much of the HTML markup - mostly just JavaScript imports. However, if I click on an element with Dev tools open, it shows me the actual HTML ...

Tips on placing a button at the bottom of the AppBar and keeping it fully responsive

I am attempting to place my login and log out buttons at the end of the AppBar. When I try forcing it with marginLeft: '70%',, it works but only on large resolutions. On smaller resolutions, the buttons go out of place. Here is an example of forc ...

The browser is only showing particular changes made to the CSS file

I'm currently working on a web project and have the following CSS and HTML code: style.css: body { background-color: red; font-size: 14px; font-family: Roboto,arial,sans-serif color: gray; } img { height: 92px; width: 272px; ...

When transformed into clickable links, the list items start piling up on

Here are two groups of code along with a straightforward question that most people can answer easily, but still worth asking. Below is the initial piece of code with working CSS implementation: <div class="subTopHolder"> <ul class="language ...

Conceal the countdown clock and reveal the message box

I am attempting to create a functionality where the text box will replace the timer when it reaches 0, and then the timer will be hidden. I am seeking a straightforward solution using either the 'v-show' or 'destroy' property in vue.js ...

Tips for displaying only a list of folders in elfinder, a jquery file management plugin

Currently, I am working on enhancing the features of a file manager plugin that allows users to manage their folders effectively. One key functionality of the plugin is the ability for users to share specific folders with others. However, if a folder has n ...

Incorporating traditional Javascript classes for modeling in React development

Can traditional JavaScript classes be utilized in models within the MVC framework while using React, as opposed to relying on Redux or contexts & reducers which may impact reusability? If this approach is feasible, how can we efficiently 'subscribe&ap ...

Alert: Prop type error encountered - The prop 'open' must be defined in Snackbar component

Recently, I've been implementing jest snapshot tests into my application. The main focus is on the LoginForm component. render() { return ( ... <DynamicSnack dialogOpen={this.props.dialogOpen} snackOpen={this.props.sna ...

What is the significance of using single quotation marks to enclose the 'raw' key in Tailwind?

While reviewing the Tailwind documentation on custom media queries, I came across an interesting option to create a fully custom breakpoint using the 'raw' key. After adding this to my configuration file, I noticed that when I saved the file, Pr ...

One way to incorporate type annotations into your onChange and onClick functions in TypeScript when working with React is by specifying the expected

Recently, I created a component type Properties = { label: string, autoFocus: boolean, onClick: (e: React.ClickEvent<HTMLInputElement>) => void, onChange: (e: React.ChangeEvent<HTMLInputElement>) => void } const InputField = ({ h ...

Firefox not displaying caret in Bootstrap dropdown

I am trying to display a caret on the right side of a bootstrap dropdown button inside a button-group. Here is the code snippet I am using: <div class="span3 well"> <div class="btn-group btn-block"> <a class="btn btn-primary dro ...

Learn How to Implement Styling in Material UI using Styled Components

Utilizing styled-components with MaterialUI integration. Encountering the following error in LoginTextField component and seeking a resolution. Error Message: [ts] Type '{ width: number; }' is missing the following properties from type &apos ...

Removing the switcher outline in Bootstrap Switch: a step-by-step guide

I have implemented the bootstrap-switch - v3.3.1, however, I want to remove the default blue outline that appears around the switcher when toggling it on or off. Despite setting style="outline: 0 none; for the input, the outline remains visible. Below is ...

An error with code -4058 occurred when I tried to execute the command npm run start-rewired

When I start my React application using npm with the command npm run start-rewired, I encountered an error that looks like this: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="265654494c434552661608170816">[email p ...

Modify the parent's style in CSS based on the presence of certain specified children

I'm working with an HTML element that can contain a child with the ID 'slideshow' <div id='content'> <div id='slideshow'> </div> </div> Alternatively, it could have a different child like t ...

Creating dynamic forms with JQuery and ReactJS

I have been tasked with creating a form-builder that will be integrated into an application. My role is to focus on designing the front-end using ReactJS. The client’s requirements are very similar to the features of the "jQuery Form-Builder," so I decid ...

The impact of adjusting the article's margin-top on the margin-top of

I want these two divs to have different positioning. The sidebar should remain fixed at the top. Why is the sidebar aligned with the article div instead of sticking to the top? body{ margin: 0; padding: 0; } div#sidebar{ background-color: yel ...