I'm developing a Spring Boot Web App and am having an issue on the login page, which you can see in the attached picture, where the div is not EXACTLY centered on the page. I'm also using Bootstrap.
It appears that it is nearly centered, but is off a bit to the left for some reason. I've posted my HTML and relevant CSS below. Some parts of it must be conflicting, but I'm not sure where.
<div id="parentLogin">
<div class="row" style="width: 40%;">
<div class="col-md-12 col-md-offset-2">
<div>
<c:if test="${param.error != null}">
<div class="login-error" style="margin: 0 auto;">Incorrect username or password</div>
</c:if>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title" style="font-weight: bold; font-size: x-large; margin: 1%;">User Login</div>
</div>
<div class="panel-body">
<form method="post" action="${loginUrl}" class="login-form">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="input-group">
<input type="text" name="username" placeholder="Username"
class="form-control" />
</div>
<div class="input-group">
<input type="password" name="password" placeholder="Password"
class="form-control" />
</div>
<button type="submit" class="suit_and_tie">Sign In</button>
</form>
</div>
</div>
</div>
</div>
</div>
#parentLogin {
display: flex;
justify-content: center;
align-items: center;
height: 75vh;
}
Your div with id="parentLogin" as parent is centered... with a fixed width of 40% of the available width. But it does not appear that your bootstrap panels inside of that are centered within that div... you should add classes to the entire chain of child objects... each class should use display: flex; and should specify flex-direction, align-items, and justify-content. You can use Bootstrap classes for those... but each element in the parent-child chain should specify the alignment of its contents.
#parentLogin {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 75vh;
width: 100%;
}
.parentRow {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
height: 75vh;
}
.parentCol {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 75vh;
}
<div id="parentLogin">
<div class="row parentRow" style="width: 40%;">
<div class="col-md-12 col-md-offset-2 parentCol">
<div>
<c:if test="${param.error != null}">
<div class="login-error" style="margin: 0 auto;">
Incorrect username or password
</div>
</c:if>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title" style="font-weight: bold; font-size: x-large; margin: 1%;">
User Login
</div>
</div>
<div class="panel-body">
<form method="post" action="${loginUrl}" class="login-form">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="input-group">
<input type="text" name="username" placeholder="Username" class="form-control" />
</div>
<div class="input-group">
<input type="password" name="password" placeholder="Password" class="form-control" />
</div>
<button type="submit" class="suit_and_tie">
Sign In
</button>
</form>
</div>
</div>
</div>
</div>
</div>
Related
This is reduction of a simple page I am working on. I am trying to align the image, User Name, Password and Sign in button with the E from Enter. No matter what I do they are sticking to the left of the page and not budging. Do I need to tie them to the Enter with a special align?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<style>
.text-box {
text-align: left;
width: 300px;
position: center;
}
.btn {
font-size: 14px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
<img src="" width="200" height="300"/>
<div class="login-header">
<h2 id="title" align="center">
Enter
Test
</h2>
<br>
</div>
<form onsubmit="return false;" method="post">
<div class="form-group text-box" align="center">
<label for="name">User Name*</label>
<input
type="email"
class="form-control"
id="email">
</div>
<div class="form-group text-box">
<label for="name">Password*</label>
<input
type="password"
class="form-control"
id="password"
placeholder="Enter your password">
</div>
<button
type="submit"
id="btn-login"
class="btn btn-primary btn-block button">
Sign In
</button>
<br>
<hr>
</form>
There's a lot I had to change, as your layout is honestly not very well written, it seems like you are learning to write HTML from a very old guide (I'm guessing W3). You shouldn't be using things like <br> or The sooner you can start seeing styling as blocks and not the alignment of text like in a word document, the better.
but what I did here was remove the formatting from your header, left aligned it, then gave both it and your form a fixed width and automatic margin:
.text-box {
text-align: left;
width: 300px;
position: center;
}
.btn {
font-size: 14px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#title{
margin: auto;
width:250px;
}
form{
margin: 0 auto;
width:250px;
}
<img src="" width="200" height="300"/>
<div class="login-header">
<h2 id="title">
Enter
</h2>
<br>
</div>
<form onsubmit="return false;" method="post">
<div class="form-group text-box" align="center">
<label for="name">User Name*</label>
<input
type="email"
class="form-control"
id="email">
</div>
<div class="form-group text-box">
<label for="name">Password*</label>
<input
type="password"
class="form-control"
id="password"
placeholder="Enter your password">
</div>
<button
type="submit"
id="btn-login"
class="btn btn-primary btn-block button">
Sign In
</button>
<br>
<hr>
</form>
However that isn't what I would recommend doing, you should put the header IN the form. Like so, then format the form the same as before.
.text-box {
text-align: left;
width: 300px;
position: center;
}
.btn {
font-size: 14px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
form{
margin: 0 auto;
width:250px;
}
<img src="" width="200" height="300"/>
<form onsubmit="return false;" method="post">
<h2 id="title">
Enter
</h2>
<div class="form-group text-box" align="center">
<label for="name">User Name*</label>
<input
type="email"
class="form-control"
id="email">
</div>
<div class="form-group text-box">
<label for="name">Password*</label>
<input
type="password"
class="form-control"
id="password"
placeholder="Enter your password">
</div>
<button
type="submit"
id="btn-login"
class="btn btn-primary btn-block button">
Sign In
</button>
<br>
<hr>
</form>
This question already has answers here:
How can I center an absolutely positioned element in a div?
(37 answers)
How can I horizontally center an element?
(133 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 1 year ago.
we would like to center an html component.
The situation now:
.card{
width: 400px;
border: 3px solid #54722b;
left: 0;
right: 0;
margin: auto;
margin-top: 20px;
}
.card-header{
background-color: rgb(221, 211, 123);
}
.card-header h3{
color: white;
}
<div class="card">
<div class="card-header">
<h3>Title</h3>
</div>
<div class="card-body">
<div class="form-group">
<form (ngSubmit)="register()">
<div class="input-group form-group">
<input name="name1" class="form-control">
</div>
<div class="input-group form-group">
<input name="name2" class="form-control">
</div>
<div class="input-group form-group">
<input name="name3" class="form-control">
</div>
<div class="input-group form-group">
<input name="name4" class="form-control">
</div>
<div class="input-group form-group">
<input name="name5" class="form-control">
</div>
<div class="input-group form-group">
<input name="name6" class="form-control">
</div>
</form>
</div>
</div>
</div>
We tried: (but it didn't work)
To add to the .card in the css file, the line:
position: fixed;
This line of code centered the component, but when we zoomed in, it was not possible to scroll the screen and see the entire component.
How to center the html component, so that after zooming in, it will be possible to scroll the screen?
You can try these codes.
See to big screen: https://codepen.io/en0ndev/pen/bGgqpPm
body {
height:5000px;
}
.card_main {
position:fixed;
left:0;
right:0;
top:50%;
transform:translateY(-50%);
}
.card{
width: 400px;
border: 3px solid #54722b;
left: 0;
right: 0;
margin: auto;
margin-top: 20px;
}
.card-header{
background-color: rgb(221, 211, 123);
}
.card-header h3{
color: white;
}
<body>
<div class="card_main">
<div class="card">
<div class="card-header">
<h3>Title</h3>
</div>
<div class="card-body">
<div class="form-group">
<form (ngSubmit)="register()">
<div class="input-group form-group">
<input name="name1" class="form-control">
</div>
<div class="input-group form-group">
<input name="name2" class="form-control">
</div>
<div class="input-group form-group">
<input name="name3" class="form-control">
</div>
<div class="input-group form-group">
<input name="name4" class="form-control">
</div>
<div class="input-group form-group">
<input name="name5" class="form-control">
</div>
<div class="input-group form-group">
<input name="name6" class="form-control">
</div>
</form>
</div>
</div>
</div>
</div>
</body>
.card{
width: 400px;
border: 3px solid #54722b;
left: 0;
right: 0;
margin: auto;
margin-top: 20px;
display: flex;
justify-content: center;
}
.card-header{
background-color: rgb(221, 211, 123);
}
.card-header h3{
color: white;
}
<div class="card">
<div class="card-header">
<h3>Title</h3>
</div>
<div class="card-body">
<div class="form-group">
<form (ngSubmit)="register()">
<div class="input-group form-group">
<input name="name1" class="form-control">
</div>
<div class="input-group form-group">
<input name="name2" class="form-control">
</div>
<div class="input-group form-group">
<input name="name3" class="form-control">
</div>
<div class="input-group form-group">
<input name="name4" class="form-control">
</div>
<div class="input-group form-group">
<input name="name5" class="form-control">
</div>
<div class="input-group form-group">
<input name="name6" class="form-control">
</div>
</form>
</div>
</div>
</div>
Is this what you are trying to achieve?
body {
display:grid;
justify-content:center;
}
.card{
width: 400px;
border: 3px solid #54722b;
margin-top: 20px;
}
.card-header{
background-color: rgb(221, 211, 123);
}
.card-header h3{
color: white;
}
<div class="card">
<div class="card-header">
<h3>Title</h3>
</div>
<div class="card-body">
<div class="form-group">
<form (ngSubmit)="register()">
<div class="input-group form-group">
<input name="name1" class="form-control">
</div>
<div class="input-group form-group">
<input name="name2" class="form-control">
</div>
<div class="input-group form-group">
<input name="name3" class="form-control">
</div>
<div class="input-group form-group">
<input name="name4" class="form-control">
</div>
<div class="input-group form-group">
<input name="name5" class="form-control">
</div>
<div class="input-group form-group">
<input name="name6" class="form-control">
</div>
</form>
</div>
</div>
</div>
The issue might be on the parent container of .card. You have a margin of auto, which should center the div inside the parent div. Check the margins with inspector to see which container is causing the issue.
Use FlexBox styling:
display: flex property.
Once the container is flex container you can align the content centrally using
align-content: center // along the cross axis, other possible values flex-start, flex-end
justify-content: center //along the main axis
.card {
display: flex;
align-items: center;
justify-content: center;
}
Add margin:0 auto to css for .card
Or use center tags
i'm having troubles centering a card (or container). I'm able to center it horizontally but not vertically. I alredy tested some code found on stack and other webpages but it does not seems to work.
My current code is:
<div class="container col-md-3 py-5">
<div class="card">
<div class="card-header">
<h3 class="mb-0">Login</h3>
</div>
<div class="card-body">
<form class="form">
<div class="form-group">
<label for="username">Username:</label> <input class="form-control" type="text" name="username" id="username" autofocus required >
</div>
<div class="form-group">
<label for="password">Password: </label> <input class="form-control" type="password" name="user" id="password" required>
</div>
</form>
</div>
</div>
</div>
[TESTED - working] Use flexbox.
https://www.bootply.com/AaDqw82aFL#
Wrap your code with additional div with class containing following two style rules.
display: flex;
align-items: center;
justify-content: center;
align-conten:center;
min-height: 100%; /* Fallback for browsers do NOT support vh unit */
min-height: 100vh;
It should work fine.
Full code with inline style (I recommend you to use class instead of style attribute):
<div style=" display: flex; align-items: center; justify-content: center; align-conten:center; min-height: 100%; /* Fallback for browsers do NOT support vh unit */min-height: 100vh;">
<div class="container col-md-3 py-5" style="height: 200px; width:200px">
<div class="card">
<div class="card-header">
<h3 class="mb-0">Login</h3>
</div>
<div class="card-body">
<form class="form">
<div class="form-group">
<label for="username">Username:</label> <input class="form-control" type="text" name="username" id="username" autofocus="" required="">
</div>
<div class="form-group">
<label for="password">Password: </label> <input class="form-control" type="password" name="user" id="password" required="">
</div>
</form>
</div>
</div>
</div>
</div>
I believe below code should work for you, just try and check
.container.py-5{
position: absolute; //or relative
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
}
Add d-flex align-items-center bootstrap4 classes on your body and put its height on 100vh;
body{
height:100vh;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<body class="d-flex align-items-center">
<div class="container col-md-3 py-5">
<div class="card">
<div class="card-header">
<h3 class="mb-0">Login</h3>
</div>
<div class="card-body">
<form class="form">
<div class="form-group">
<label for="username">Username:</label> <input class="form-control" type="text" name="username" id="username" autofocus required >
</div>
<div class="form-group">
<label for="password">Password: </label> <input class="form-control" type="password" name="user" id="password" required>
</div>
</form>
</div>
</div>
</div>
</body>
I am trying to make an app landing Page using bootstrap framework (Bootstrap 4 alpha). While trying to align everything in center I used text-align:center in jumbotron id. Everything except email form aligned.
Is there any way to align email form in center as well also making sure that it remains in center irrespective of web page width.
This is the code in stylesheet.
.jumbotron
{
background-image: url(background.jpg);
text-align: center;
margin-top: 50px;
}
This is the main code.
<div class="jumbotron" id="jumbotron">
<h1 class="display-3">My Awesome App!</h1>
<p class="lead">This is why YOU should download this fantastic app!</p>
<hr class="m-y-2">
<p>Want to know more? Join our mailing list!</p>
<form class="form-inline" id = "emailbar">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<div class="input-group">
<div class="input-group-addon">#</div>
<input type="email" class="form-control" id="email" placeholder="Your email">
</div>
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
</div>
Since your form has display: flex it spans the whole width of its parent.
Also the text-align: center in your jumbotron class won't apply to the forms element as they are flex items.
So change either of these in your form-inline class
change it to display: inline-flex (and the text-align: center will apply)
add justify-content: center; (the Flexbox property to align horizontally in row direction)
To target only the emailbar, do like this
#emailbar {
display: inline-flex;
}
or this
#emailbar {
justify-content: center;
}
Just add css part in jumbotron to get the center.
.jumbotron {
background-image: url(background.jpg);
text-align: center;
margin-top: 50px;
display: flex;/*Add this*/
flex-direction: column; /*Add this*/
align-items: center; /*Add this*/
}
Working fiddle
.jumbotron {
display: flex;/*Add this*/
flex-direction: column; /*Add this*/
align-items: center; /*Add this*/
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<div class="jumbotron" id="jumbotron">
<h1 class="display-3">My Awesome App!</h1>
<p class="lead">This is why YOU should download this fantastic app!</p>
<hr class="m-y-2">
<p>Want to know more? Join our mailing list!</p>
<form class="form-inline" id="emailbar">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<div class="input-group">
<div class="input-group-addon">#</div>
<input type="email" class="form-control" id="email" placeholder="Your email">
</div>
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
</div>
There's no reason for extra CSS. Just use justify-content-center on the form...
https://www.codeply.com/go/7LjBkEtvVV
<form class="form-inline justify-content-center" id = "emailbar">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<div class="input-group">
<div class="input-group-addon">#</div>
<input type="email" class="form-control" id="email" placeholder="Your email">
</div>
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
I am making a form inside of jumbotron and for some reason I get it to look good on desktop view but when viewing it mobile it goes out of the jumbotron. I am using netbeans to do my coding. I even use #media (max-width: 620px). I tried using google but failed :(
#wrapper{
width: 500px;
height: 700px;
position: relative;
margin: 0 auto;
background-color: rgba(255,255,255,.75);
text-align: center;
}
form{
color: black;
text-align: center;
}
div{
clear: both;
}
#off{
background-color: rgba(225,0,0,.85);
color: white;
width: 525px;
height: 140px;
font-size: 1rem;
text-align: center;
}
.formHeader{
color: #104c8b;
}
.formFooter{
background-color: rgba(0,0,255,.5);
color: white;
margin: 65px 0 0 0;
}
#media(max-width: 620px) {
#wrapper{
width: 100px;
height: 100px;
position: relative;
margin: 0 auto;
background-color: rgba(255,255,255,.75);
text-align: center;
}
<section id="top" class="jumbotron">
<div class="container-fluid">
<div class="row text-center">
<div id="wrapper"><p>Contact Form</p>
<div class="formHeader">
</div>
<form action="test.html" method="get" name="test" id="myForm">
<label>Name</label><input name="name" type="text" /><br>
<label>Email</label><input name="email" type="text" /><br>
<label>Phone</label><input name="number" type="text" /><br>
<input type="submit" value="Send"/>
</form>
<div class="formFooter">
</div>
</div>
</div>
</div>
</section>
Given that you're using Bootstrap as a framework it seems a shame not to make use of all of well... the framework. With some restructuring of your HTML (to make use of Bootstrap's responsive classes and form controls) you can easily achieve the responsiveness you desire.
Most of it is just adding the necessary .col-*-* for grid layouts, and of course applying Bootstrap classes to your input and label elements, etc. An example Bootply is below though the code is significantly altered to better-reflect Bootstrap's Framework and modern input classifications (like tel or email, etc).
http://www.bootply.com/mPNCZyXbbD
The HTML:
<section id="top" class="jumbotron">
<div class="container-fluid">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-wrapper text-center">
<p>Contact Form</p>
<form action="test.html" method="get" name="test" id="myForm" class="form form-horizontal">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input name="name" type="text" class="form-control">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input name="email" type="email" class="form-control">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input name="number" type="tel" class="form-control">
</div>
</div>
<button type="submit" class="btn btn-default">Send</button>
</form>
</div>
</div>
</div>
</div>
</section>
And the CSS:
.form-wrapper {
padding: 15px;
background: rgba(255,255,255,0.75);
}