Creating a responsive div class that is centered

Struggling to center my <div class="loginfeld"> while keeping it responsive.

<html>
    <body>
        <div class ="loginfeld"> 
        <div class="container">
            <div class="col-lg-4"></div>
            <div class="col-lg-4">
                <div class="jumbotron">

                    <center><h1>Hello!</h1></center>
                    <br>

                    <form action="login.php" method="POST">
                    <div class="form-group">
                    <input type="text" class="form-control" name="username"
                    placeholder="Enter Username">

                    </div>

                    <div class="form-group">
                    <input type="password" class="form-control" name="password"
                    placeholder="Enter Password">

                    </div>

                    <button type="submit" class="btn
                    btn-primary form-control" name ="login">Login</button>
                    <center><p><?php echo $message; ?></p></center>
                    </form>
                </div>
            </div>
            <div class="col-lg-4"></div>    
        </div>
        </div>
    </body>
</html>

In my CSS style file:

.loginfeld{
    margin-top: 250px;
    margin-left: 750px;
    max-width: 1000px;
}

Currently, it is centered on the screen but lacks responsiveness. Attempts to use forum-suggested codes for responsive centering have not been successful.

After implementing the code suggested by K_LUN, it is now correctly centered but the login box layout is distorted. https://i.sstatic.net/0fDFB.png

Answer №1

Utilizing Bootstrap's vast array of classes allows you to accomplish your desired layout with minimal need for custom CSS.

I have removed the extra col-lg-4 from both the beginning and end of the container, while also introducing a row div since col-* classes should only be contained within a row.

Following this adjustment, simply add justify-content-center to the row for center alignment of the element. Additionally, include align-items-center along with a custom property of height: 100vh to ensure the row spans the full height of the viewport.

