Flex, Form input element overflowed - html

Hi there I am creating a form for my webpage. I am trying to implement it using flex so it can adjust to the window size. My problem is that the input box for the lastName overflows. Any pointers how I can fix this so that the last name input box and the label drops to a new line or if its possible to squeeze both in. If you could help that would be greatly appreciated! Here so pics of whats going on and the code will be below that.
form before resizing :
form after resizing :
The code:
#survey {
border: 1px solid black;
padding: 15px;
display: flex;
flex-flow: column nowrap;
}
div#names {
display: flex;
flex-flow: row nowrap;
flex: 1 1 auto;
}
div.formRow {
flex: 1 1 auto;
display: -webkit-flex;
display: flex;
flex-flow: column nowrap;
}
div.formRow>* {
flex: 1 1 auto;
}
div.horizontalRow {
display: -webkit-flex;
display: flex;
flex-flow: column nowrap;
}
div.horizontalRow>* {
flex: 1 1 auto;
}
<form id="survey" action="url" method="post">
<label>Name*</label>
<br>
<div id="names">
<div class="formRow">
<input type="text" name="firstname">
<label for="firstname">First Name</label>
</div>
<div class="formRow">
<input type="text" name="lastname">
<label for="lastname">LastName</label>
</div>
</div>
<div class="horizontalRow">
<label for="email">Email Address *</label>
<input type="email" name="email">
</div>
<div class="horizontalRow">
<label for="subject">Subject *</label>
<input type="text" name="subject">
</div>
<div class="horizontalRow">
<label for="message">Message *</label>
<input type="text" name="message">
</div>
<br>
<div id="submitDiv">
<input id="submit" type="submit" value="Submit">
</div>
</form>

You may use media query to change behavior under 400px (or any other value you want) so each input will be in a seperate line instead of having them both in the same line.
It's also better for UX/UI than having two small inputs in one line :
#media all and (max-width:400px) {
div#names {
flex-flow: column nowrap;
}
div.formRow>input {
order:1; /*Put the label on top to behave like other fields*/
}
}
Full code :
#survey {
border: 1px solid black;
padding: 15px;
display: flex;
flex-flow: column nowrap;
}
div#names {
display: flex;
flex-flow: row nowrap;
flex: 1 1 auto;
}
div.formRow {
flex: 1 1 auto;
display: -webkit-flex;
display: flex;
flex-flow: column nowrap;
}
div.formRow>* {
flex: 1 1 auto;
}
div.horizontalRow {
display: -webkit-flex;
display: flex;
flex-flow: column nowrap;
}
div.horizontalRow>* {
flex: 1 1 auto;
}
#media all and (max-width:400px) {
div#names {
flex-flow: column nowrap;
}
div.formRow>input {
order:1;
}
}
<form id="survey" action="url" method="post">
<label>Name*</label>
<br>
<div id="names">
<div class="formRow">
<input type="text" name="firstname">
<label for="firstname">First Name</label>
</div>
<div class="formRow">
<input type="text" name="lastname">
<label for="lastname">LastName</label>
</div>
</div>
<div class="horizontalRow">
<label for="email">Email Address *</label>
<input type="email" name="email">
</div>
<div class="horizontalRow">
<label for="subject">Subject *</label>
<input type="text" name="subject">
</div>
<div class="horizontalRow">
<label for="message">Message *</label>
<input type="text" name="message">
</div>
<br>
<div id="submitDiv">
<input id="submit" type="submit" value="Submit">
</div>
</form>

Related

How to place a label on a single row/ on a single line

