Centering a div vertically [duplicate] - html

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed last year.
I am having trouble vertically centering forms on this web page. I want the forms to appear at the centre of my page, and have managed to centre them horizontally but am somehow failing to centre the forms vertically as well.
.alignment_container {
/* Center child horizontally*/
display: flex;
justify-content: center;
align-items: center;
}
<div class="alignment_container">
<form action="/chat-page" class="custom-centered" method="POST">
<!-- styling the form -->
<div class="d-inline-flex p-2">
<div class="input-group flex-nowrap">
<span class="input-group-text" id="addon-wrapping">#</span>
<input type="text" class="form-control" placeholder=" Enter a Username" name="user_name" aria-label="Username" aria-describedby="addon-wrapping" required>
</div>
</div>
<!-- end of styling the form -->
<button type="submit" class="btn btn-primary">Login!</button>
</form>
</div>
Where is my mistake?

Just add height:92vh to account for the margin on body or set to 100vh and add body{overflow:hidden;} as illustrated below:
.alignment_container {
/* Center child horizontally*/
display: flex;
justify-content: center;
align-items: center;
height:92vh;
}
<div class="alignment_container">
<form action="/chat-page" class="custom-centered" method="POST">
<!-- styling the form -->
<div class="d-inline-flex p-2">
<div class="input-group flex-nowrap">
<span class="input-group-text" id="addon-wrapping">#</span>
<input type="text" class="form-control" placeholder=" Enter a Username" name="user_name" aria-label="Username" aria-describedby="addon-wrapping" required>
</div>
</div>
<!-- end of styling the form -->
<button type="submit" class="btn btn-primary">Login!</button>
</form>
</div>

Related

How do I Align Form to center in BootStrap [duplicate]

This question already has answers here:
Vertical Align Center in Bootstrap 4 [duplicate]
(20 answers)
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 1 year ago.
I've question how to make form align to center with bootstrap.
Here's my code:
<body>
<div class="p-3 mb-2 bg-light text-dark container text-center">
<h2>Rezervacija Stanovanje Aljaz</h2>
<br>
<br>
<label><b>Ime: </b></label>
<input type="text" class="form-control col-sm-6" disabled value="" >
<label><b>Priimek: </b></label>
<input type="text" class="form-control col-sm-6" disabled value="" ><label><b>Telefon: </b></label>
<input type="text" class="form-control col-sm-6" disabled value="">
</div>
</body>
If you are making HTML email template as you said in one of your comment.. you should use table layout or you will have problems with that template in many clients.
I don't suggest using flex in emails templates too.. but to answer your question add these classes to your first div d-flex flex-column align-items-center.
Add CSS link of bootstrap4-5 after your title tag else write custom CSS code. text-algin: center;
You need to use the various Bootstrap 4 centering methods...
Use text-center for inline elements.
Use justify-content-center for flexbox elements (ie; form-inline)
https://codeply.com/go/Am5LvvjTxC
Also, to offset the column, the col-sm-* must be contained within a .row, and the .row must be in a container...
<section id="cover">
<div id="cover-caption">
<div id="container" class="container">
<div class="row">
<div class="col-sm-10 offset-sm-1 text-center">
<h1 class="display-3">Welcome to Bootstrap 4</h1>
<div class="info-form">
<form action="" class="form-inline justify-content-center">
<div class="form-group">
<label class="sr-only">Name</label>
<input type="text" class="form-control" placeholder="Jane Doe">
</div>
<div class="form-group">
<label class="sr-only">Email</label>
<input type="text" class="form-control" placeholder="jane.doe#example.com">
</div>
<button type="submit" class="btn btn-success ">okay, go!</button>
</form>
</div>
<br>
↓
</div>
</div>
</div>
</div>
</section>

Using Bootstrap 4: How do I take the Navbar into account when centering a login form in the center of the page?

I found plenty of help for centering a login form but once centered it's slightly offset by what I believe must be the navbar. I can't figure out why this is happening as the centering relies on the body height set at 100% in my css. How do I center the login form in the center of the document without it being offset by the navbar? (it's lower than the exact center of the page) What I mean is to the eye the form isn't in the center of the screen.
CSS
html, body {
background-color: lightgray;
height: 100%;
PHP/HTML
<div class="container h-100">
<div class="row h-100 justify-content-center align-items-center">
<div class="col-10 col-md-8 col-lg-6">
<form action="{{ path('login') }}" method="post" >
<div class="form-group">
<label for="_username">Username:</label>
<input type="text" class="form-control" placeholder="Username..." required name="_username">
</div>
<div class="form-group">
<label for="_password">Password:</label>
<input type="password" class="form-control" placeholder="Password..." required name="_password">
</div>
<div class="form-group">
<button class="btn btn-success">Login</button>
</div>
</form>
</div>

Complex Form elements alignment

I'm using primeng css and I want to button and inputs to be aligned (end) horizontally I don't want to be aligned labels with button
jsfiddle
<div class="ui-g ui-fluid dashboard">
<div class="ui-g-12">
<div class="ui-g-12 ui-md-2">
<label for="year"> Year</label>
<input type="text" name="year" >
</div>
<div class="ui-g-12 ui-md-2">
<label for="month"> Month</label>
<input type="text" name="month" >
</div>
<div class="ui-g-12 ui-md-1">
<input type="button" value="Search" id="myButton">
</div>
</div>
</div>
use the align-self-end class (bootstrap 4 / flexbox) to vertically align the button to the bottom of the column.
<div class="form-group col-md-2 align-self-end"><button class="btn btn-primary" id="btn" name="btn" type="submit">Submit</button></div>
Also visit Here for further query
You can also use align-self: flex-end;
Use it on your Compiler not on Fiddle

Bootstrap center div vertically [duplicate]

This question already has answers here:
Bootstrap get div to align in the center
(2 answers)
Closed 5 years ago.
I would like to center horizontally the login form, it is centered on mobile devices, but I can't figure out how to achieve the same on a monitor screen. I am using Bootstrap 3.
<div class="container-fluid main-container">
<h2>Login</h2>
<form action="/action_page.php">
<div class="form-group row">
<div class="col-sm-3">
<label for="email">Username:</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email" name="email">
</div>
</div>
<div class="form-group row">
<div class="col-sm-3">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div>
</div>
<div class="checkbox">
<label><input type="checkbox" name="remember"> Remember me</label>
</div>
<div class="form-group row">
<div class="col-sm-3">
<button type="submit" class="btn btn-default btn-block">Submit</button>
</div>
</div>
</form>
</div>
Set the container to display flex and use justify-content and align-items like this
.container-fluid {
display: flex;
justify-content: center;
align-items: center;
}
this is a css solution I don't think you can achieve this with bootstrap

Using text-align center to center the email form :

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>