specify height of flexbox other than 100vh - html

I have a form inside a div inside. The div is inside another div.
<div class="div__signup"> <!-- takes the complete width/height of content section -->
<div class="content_div--white"> <!-- contains the form. Adjust this div to create a border around the form -->
<form class="signup-form" actions="">
<label for="firstname" class="first-name">First Name</label>
<input id="firstname" type="text">
<label for="lastname" class="last-name">Last Name</label>
<input id="lastname" type="text">
<label for="email" class="email"> Email</label>
<input id="email" type="email">
<label for="password" class="password">Password</label>
<input id="password" type="password">
<label for="verify-password" class="verify-password">Verify Password</label>
<input id="verify-password" type="password">
</form>
</div>
</div>
This html is inside a css-grid which has 3 rows - navigation, content and footer.
.css-grid-container{
height:100vh; /*???*/
display: grid;
grid-gap:20px;
grid-template-columns: 1fr; /* 1 columns*/
grid-template-rows: auto 15fr 1fr; /* 3 rows. Auto takes height of navigation, remaining is divided into 2 rows, middle row (content) is 15 times larger than the 3rd row (footer).*/
}
I want the html of the form page to be at the center of the content section of the css-grid. If I use flexbox, I suppose I have to provide specific height for the flexbox. The only height parameter I know is 100vh which equals height of the viewport.
.div__signup{
display:flex;
align-items: center;
justify-content: center;
height: 100vh; /*need something else than 100vh as I dont want alignment w.r.t viewport */
}
But I want the form to be at the center of the content section of the grid, not the center of the viewport. How could I align the form to the center of the content section?

Based on the fact that .div__signup is a child of a grid item, here are two suggestions:
Using absolute position
.css-grid-item {
position: relative;
}
.div__signup {
position: absolute;
left: 0; top: 0;
right: 0; bottom: 0;
display: flex;
align-items: center;
justify-content: center;
}
Make the grid item a flex container
.css-grid-item {
display: flex;
align-items: center;
justify-content: center;
}
Do note, sample 2 may, or may not work, based on how the justify-content/align-items is set on the css-grid-item.
Assumed HTML
<div class="css-grid-item">
<div class="div__signup"> <!-- takes the complete width/height of content section -->
<div class="content_div--white"> <!-- contains the form. Adjust this div to create a border around the form -->
<form class="signup-form" actions="">
<label for="firstname" class="first-name">First Name</label>
<input id="firstname" type="text">
<label for="lastname" class="last-name">Last Name</label>
<input id="lastname" type="text">
<label for="email" class="email"> Email</label>
<input id="email" type="email">
<label for="password" class="password">Password</label>
<input id="password" type="password">
<label for="verify-password" class="verify-password">Verify Password</label>
<input id="verify-password" type="password">
</form>
</div>
</div>
</div>

See this I use display fixed, table and table cell
.seline-preloader {
position: fixed;
left: 0px;
top: 0;
width: 100%;
height: 100%;
z-index: 9999;
background: #ffffff;
display: block;
box-sizing: border-box;
}
.table {
display: table;
width: 100%;
height: 100%;
background-color: transparent !important;
}
.table {
width: 100%;
max-width: 100%;
margin-bottom: 20px;
}
.table-cell {
display: table-cell;
width: 100%;
height: 100%;
vertical-align: middle;
}
<section class="seline-preloader">
<div class="table">
<div class="table-cell">
<h2 class="hide">Form</h2>
<!--start of your code-->
<div class="div__signup">
<!-- takes the complete width/height of content section -->
<div class="content_div--white">
<!-- contains the form. Adjust this div to create a border around the form -->
<form class="signup-form" actions="">
<label for="firstname" class="first-name">First Name</label>
<input id="firstname" type="text">
<label for="lastname" class="last-name">Last Name</label>
<input id="lastname" type="text">
<label for="email" class="email"> Email</label>
<input id="email" type="email">
<label for="password" class="password">Password</label>
<input id="password" type="password">
<label for="verify-password" class="verify-password">Verify Password</label>
<input id="verify-password" type="password">
</form>
</div>
</div>
<!--end of your code-->
</div>
</div>
</section>
jsfiddle