Two labels get separated by a line. I need to fix that(Look at First Name, Last Name)
<section id="names">
<B>First Name:</B><input type="text" name="Name" id="name">
<B>Last Name:</B><input type="text" name="Surname" id="surname">
</section>
I don't know if they will help but here I wrote some useless css for it
#names {
display:flex;
flex-direction: row;
float: left;
}
you can use white-space: nowrap;
#names {
display:flex;
flex-direction: row;
float: left;
}
label {
white-space: nowrap;
}
<section id="names">
<!--use proper tags-->
<label for="name">
<b>First Name:</b>
</label>
<input type="text" name="Name" id="name">
<label for="surname">
<b>Last Name:</b>
</label>
<input type="text" name="Surname" id="surname">
</section>
Not sure what you really want. I think you want them to be seperate by a new line. If you change the flex-direction to "column" instead of "row". It will divide them by space instead of being up against eachother.
#names {
display:flex;
flex-direction: column;
float: left;
}
<section id="names">
<B>First Name:</B><input type="text" name="Name" id="name">
<B>Last Name:</B><input type="text" name="Surname" id="surname">
</section>

How do I get the form labels so they are in-line and right justified, against the form elements

For the life of me I can't get my labels to be in-lin and right justified against form elements, rather I am getting this
section {
display: flex;
flex-direction: row;
justify-content: center;
width: 960px;
margin: 0 auto;
}
.feedback_right {
display: flex;
flex-direction: row;
flex: 1;
}
/* Contact Form Styling */
form#contact_form {
display: flex;
flex-direction: column;
padding: 0;
margin: auto;
}
form#contact_form p {
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
padding: 0;
}
form#contact_form p > label {
display: inline-block;
padding-right: 2px;
text-align: right;
}
form#contact_form p > input {
font-family: monospace;
font-size: 1em;
}
button {
display: inline-block;
width: 25%;
margin: 0 0 0 auto;
margin-bottom: 5px;
}
<section>
<div class="feedback_right">
<form id="contact_form">
<p>
<label for="contact_name">Full Name:</label>
<input type="text" id="contact_name" name="contact_name" placeholder="Name">
</p>
<p>
<label for="contact_email">Email Address:</label>
<input type="email" id="contact_email" name="contact_email" placeholder="Email">
</p>
<p>
<label for="contact_number">Mobile Number:</label>
<input type="tel" id="contact_number" name="contact_number" placeholder="Mobile Number">
</p>
<p>
<label for="victoria">Victoria</label>
<input type="radio" id="victoria" name="victoria">
</p>
<p>
<label for="queensland">Queensland</label>
<input type="radio" id="queensland" name="queensland">
</p>
<p>
<label for="new_south_wales">New South Wales</label>
<input type="radio" id="new_south_wales" name="new_south_wales">
</p>
<p>
<label for="act">Australian Capital Territory</label>
<input type="radio" id="act" name="act">
</p>
<p>
<label for="tasmania">Tasmania</label>
<input type="radio" id="tasmania" name="tasmania">
</p>
<p>
<label for="south_australia">South Australia</label>
<input type="radio" id="south_australia" name="south_australia">
</p>
<p>
<label for="western_australia">Western Australia</label>
<input type="radio" id="western_australia" name="western_australia">
</p>
<p>
<label for="northern_territory">Northern Territory</label>
<input type="radio" id="northern_territory" name="northern_territory">
</p>
<p>
<label for="contact_message">Message:</label>
<textarea rows="4" cols="40" id="contact_message" name="contact_message" placeholder="Message"></textarea>
</p>
<button type="submit">Submit</button>
</form>
</div>
</section>
I want the submit button to stay exactly where it is, I just need some assistance in troubleshooting and getting the labels right align against the input elements that are all nicely aligned.
My desired outcome would be something similar to this - just without that vertical divider line.
It shouldn't be really hard to do, if you are not just a backend dev with little knowledge of css. Just float them right!!!, and then play around with the max-width of the whole form as you like.
I have shown #3 method to right justify the label items, there are plenty of ways to do that though.
section {
display: flex;
flex-direction: row;
justify-content: center;
width: 960px;
margin: 0 auto;
}
.feedback_right {
display: flex;
flex-direction: row;
flex: 1;
}
/* Contact Form Styling */
form#contact_form {
display: flex;
flex-direction: column;
padding: 0;
margin: auto;
}
form#contact_form p {
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
padding: 0;
}
form#contact_form p > label {
display: inline-block;
padding-right: 2px;
text-align: right;
}
form#contact_form p > input {
font-family: monospace;
font-size: 1em;
}
button {
display: inline-block;
width: 25%;
margin: 0 0 0 auto;
margin-bottom: 5px;
}
/*Why not just float them right??*/
label {
float: right;
}
label[for="contact_message"] {
float: none;
}
input {
float: right;
margin-left: 52px;
}
input[type="radio"] {
margin-left: 200px;
}
<section>
<div class="feedback_right">
<form id="contact_form">
<p>
<input type="text" id="contact_name" name="contact_name" placeholder="Name">
<label for="contact_name">Full Name:</label>
</p>
<p>
<input type="email" id="contact_email" name="contact_email" placeholder="Email">
<label for="contact_email">Email Address:</label>
</p>
<p>
<input type="tel" id="contact_number" name="contact_number" placeholder="Mobile Number">
<label for="contact_number">Mobile Number:</label>
</p>
<p>
<input type="radio" id="victoria" name="victoria">
<label for="victoria">Victoria</label>
</p>
<p>
<input type="radio" id="queensland" name="queensland">
<label for="queensland">Queensland</label>
</p>
<p>
<input type="radio" id="new_south_wales" name="new_south_wales">
<label for="new_south_wales">New South Wales</label>
</p>
<p>
<input type="radio" id="act" name="act">
<label for="act">Australian Capital Territory</label>
</p>
<p>
<input type="radio" id="tasmania" name="tasmania">
<label for="tasmania">Tasmania</label>
</p>
<p>
<input type="radio" id="south_australia" name="south_australia">
<label for="south_australia">South Australia</label>
</p>
<p>
<label for="contact_message">Message:</label>
<textarea rows="4" cols="40" id="contact_message" name="contact_message" placeholder="Message"></textarea>
</p>
<button type="submit">Submit</button>
</form>
</div>
</section>
Flexbox approach:
section {
display: flex;
flex-direction: row;
justify-content: center;
width: 960px;
margin: 0 auto;
}
.feedback_right {
display: flex;
flex-direction: row;
flex: 1;
}
/* Contact Form Styling */
form#contact_form {
display: flex;
flex-direction: column;
padding: 0;
margin: auto;
}
form#contact_form p {
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
padding: 0;
}
form#contact_form p > label {
display: inline-block;
padding-right: 2px;
text-align: right;
}
form#contact_form p > input {
font-family: monospace;
font-size: 1em;
}
button {
display: inline-block;
width: 25%;
margin: 0 0 0 auto;
margin-bottom: 5px;
}
/*changes*/
form#contact_form p {
display: flex;
justify-content: space-between;
}
form#contact_form p > label {
flex-basis: 50%;
margin-right: 40px;
}
<section>
<div class="feedback_right">
<form id="contact_form">
<p>
<label for="contact_name">Full Name:</label>
<input type="text" id="contact_name" name="contact_name" placeholder="Name">
</p>
<p>
<label for="contact_email">Email Address:</label>
<input type="email" id="contact_email" name="contact_email" placeholder="Email">
</p>
<p>
<label for="contact_number">Mobile Number:</label>
<input type="tel" id="contact_number" name="contact_number" placeholder="Mobile Number">
</p>
<p>
<label for="victoria">Victoria</label>
<input type="radio" id="victoria" name="victoria">
</p>
<p>
<label for="queensland">Queensland</label>
<input type="radio" id="queensland" name="queensland">
</p>
<p>
<label for="new_south_wales">New South Wales</label>
<input type="radio" id="new_south_wales" name="new_south_wales">
</p>
<p>
<label for="act">Australian Capital Territory</label>
<input type="radio" id="act" name="act">
</p>
<p>
<label for="tasmania">Tasmania</label>
<input type="radio" id="tasmania" name="tasmania">
</p>
<p>
<label for="south_australia">South Australia</label>
<input type="radio" id="south_australia" name="south_australia">
</p>
<p>
<label for="western_australia">Western Australia</label>
<input type="radio" id="western_australia" name="western_australia">
</p>
<p>
<label for="northern_territory">Northern Territory</label>
<input type="radio" id="northern_territory" name="northern_territory">
</p>
<p>
<label for="contact_message">Message:</label>
<textarea rows="4" cols="40" id="contact_message" name="contact_message" placeholder="Message"></textarea>
</p>
<button type="submit">Submit</button>
</form>
</div>
</section>
You can eliminate the last textarea label from flex positioning yourself.

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>