.full-scrn {
  height: 100vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />

<body>
  <div class="container">
    <div class="row justify-content-center align-items-center full-scrn">
      <div class="col-10 col-md-8 col-lg-4">
        <div class="jumbotron">

          <center>
            <h1>Hello!</h1>
          </center>
          <br>

          <form action="login.php" method="POST">
            <div class="form-group">
              <input type="text" class="form-control" name="username" placeholder="Enter username">

            </div>

            <div class="form-group">
              <input type="text" class="form-control" name="password" placeholder="Enter password">

            </div>

            <button type="submit" class="btn btn-primary form-control" name="login">Login</button>
            <center>
              <p>
                <?php echo $message; ?>
              </p>
            </center>
          </form>
        </div>
      </div>
    </div>
  </div>
</body>

Answer №2

If you're unsure whether you want the element to be positioned at the exact center of the screen (from left, right, top, bottom) or just the horizontal center, here's how you can achieve the latter:

To horizontally center the element, you can use the following CSS:

.parent-element{
            background: lightblue;
            width: 300px;
            height: 300
        }
        .centered-element{
          margin: auto;
          width: 200px;
          height: 200px;
          background: cyan;
          text-align: center;
          margin-top: 10px;
          margin-bottom: 10px;
        }
<div class="parent-element">
          <div class="centered-element">
              Sample Text
          </div>
        </div>

Answer №3

To ensure a column stays centered, grid offsets can be utilized:

<div class="container">
  <div class="col-xs-12 col-xs-offset-0 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
    <div class="jumbotron">
      <h1 class="text-center">Hello!</h1>

      <form action="login.php" method="POST">
        <div class="form-group">
          <input type="text" class="form-control" name="username" placeholder="Enter username">

        </div>

        <div class="form-group">
          <input type="text" class="form-control" name="password" placeholder="Enter password">

        </div>

        <button type="submit" class="btn
                    btn-primary form-control" name="login">Login</button>
          <p class="text-center">
            <?php echo $message; ?>
          </p>
      </form>
    </div>
  </div>
</div>

Demo

Key points to note:

  • Utilize text-center instead of extra markup for centering content
  • Avoid line breaks for spacing and utilize CSS for styling (
    .jumbotron h1 {margin-bottom: 20px;}
    )

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

What is the process for creating instant notifications using AJAX, PHP, and MySQL?

I am looking to implement real-time notifications on my website and have already set up the notification bar: <div class="alert alert-info alert-with-icon" data-notify="container"> <button type="button" aria-hidden="true" class="close"> ...

Navigate through the items in my subnavbar by scrolling the content

HTML Code <div id="subnavigation"> <?php $verbindung = mysql_connect("host", "user" , "pw") or die("Verbindung zur Datenbank konnte nicht hergestellt werden"); mysql_select_db("db") or die ("Datenbank konnte nicht ausgewählt w ...

AJAX code fetching dynamic content without relying on file loading

When I load my program, the dynamic code does not appear on the page until I reload it again. I attempted to use the onload event in the body to load content from an XML file using AJAX, but it only works after closing and reopening the program, not dynam ...

Updating the text box's font color to its original black shade

I have a form that includes dynamic text boxes fetching data from the backend. When a user clicks on a text box, the color of the text changes. Upon form submission, a confirmation message is displayed and I want the text box color to revert back to black. ...

Error in Cordova Android Compilation ("unable to locate Build Tools version 24.0.1")

I'm currently experiencing some difficulties while trying to compile my Cordova project on Android. Upon entering the command "cordova build Android", I encountered the following error message: FAILURE: Build failed with an exception. * What caused ...

Conceal or style the filter panel in a DT datatable

Here is an example to consider: library(DT) L <- 10 datatable( data.frame( var1 = sapply(1:L, function(x) paste("<X>",paste0(x, letters, LETTERS, "\n", ...

Is it a mistake in the Jquery logic or a syntax error?

Is there a logic or syntax error causing one of the options when clicking the radio to not display the corresponding tables with the same number of option dates? <style> #ch2 ,#ch3 ,#ch4 ,#ch5 ,#ch6 ,#ch7 ,#ch8 ,#ch9 ,#ch10 ,#c ...

What is the method for creating a beveled text effect using CSS?

Is there a CSS-only method to achieve a beveled text effect? https://i.sstatic.net/s4Wlw.png Just to clarify, I'm seeking a way to make text appear thick with a beveled style rather than using something like text-shadow which simply extends the font ...

PHP: Link to logo in different folder by including 'nav.php'

I am facing an issue with my nav.php file: <div> <!-- there's a lot of code here so I want to write it once and include it in all pages within the main folder as well as subfolders <img src="logo.png"> </div> The structur ...

Generate random unique values from an array to populate a text input field in HTML. Display a message once all unique values have been shown

I have included an option code and text box below: <select id="sel" class="form-control input-lg" data-live-search="true"> <option value="">-- Choose Your Country--</option> </select><br/><br/><br/> &l ...

Executing a PHP script to initiate a ping transmission

I have a project to complete for my university involving the development of a simple application. However, I lack experience in this area and am unsure how to proceed. The objective is straightforward: I want to send ping requests to 50 IP addresses at t ...

Create an HTML button with dual functionality

I currently have a button in my HTML that triggers the opening of the sidebar on mobile devices. <a href="#header" class="button scrolly">Contact Me</a> There is also a label element that, when clicked, selects the name field in the contact f ...

Misplacement of HTML head tags

Is it a grave error in this layout that H1 isn't the first rendered head element? Is there any way to correct this issue? Given that both columns have variable lengths, I am struggling to find a workaround. Reference I was requested to provide a ref ...

How can I make angular material data table cells expand to the full width of content that is set to nowrap?

This example demonstrates how the mat-cells are styled with a specific width: .mat-cell { white-space: nowrap; min-width: 150rem; } If the width is not specified, the table will cut off the text due to the white-space property being set to nowrap. Is ...

How to use Javascript to pause an HTML5 video when closed

I am new to coding and currently working on a project in Adobe Edge Animate. I have managed to create a return button that allows users to close out of a video and go back to the main menu. However, I am facing an issue where the video audio continues to p ...

Inconsistent alignment and formatting of fields in Bootstrap jQuery form

I need assistance with creating a form that includes three input fields: first name, last name, and email. Additionally, I would like to provide users with the option to add more groups of input fields. Currently, the three fields and the button are displ ...

Looping through color transitions upon hover using CSS

I am trying to create a color transition effect on hover, where the background changes from yellow to red and then back to yellow in a loop. I'm having trouble figuring out how to make this transition repeat continuously. Do I need to incorporate Java ...

Customizing the appearance of jQuery accordion components

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js" language="javascript"></script> <script type="text/javascript" src="http://www.compactcourse.com/js/accordionNew.js" language="javascript"></script> < ...

Applying CSS blur filter to container without affecting its content

I'm trying to create a hover effect where an image within a parent div transitions into a blurred state. However, I've run into an issue where if the image is set to 100% width/height of the parent div, there is a visible bezel of the parent&apos ...

The 'data-intro' property cannot be bound to the button element as it is not recognized as a valid property

I've been using the intro.js library in Angular 8 and so far everything has been working smoothly. However, I've hit a roadblock on this particular step. I'm struggling to bind a value in the data-intro attribute of this button tag. The text ...