Align inputs and textarea with labels vertically even - html

I want to create this form in css
HTML
<div class="contact__right">
<form action="" class="contact__form">
<div>
<label for="name" class="contact__form-label">Name</label>
<input type="text" id="name" class="contact__form-input">
</div>
<div>
<label for="email" class="contact__form-label">Email</label>
<input type="text" id="email" class="contact__form-input">
</div>
<div>
<label for="phone" class="contact__form-label">Phone</label>
<input type="text" id="phone" class="contact__form-input">
</div>
<div>
<label for="message" class="contact__form-label">Message</label>
<textarea name="message" id="message" cols="30" rows="10" class="contact__form-textarea"></textarea>
</div>
</form>
</div>
CSS
.contact {
&__form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 105px 95px;
color: $white;
}
&__form-input,
&__form-textarea {
width: 300px;
height: 40px;
background: transparent;
border: 1px solid white;
border-radius: 2px;
margin-bottom: 25px;
}
&__form-textarea {
height: 150px;
}
&__form-label {
padding-right: 70px;
}
}
What I'm struggling with is alignment of inputs with labels and of textarea at the same line vertically.
textarea always sticks out at the side, because of its label which has longer text than the rest.
All the help will be appreciated.

I would not use a flexbox for the form, but for each <div> inside the form. The labels are vertically centered for each input field, except for the textarea. There the label is vertically aligned at the top. You can play with top padding there when desired.
* {
box-sizing: border-box;
}
.contact__right {
background-color: gray;
}
.contact__form {
padding: 105px 95px;
width: 100%;
color: white;
}
.contact__form>div {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 25px;
}
.contact__form-input,
.contact__form-textarea {
width: 300px;
height: 40px;
background: transparent;
border: 1px solid white;
border-radius: 2px;
}
.contact__form-textarea {
height: 150px;
}
label[for="message"].contact__form-label {
align-self: flex-start;
}
<div class="contact__right">
<form action="" class="contact__form">
<div>
<label for="name" class="contact__form-label">Name</label>
<input type="text" id="name" class="contact__form-input">
</div>
<div>
<label for="email" class="contact__form-label">Email</label>
<input type="text" id="email" class="contact__form-input">
</div>
<div>
<label for="phone" class="contact__form-label">Phone</label>
<input type="text" id="phone" class="contact__form-input">
</div>
<div>
<label for="message" class="contact__form-label">Message</label>
<textarea name="message" id="message" cols="30" rows="10" class="contact__form-textarea"></textarea>
</div>
</form>
</div>

Add following css code and your problem will be solved.
.contact__form-label {
vertical-align: top;}

Add this CSS rule for label:
label {
width: 100px;
display: inline-block;
vertical-align: top;
}
(otherwise label will be treated as an inline-element. This way you give it a fixed width and align it to the top)
If you don't like the alignment at the very top, you can add a padding-top ike in the following snippet:
form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 105px 95px;
color: $white;
}
input,
textarea {
width: 300px;
height: 40px;
background: transparent;
border: 1px solid white;
border-radius: 2px;
margin-bottom: 25px;
}
textarea {
height: 150px;
}
label {
width: 100px;
display: inline-block;
vertical-align: top;
padding-top: 14px;
}
.contact__right {
background: #ccc;
}
<div class="contact__right">
<form action="" class="contact__form">
<div>
<label for="name" class="contact__form-label">Name</label>
<input type="text" id="name" class="contact__form-input">
</div>
<div>
<label for="email" class="contact__form-label">Email</label>
<input type="text" id="email" class="contact__form-input">
</div>
<div>
<label for="phone" class="contact__form-label">Phone</label>
<input type="text" id="phone" class="contact__form-input">
</div>
<div>
<label for="message" class="contact__form-label">Message</label>
<textarea name="message" id="message" cols="30" rows="10" class="contact__form-textarea"></textarea>
</div>
</form>
</div>
ยด

You are gonna have to do that manually I guess. Normally websites tend to break line after the labels and bring the input fields to the next line.