CSS media query and flex direction

I am following Traversy course on Udemy, i am less than a month in HTML CSS, so kind of a newbie. So in a section of the course he makes us do a website and in the last episodes he explains how to make it responsive for devices.
This is the problematic section on the html:
<!-- Section: Contact -->
<section id="contact">
<div class="contact-form bg-primary p-2">
<h2 class="m-heading">Contact Us</h2>
<p>Please use the form below to contact us</p>
<form action="">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input type="email" name="email" id="email" placeholder="Enter E-mail">
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="text" name="phone" id="phone" placeholder="Enter Phone Number">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" id="message" placeholder="Enter Message"></textarea>
</div>
<input type="sumbit" value="Send" class="btn btn-dark">
</form>
</div>
<div class="map"></div>
</section>
And this is what we did on the main css file:
/* Section: Contact */
#contact {
display: flex;
}
#contact .map, #contact .contact-form {
flex: 1;
}
#contact .contact-form .form-group {
margin: 0.75rem;
}
#contact .contact-form label {
display: block;
}
#contact .contact-form input,
#contact .contact-form textarea {
width: 100%;
padding: 0.5rem;
}
We had to make the map just go under the contact form on the mobile css sheet, but, put simply it worked only when he did it on his computer. on mine the map disappears XD:
#contact {
flex-direction: column;
}
#contact .map {
height: 200px;
}
I tried to workaround:
#contact {
flex-wrap: wrap;
}
#contact .contact-form {
display: block;
}
#contact .map {
display: block;
}
But the same thing happened. Suppose I'm not yet good enough to understand what is happening, let alone fix it myself XD
Just add:
#contact {
display: block;
flex-direction: column;
}