Related

How can I center a div which is direct child to body? [duplicate]

This question already has answers here:
Vertically centering a div in body?
(7 answers)
Center divs in body horizontally and vertically with flexbox
(3 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed last month.
I'm trying to center this div both main axis and cross axis but justify-content works but align item doesn't. Can't figure out why it is happening.
* {
margin: 0;
padding: 0;
}
body {
font-size: 10px;
background-color: rgb(255, 255, 255);
display: flex;
flex-direction: column;
align-items: center;
}
<div class="formdiv">
<form class="form">
<br>
<label for="email">Username</label>
<br>
<input type="email" id="email" placeholder="Username" required>
<br>
<label for="password">Password</label>
<br>
<input type="text" id="password" placeholder="Password" required>
<br>
<button>submit</button>
</form>
</div>
The div and the form are centered, but the body is only as tall as they are. You'd have to give it a larger height.
body {
min-height: 100vh;
}
body {
font-size: 10px;
background-color: rgb(255, 255, 255);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
<div class="formdiv">
<form class="form">
<br>
<label for="email">Username</label>
<br>
<input type="email" id="email" placeholder="Username" required>
<br>
<label for="password">Password</label>
<br>
<input type="text" id="password" placeholder="Password" required>
<br>
<button>submit</button>
</form>
</div>
By default, the body will take the height of the contents. Try giving height to the body.
* {
margin: 0;
padding: 0;
}
body {
height: 100vh;
font-size: 10px;
background-color: rgb(255, 255, 255);
display: grid;
place-items: center;
}
<div class="formdiv">
<form class="form">
<br>
<label for="email">Username</label>
<br>
<input type="email" id="email" placeholder="Username" required>
<br>
<label for="password">Password</label>
<br>
<input type="text" id="password" placeholder="Password" required>
<br>
<button>submit</button>
</form>
</div>
The form doesn't appear centered because the body element itself only occupies the height of the content. If you set the min-height of the body to 100vh then justify content works as intended. See below
* {
margin: 0;
padding: 0;
}
body {
font-size: 10px;
background-color: rgb(255, 255, 255);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
/* added this */
min-height: 100vh;
}
<div class="formdiv">
<form class="form">
<br>
<label for="email">Username</label>
<br>
<input type="email" id="email" placeholder="Username" required>
<br>
<label for="password">Password</label>
<br>
<input type="text" id="password" placeholder="Password" required>
<br>
<button>submit</button>
</form>
</div>
Also ... I use grid. Even less CSS!
* {
margin: 0;
padding: 0;
}
body {
font-size: 10px;
background-color: rgb(255, 255, 255);
display: grid;
place-items: center;
/* added this */
min-height: 100vh;
}
<div class="formdiv">
<form class="form">
<br>
<label for="email">Username</label>
<br>
<input type="email" id="email" placeholder="Username" required>
<br>
<label for="password">Password</label>
<br>
<input type="text" id="password" placeholder="Password" required>
<br>
<button>submit</button>
</form>
</div>

Flexbox form width does not change

I am trying to get a form's inputs to fill the container width but since I am using two flexboxes, it doesn't seem to allow me to set the width of the input boxes. Is there a way around this? I've tried using flex-shrink but got the same result.
What is the best way to set a form's width to fill its container using flexbox?
.sd-form-group {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: flex-start;
}
.sd-form-wrap {
display: flex;
}
.sd-form-group input[type=text],
.sd-form-group textarea {
width: 100%;
}
<form>
<div class="sd-form-group">
<div class="sd-form-wrap">
<div class="form-name form-first-name">
<label>First Name</label>
<br>
<input type="text">
</div>
<div class="form-name form-last-name">
<label>Last Name</label>
<br>
<input type="text">
</div>
</div>
<div class="form-email">
<label>Email</label>
<br>
<input type="text">
</div>
<div class="form-message">
<label>Message</label>
<br>
<textarea placeholder="Enter your comments, questions and feedback here." rows="4"></textarea>
</div>
<div class="form-checkbox">
<input type="checkbox">
<label>
Communications box
</label>
</div>
<input type="submit" value="Submit">
</div>
</form>
On the container, instead of flex-flow: column nowrap use row wrap.
On the items, force each one to occupy the full width of the row.
.sd-form-group {
display: flex;
/* flex-flow: column nowrap; */
flex-wrap: wrap; /* new */
justify-content: center;
/* align-items: flex-start; */ /* does nothing */
}
.sd-form-group > div {
flex: 0 0 100%; /* new */
}
.sd-form-wrap {
display: flex;
}
.sd-form-group input[type=text],
.sd-form-group textarea {
width: 100%;
}
<form>
<div class="sd-form-group">
<div class="sd-form-wrap">
<div class="form-name form-first-name">
<label>First Name</label>
<br>
<input type="text">
</div>
<div class="form-name form-last-name">
<label>Last Name</label>
<br>
<input type="text">
</div>
</div>
<div class="form-email">
<label>Email</label>
<br>
<input type="text">
</div>
<div class="form-message">
<label>Message</label>
<br>
<textarea placeholder="Enter your comments, questions and feedback here." rows="4"></textarea>
</div>
<div class="form-checkbox">
<input type="checkbox">
<label>
Communications box
</label>
</div>
<input type="submit" value="Submit">
</div>
</form>

Label sticking next to another label

I am having a simple registration form, but one of the label fields sticks next to another label field.
Currently it looks like this:
Email should be under the Username, not next to it. Other form elements align nicely, but not these two.
label {
float: left;
}
input {
float: right;
}
<div class="form-wrapper">
<div>
<div>
<label for="user-name">Username:</label>
<input type="text" id="user-name" name="user-name" required>
</div>
<div>
<label for="user-email">Email:</label>
<input type="email" id="user-email" name="user-email" required>
</div>
</div>
</div>
Why don't you just use flex, clean and less code.
.form-wrapper {
width: 400px;
border: 1px solid blue;
}
.username,
.useremail {
display: flex;
margin: 10px;
width: 350px;
justify-content: space-between;
font-weight: bold;
}
<div class="form-wrapper">
<div>
<div class="username">
<label for="user-name">Username:</label>
<input type="text" id="user-name" name="user-name" required>
</div>
<div class="useremail">
<label for="user-email">Email:</label>
<input type="email" id="user-email" name="user-email" required>
</div>
</div>
</div>
If you are going with float you have to know about using clear property for it's next elements. So a best way to handle is, to create a after pseudo-element on the parent and clear:both.
In the below code have added 'field' class for each container and styled it with :after.
.field::after{
content: '';
display: block;
height: 0;
clear: both;
visibility: hidden;
}
label {
float: left;
}
input {
float: right;
}
<div class="form-wrapper">
<div>
<div class="field">
<label for="user-name">Username:</label>
<input type="text" id="user-name" name="user-name" required>
</div>
<div class="field">
<label for="user-email">Email:</label>
<input type="email" id="user-email" name="user-email" required>
</div>
</div>
</div>
Labels and input fields are stacked from the left and the right, resp., due to the css float properties. Note that the label/input pairs render on individual lines when removing the css, though without proper vertical alignment.
The CSS display: table property and friends can be employed to rectify this. Basically they cause the renderer to apply table layouting to elements other than tableand descendants.
.d-t {
display: table;
}
.d-tr {
display: table-row;
}
.d-tr > label, .d-tr > input {
display: table-cell;
}
<div class="form-wrapper">
<div class="d-t">
<div class="d-tr">
<label for="user-name">Username:</label>
<input type="text" id="user-name" name="user-name" required>
</div>
<div class="d-tr">
<label for="user-email">Email:</label>
<input type="email" id="user-email" name="user-email" required>
</div>
</div>
</div>

Can't figure out how to align horizontally text to left of input

this is a very beginner question, but I have been trying to figure out a way to have the Name, Email, and Age aligned to the left of the input fields. And then have it centered on the input boxes.
Here is a link to my codepen.io
I have tried floating the text to the left and making the inputs display as blocks. The input seems to be stuck having an extremely wide width which is making the text not display properly. If I manage to shrink the width of the box, and want to adjust the padding it makes the input box bigger! Instead of moving the the text and input left or right.
#text-label {
width: 50%;
}
#text-input {
width: 40%;
display: block;
margin: auto;
}
#email-input {
width: 80%;
display: block;
margin: auto;
}
#number-input {
width: 80%;
display: block;
margin: auto;
}
<div id="name-email-age">
<div id="name" class="center">
<label id="text-label">
* Name:
</label>
<input id="text-input" type="text" name="Enter your name" placeholder="Enter your name" required />
<div id="email" class="center">
<label id="email-label">
* Email:
</label>
<input id="email-input" type="email" name="Enter your email" placeholder="Enter your Email" required />
</div>
<div id="age" class="center">
<label id="age-label">
* Age:
</label>
<input id="number-input" type="number" name="Enter your age" placeholder="Enter your Age" required max="122" min="5" />
</div>
</div>
Change the input display styles to inline-block.
https://codepen.io/AustinGordon/pen/yEodLo
Maybe your can try this:
Wrap labels and controls in .control-group for optimum spacing
and change box to ideal size =).
#text-label {
width: 50%;
}
#text-input {
width: 50%;
display: block;
}
#email-input {
width: 100%;
display: block;
}
#number-input {
width: 100%;
display: block;
}
.control-group {
width: 80%;
margin: auto;
text-align: left;
}
<div id="name-email-age">
<div id="name" class="center">
<div class="control-group">
<label id="text-label">* Name:</label>
<input id="text-input" type="text" name="Enter your name" placeholder="Enter your name" required />
</div>
</div>
<div id="email" class="center">
<div class="control-group">
<label id="email-label">* Email:</label>
<input id="email-input" type="email" name="Enter your email" placeholder="Enter your Email" required />
</div>
</div>
<div id="age" class="center">
<div class="control-group">
<label id="age-label">* Age:</label>
<input id="number-input" type="number" name="Enter your age" placeholder="Enter your Age" max="122" min="5" required />
</div>
</div>
</div>

Center input fields in form

I have a login form that is on the center of the page. The FORM is already in the right place with the right size. However, the two inputs are aligned to the left and I want them to be centered. The following code does not work. Any ideas?
HTML
<div class="col-sm-4 col-sm-offset-4">
<form action="/new" method="POST">
<input class="loginField" type="text" name="email" placeholder="Email address"></input>
<input class="loginField" type="text" name="password" placeholder="Password"></input>
</form>
</div>
CSS
.loginField {
margin: 0 auto;
width: 500px;
height: 50px;
}
You should use the form-control class to alter the default behavior of Bootstrap inputs.
*I altered your HTML so it's mobile first, this wont effect the text being centered if it's unnecessary for your needs tho.
body {
padding-top: 50px;
}
#loginForm {
max-width: 500px;
margin: 0 auto;
}
#loginForm .form-control {
position: relative;
height: 50px;
font-size: 16px;
text-align: center;
border-radius: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<form id="loginForm">
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
</form>
</div>
<!-- /container -->
<hr>
Add a class="text-center" to your form
<form class="text-center" action="/new" method="POST">
jsBin demo
Logically, if you don't want all your form's text to be centered, wrap your inputs inside a i.e: <div> and add the class to that DIV instead.