Related

Align 3 inputs in the same row with label above

I have a small issue here with aligning three input fields in the same row with equal space between each other.
I tried to do it with display: flex and space-between but then the label is placed left to the input and not on-top.
What I want to achieve is as followed:
Then if there is more inputs I want it to "break a row" and start a new 3 input row.
How can I achieve it ?
Code snippet:
/*Stage 3 input group*/
.dInput{
margin-top: 15px;
}
.dInput label {
display: block;
padding: 6px;
color: #f8f9fa;
}
.dInput input {
padding: 5px;
margin-left: 5px;
width: 33%;
background-color: #495057;
border-radius: 4px;
}
<div class="dInput" v-if="activeStage == 3">
<label for="name">Username*</label>
<input type="text" id="name">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
I think this is what you want. I hope my code is helpful to you.
/*Stage 3 input group*/
.dInput{
display:flex;
justify-content:space-between;
align-items:center;
width:100%;
margin-top: 15px;
}
.dInput label {
text-align:center;
display: block;
padding: 6px;
color: #000;
}
.dInput input {
padding: 5px;
margin-left: 5px;
background-color: #495057;
border-radius: 4px;
}
<div class="dInput" v-if="activeStage == 3">
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
</div>
<div class="dInput" v-if="activeStage == 3">
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
</div>
display: flex implementation
.dInput {
margin-top: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.input-wrapper {
display: flex;
flex-direction: column;
max-width: 33%;
flex: 33%;
}
.dInput label {
display: block;
padding: 6px;
/* color: #f8f9fa; */
}
.dInput input {
padding: 5px;
margin-left: 5px;
width: 33%;
background-color: #495057;
border-radius: 4px;
}
<div class="dInput">
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
</div>
With css grid :
/*Stage 3 input group*/
.dInput{
margin-top: 15px;
display: grid;
grid-template-columns: repeat(3, 1fr);
align-items: center;
justify-content: space-between;
justify-items: center;
background-color: violet;
gap: 1em;
padding: 1em;
}
.dInput label {
display: block;
color: #f8f9fa;
}
.dInput input {
padding: 5px;
background-color: #495057;
border-radius: 4px;
max-width: 8em;
margin-top: 1em;
}
.dInput-item {
justify-self: center;
text-align: center;
}
<div class="dInput" v-if="activeStage == 3">
<div class="dInput-item">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="dInput-item">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="dInput-item">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="dInput-item">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
</div>
Wrap each label and input in a div
<div class="dInput">
<div>
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div>
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div>
<label for="name">Username*</label>
<input type="text" id="name">
</div>
</div>
css:
.dInput{
margin-top: 15px;
display:flex;
justify-content: space-between;
}

input text box does not adapt with window size

I am aiming to make text boxes that adapt to the size of the browser window. I'm using css flex properties. The textboxes in the code below do not adapt with the screen size, and I'm not sure exactly why. I believe it might be because I have an outer div that's interacting with the css properties for the actual textboxes.
One other thing that I'm not sure how to do, is to align the button to the right edge of the text boxes, so that the button does not go past the textboxes. Because the textboxes are not dynamically reshaping their width I'm not sure how to proceed with the button.
.search_btn {
width: 150px;
padding: 10px;
background-color: black;
color: white;
border: 0;
}
.search_btn:hover {
box-shadow: 1px 1px 1px grey;
/*background-color: darkblue;*/
}
input {
justify-content: flex-left;
padding: 5px;
position: absolute;
margin-left: 200px;
}
.txt_box {
display: flex;
width: 80%;
}
.search_section {
/*padding: 5px;*/
padding-top: 10px;
padding-bottom: 10px;
}
.search_options {
padding-bottom: 10px;
}
.row {
display: flex;
flex-direction: row;
align-items: left;
justify-content: left;
padding-top: 10px;
padding-bottom: 10px;
}
<form action="/action_page.php">
<div class="search_section">
<div class="search_options">
<h3 class="tit_label" for="fname">SHAPES</h3>
<div class="row">
<label class="left_label" for="fname">average square:</label>
<!-- <div id="wrapper" -->
<input class="txt_box" type="text" id="fname" name="fname" />
</div>
<div class="row">
<label class="left_label" for="lname">average circle:</label>
<input class="txt_box" type="text" id="lname" name="lname" />
</div>
</div>
<div class="form-buttons">
<input class="search_btn" type="submit" value="calculate" />
</div>
</div>
</form>
You need to remove position: absolute from input to make it flex-item. Since you have made absolute positioned then it comes out of the normal flow, then you don't need to use display: flex on input.
No need to use the below style, since txt_box will be a flex item then it will adjust its width accordingly
.txt_box { // Not Required
display: flex;
width: 80%;
}
There is no such property justify-content: flex-left;. There is justify-content: flex-start; instead
.search_btn {
width: 150px;
padding: 10px;
background-color: black;
color: white;
border: 0;
}
.search_btn:hover {
box-shadow: 1px 1px 1px grey;
}
input {
padding: 5px;
}
.search_section {
padding-top: 10px;
padding-bottom: 10px;
}
.search_options {
padding-bottom: 10px;
}
.row {
display: flex;
padding-top: 10px;
padding-bottom: 10px;
}
.left_label {
flex-basis: 20%;
}
.txt_box {
margin: 0;
flex-basis: 80%;
}
<form action="/action_page.php">
<div class="search_section">
<div class="search_options">
<h3 class="tit_label" for="fname">SHAPES</h3>
<div class="row">
<label class="left_label" for="fname">average square:</label>
<input class="txt_box" type="text" id="fname" name="fname" />
</div>
<div class="row">
<label class="left_label" for="lname">average circle:</label>
<input class="txt_box" type="text" id="lname" name="lname" />
</div>
</div>
<div class="form-buttons">
<input class="search_btn" type="submit" value="calculate" />
</div>
</div>
</form>

message box to show in right column

I want to design the responsive contact form where the layout is name, email, subject fields be in one column and message field be in the another column. I could not make that message field appear in another column though I have used the width as 50%. Here is the demo
http://jsbin.com/vuxojuqeve/edit?html,css,output
The code
#responsive-form {
max-width: 600px;
margin: 0 auto;
width: 100%;
}
.form-row {
width: 100%;
}
.form-row input {
margin: 10px 0;
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
.button {
float: left;
background: #CA0002;
color: #fff;
text-transform: uppercase;
border: none;
padding: 8px 20px;
cursor: pointer;
}
input[type="text"],
textarea {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 3px;
box-sizing: border-box
}
.half-size {
width: 50%;
}
<div id="responsive-form" class="clearfix">
<div class="half-size">
<div class="form-row">
<div class="column">
<input type="text" name="name" class="form-control">
</div>
</div>
<div class="form-row">
<div class="column">
<input type="text" name="name" class="form-control">
</div>
</div>
<div class="form-row">
<div class="column">
<input type="text" name="name" class="form-control">
</div>
</div>
<div class="form-row">
<div class="column">
<input type="text" name="name" class="form-control">
</div>
</div>
</div>
<div class="half-size">
<div class="form-row">
<div class="column">
<textarea class="form-control" cols="40" rows="10"></textarea>
</div>
</div>
</div>
<div class="form-row">
<div class="column">
<input type="submit" name="name" class="button">
</div>
</div>
</div>
</div>
This is what I wanted with submit button on the left column
You need to set your containers to display:inline-block, as they are block level elements by default and won't allow others to be positioned beside them.
I'm using 45% because 50% + 50% is not usually 100% when using inline-blocks because of an issue with whitespace, there are some ways around it, I'll let you decide for yourself which hack to use...
#responsive-form {
max-width: 600px;
margin: 0 auto;
width: 100%;
}
.form-row {
width: 100%;
}
.form-row input {
margin: 10px 0;
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
.button {
float: left;
background: #CA0002;
color: #fff;
text-transform: uppercase;
border: none;
padding: 8px 20px;
cursor: pointer;
}
input[type="text"],
textarea {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 3px;
box-sizing: border-box
}
.half-size {
width: 45%;
display:inline-block;
}
<div id="responsive-form" class="clearfix">
<div class="half-size">
<div class="form-row">
<div class="column">
<input type="text" name="name" class="form-control">
</div>
</div>
<div class="form-row">
<div class="column">
<input type="text" name="name" class="form-control">
</div>
</div>
<div class="form-row">
<div class="column">
<input type="text" name="name" class="form-control">
</div>
</div>
<div class="form-row">
<div class="column">
<input type="text" name="name" class="form-control">
</div>
</div>
</div>
<div class="half-size">
<div class="form-row">
<div class="column">
<textarea class="form-control" cols="40" rows="10"></textarea>
</div>
</div>
</div>
<div class="form-row">
<div class="column">
<input type="submit" name="name" class="button">
</div>
</div>
</div>
</div>
If there are columns, use flexbox.
Inputs and textarea width unit is vw and e.g. min-width: 180px.
Example
body {
margin: 0;
}
::placeholder,
input[type="submit"] {
color: #BACEE7;
font-weight: bold;
}
.form {
display: flex;
justify-content: center;
padding: 15px;
}
.form input,
.form textarea {
min-width: 180px;
background: #F2F2F2;
border: 1px solid #ccc;
text-transform: uppercase;
border-radius: 3px;
text-align: center;
box-sizing: border-box;
}
.form>.inputs {
margin-right: 5px;
}
.form>.inputs>input {
width: 30vw;
display: block;
margin-bottom: 5px;
padding: 1.5vw;
}
.form>.inputs>input:last-child {
margin-bottom: 0 !important;
}
.form textarea {
width: 35vw;
padding-top: 80px;
display: block;
height: 100%;
}
.form input[type="submit"] {
background: #CA0002;
color: white;
border: none;
cursor: pointer;
}
<div class="form">
<div class="inputs">
<input type="text" name="first_name" placeholder="first name">
<input type="text" name="last_name" placeholder="last name">
<input type="email" name="email" placeholder="email">
<input type="text" name="organization" placeholder="organization">
<input type="tel" name="phone" placeholder="phone">
<input type="submit" name="submit">
</div>
<div class="textarea">
<textarea name="" id="" cols="30" placeholder="message"></textarea>
</div>
</div>

Aligning Span Fields to the Left while Aligning Form Center

I am having an issue where I am using text-align on my container to center all of the content within it, but would like to align my <span> elements to the left within that centered container. I have tried isolating the span elements in my css and using text-align: left; but have not had any success. I feel like it is a simple fix that is slipping my mind at the moment, but hope that it can be easily resolved.
HTML:
<div class="grid">
<div class="col-1-1" id="bar-registration-container">
<h1>Register</h1>
<div id="form-fields">
<form>
<label class="form-label" id="email">
<span>Email</span>
<br />
<input id="email" type="email" name="email" />
</label>
<br />
<label class="form-label" id="password">
<span>Password</span>
<br />
<input id="password" type="password" name="password" />
</label>
<br />
<label>
<input class="form-label" id="submit" type="submit" value="Register" />
</label>
</form>
</div>
</div>
</div>
CSS:
#bar-registration-container h1{
text-align: center;
}
#form-fields {
text-align: center;
background-color: #fff;
margin: 0 auto;
padding: 20px;
}
#form-fields input[type=email] {
font-size: 27px;
margin-bottom: 20px;
}
#form-fields input[type=password] {
font-size: 27px;
margin-bottom: 10px;
}
.form-label span {
color: #000;
margin-right: 10px;
}
#submit {
margin-top: 20px;
width: 300px;
height: 30px;
background-color: #000;
color: #fff;
font-size: 15px;
border: none;
}
I believe this snippet accomplishes what you want. I just gave the form-fields div a set width, and added a few styles to those span elements to get them to stretch the full width of that div, but be left-aligned.
#bar-registration-container h1{
text-align: center;
}
#form-fields {
width: 340px;
text-align: center;
background-color: #fff;
margin: 0 auto;
padding: 20px;
}
#form-fields input[type=email] {
font-size: 27px;
margin-bottom: 20px;
}
#form-fields input[type=password] {
font-size: 27px;
margin-bottom: 10px;
}
.form-label span {
display: inline-block;
text-align: left;
color: #000;
width: 100%;
margin-left: 5px;
}
#submit {
margin-top: 20px;
width: 300px;
height: 30px;
background-color: #000;
color: #fff;
font-size: 15px;
border: none;
}
<div class="grid">
<div class="col-1-1" id="bar-registration-container">
<h1>Register</h1>
<div id="form-fields">
<form>
<label class="form-label" id="email">
<span>Email</span>
<br />
<input id="email" type="email" name="email" />
</label>
<br />
<label class="form-label" id="password">
<span>Password</span>
<br />
<input id="password" type="password" name="password" />
</label>
<br />
<label>
<input class="form-label" id="submit" type="submit" value="Register" />
</label>
</form>
</div>
</div>
</div>
In order to left align your <span> you need to left align them and in edition to that you will have to use display:block. You actually were on right track you just missed display:block. Using display:block will make your <span> to act as a block element like <div> or <p> and then it will respect alignment properties better.
span{
text-align:left;
display:block;
margin-left:50px;// adjust yourself. Was just playing around with it.
}