How do I make the following input areas expand until the end of the screen?

This is a Bootstrap-like form:
HTML:
<div class="container">
<div class="row">
<div class="form-group row-group">
<label class="col-6">First name</label>
<input class="col-6" type="text" value=""></input>
</div>
<div class="form-group row-group">
<label class="col-6">Last name</label>
<input class="col-6" type="text" value=""></input>
</div>
</div>
<div class="row">
<div class="form-group row-group">
<label class="col-4">Message</label>
<input class="col-8" type="text" value=""></input>
</div>
</div>
<div class="row">
<div class="form-group row-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
<div class="form-group row-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
<div class="form-group row-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
</div>
<div class="row">
<div class="col-12 row-group">
<button class="btn">Submit</button>
</div>
</div>
</div>
CSS:
.row {
display: flex
}
.row-group {
display: flex
flex-direction: row !important
align-items: center
label {
flex: 0 0 100px
}
input {
flex: 1
}
}
.form-group {
flex: 1 0 0
flex-direction: column
}
This is the result:
I want the inputs of the first row and third row to reach the end of the container div (to not to stack horizontally with its neigbhor).
I tried this:
.row-group {
input: flex: 1 1 100%
}
But nothing moves.
What's the correct way to do this?
Please check out this Fiddle - is this what you're going for?
https://jsfiddle.net/jspruance/1p739t1h/
Here's the modified CSS:
.row {
xdisplay: flex;
}
.row-group {
display: flex;
flex-direction: row !important;
align-items: center;
}
label {
flex: 0 0 100px;
}
input {
width: 100%;
box-sizing: border-box;
flex: 1;
}
.form-group {
flex: 1 0 0;
flex-direction: column;
}
I fixed a few problems with the CSS:
1) Extra close bracket after input element
2) Missing bracket after .row-group
3) Added 'width: 100%' property and 'box-sizing: border-box'
4) Floated label left.
5) Commented out 'display: flex' on .row ( not needed)
Hope this helps.