centering div not working

I am trying to center the text and form field inputs in the #registration-code-entry .registration-code-entry-content div
Not sure what I am doing wrong.
http://jsfiddle.net/yLX4F/
<div id="registration-code-entry">
<div class="registration-code-entry-header"></div>
<div class="registration-code-entry-middle">
<div class="registration-code-entry-content">
<form class="registration-form">
<div class="field">
<input type="text" name="first_name" placeholder="*First Name" class="required" />
</div>
<div class="field">
<input type="text" name="last_name" placeholder="*Last Name" class="required" />
</div>
<div class="field">
<input type="email" name="email" placeholder="*Email Address" class="required" />
</div>
<div class="field">
<input type="text" name="phone" placeholder="Phone Number" />
</div>
<div class="field">
<input type="checkbox" name="optin">
<label for="optin" />Yes, I would like to receive more information</label>
</div>
<div class="field">
<input type="checkbox" name="accept" class="required" />
<label for="accept">*I accept the Official Rules.</label>
</div>
<input type="submit" class="submit-btn" value="">
</form>
</div>
</div>
<div class="registration-code-entry-footer"></div>
</div>
css
#registration-code-entry {
height: 100px;
width: 359px;
position: absolute;
top: 100px;
right: 20px;
background: transparent;
.registration-code-entry-header {
top: 0;
background: transparent url('../images/form-header.png') no-repeat;
width: 359px;
height: 17px;
}
.registration-code-entry-middle {
background: #713487;
.registration-code-entry-content {
border: 1px solid red;
width: 350px;
margin: 0 auto;
color: #fff;
form {
display: inline-block;
margin: 0 auto;
.field {
margin: 10px 0;
input[type="email"], input[type="text"] {
padding: 5px;
}
}
input.submit-btn {
background: transparent url('../images/submit-btn.png') no-repeat;
width: 168px;
height: 59px;
display: block;
border: 0;
}
}
}
}
.registration-code-entry-footer {
display: block;
bottom: 0;
background: transparent url('../images/form-footer.png') no-repeat;
width: 359px;
height: 11px;
}
}
You can add text-align: centerto .field. You can also change to display: block for form to make the form expand to the full width of the parent.
jsfiddle
Centering the input ?
This will centering it :
.field input[type="text"], .field input[type="email"] {
display:block;
margin: auto;
}
Check This
Just change youre less :
...
.field {
margin: 10px 0;
input[type="email"], input[type="text"] {
padding: 5px;
display:block;
margin:0 auto;
}
}
...