inline field are merged when adjusting label - html

I am trying to create a responsive form using flex where the first name and last name will be in same column in large devices and be in separate column in small devices. I designed the form and it was working as expected(not the responsive part though)until I adjust the label in the input box. I used the position: absolute to adjust the label, so it break the design of first name and last name field. They got merge now.
Here is the demo
http://jsbin.com/pecijupicu/3/edit?html,css
Meanwhile, here is the source code
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html,
body {
background: #f2f3f78a;
-webkit-font-smoothing: antialiased;
}
aside.form-container {
display: flex;
flex-direction: column;
flex: 1;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px 0 #f0efef;
margin: 0 40px;
border-radius: 6px;
}
.form-container h3 {
color: rgb(67, 77, 90);
}
form {
min-width: 400px;
width: 600px;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 auto;
}
form .field {
margin: 15px 0;
/* flex-direction: column; */
display: flex;
position: relative;
}
.form-input {
flex: 1;
}
label {
position: absolute;
top: 4px;
}
form .field label {
margin: 10px 0;
color: rgb(67, 77, 90);
}
button.next {
padding: 10px 0;
width: 100px;
background: rgb(0, 213, 229);
border: none;
display: flex;
justify-content: center;
}
button.next:after {
content: "Next";
color: #fff;
}
input[type="text"],
textarea {
padding: 8px;
border: transparent;
border-bottom: solid 1px #999;
}
input[type="text"]:focus,
textarea:focus {
outline: none;
border-bottom: solid 1px rgb(0, 213, 229);
}
textarea {
width: 100%;
}
<aside class="form-container">
<h3>Personal</h3>
<form>
<div class="field">
<label>First Name</label>
<input type="text" name="name" class="form-input" />
<label>Last Name</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Email</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Country</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>City</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Description</label>
<textarea></textarea>
</div>
<!-- show button to the bottom right corner -->
<button class="next"></button>
</form>
</aside>

I have made some changes to the html structure and added some extra styles to suit your use case. My solution supports responsiveness as well.
link to working example: JSbin
Here is the code:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html, body {
background: #f2f3f78a;
-webkit-font-smoothing: antialiased;
}
aside.form-container {
display: flex;
flex-direction: column;
flex: 1;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px 0 #f0efef;
margin: 0 40px;
border-radius: 6px;
}
.form-container h3{
color: rgb(67, 77, 90);
}
form {
width: 100%;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 auto;
}
form .field {
margin: 15px 0;
/* flex-direction: column; */
display: flex;
position: relative;
}
form .combined{
display: flex;
flex-direction: row;
}
.form-input {
flex: 1;
}
form .combined .field{
flex:1;
justify-content: space-between;
/* border: 1px solid red; */
}
form .combined .field .form-input{
margin-right: 20px;
}
label {
position: absolute;
top: 4px;
}
form .field label {
margin: 10px 0;
color: rgb(67, 77, 90);
}
button.next {
padding: 10px 0;
width: 100px;
background: rgb(0,213,229);
border: none;
display: flex;
justify-content: center;
}
button.next:after {
content: "Next";
color: #fff;
}
input[type="text"], textarea {
padding: 8px;
border: transparent;
border-bottom: solid 1px #999;
}
input[type="text"]:focus, textarea:focus {
outline: none;
border-bottom: solid 1px rgb(0,213,229);
}
textarea {
width: 100%;
}
#media screen and (max-width: 520px) {
form .combined{
display: flex;
flex-direction: column;
}
form .combined .field{
flex:1;
justify-content: space-between;
margin: 25px 0;
}
form .combined .field label{
position: absolute;
top: -11px;
}
form .combined .field .form-input{
margin-right: 0;
}
}
<aside class="form-container">
<h3>Personal</h3>
<form>
<div class="combined">
<div class="field">
<label>First Name</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Last Name</label>
<input type="text" name="name" class="form-input" />
</div>
</div>
<div class="field">
<label>Email</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Country</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>City</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Description</label>
<textarea></textarea>
</div>
<!-- show button to the bottom right corner -->
<button class="next"></button>
</form>
</aside>

Add left: 50% to the lastname label - it makes sense as the absolutely positioned labels share the full-width 50-50 due flex: 1 given to them. See demo below:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html,
body {
background: #f2f3f78a;
-webkit-font-smoothing: antialiased;
}
aside.form-container {
display: flex;
flex-direction: column;
flex: 1;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px 0 #f0efef;
margin: 0 40px;
border-radius: 6px;
}
.form-container h3 {
color: rgb(67, 77, 90);
}
form {
min-width: 400px;
width: 600px;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 auto;
}
form .field {
margin: 15px 0;
/* flex-direction: column; */
display: flex;
position: relative;
}
.form-input {
flex: 1;
}
label {
position: absolute;
top: 4px;
}
form .field label {
margin: 10px 0;
color: rgb(67, 77, 90);
}
form .field label:nth-of-type(2) {
left: 50%; /* ADDED */
}
button.next {
padding: 10px 0;
width: 100px;
background: rgb(0, 213, 229);
border: none;
display: flex;
justify-content: center;
}
button.next:after {
content: "Next";
color: #fff;
}
input[type="text"],
textarea {
padding: 8px;
border: transparent;
border-bottom: solid 1px #999;
}
input[type="text"]:focus,
textarea:focus {
outline: none;
border-bottom: solid 1px rgb(0, 213, 229);
}
textarea {
width: 100%;
}
<aside class="form-container">
<h3>Personal</h3>
<form>
<div class="field">
<label>First Name</label>
<input type="text" name="name" class="form-input" />
<label>Last Name</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Email</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Country</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>City</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Description</label>
<textarea></textarea>
</div>
<!-- show button to the bottom right corner -->
<button class="next"></button>
</form>
</aside>

Related

html forms responsive in mobile view?

I have a form coded like this I want to make it responsive in both mobile and tablet version or responsive regardless:
</div>
<div class="contact_form">
<form
class="form"
action="https://sheetdb.io/api/v1/it1l3r9npizts"
method="post"
id="sheetdb-form"
>
<label for="name">Name:</label>
<input
type="text"
id="name"
name="data[name]"
placeholder="Your name"
/>
<label for="email">Email:</label>
<input
type="email"
id="email"
name="data[email]"
placeholder="Your email"
/>
<label for="message">Message:</label>
<textarea
id="message"
placeholder="Your message"
rows="6"
name="data[message]"
></textarea>
<div class="center">
<input type="submit" value="Send Message" />
</div>
</form>
</div>
with these styles in css:
.contact_section {
width: 100%;
height: 100%;
}
.contact_form {
display: flex;
align-items: center;
flex-direction: column;
}
.contact_form label {
margin: 15px 0;
}
.contact_form label {
font-size: 20px;
font-weight: 400;
}
.contact_form input,
textarea {
width: 100%;
padding: 10px;
outline: none;
resize: none;
border: none;
border-bottom: 1px solid var(--contrast-color);
}
input[type="text"]:focus,
textarea:focus {
border-bottom: 2px solid var(--main-color);
}
textarea::-webkit-scrollbar {
width: 4px;
}
textarea::-webkit-scrollbar-thumb {
background-color: var(--contrast-color);
}
.center {
text-align: center;
width: 602px;
font-size: 20px;
}
input[type="submit"] {
margin-top: 30px;
width: 100%;
max-width: 200px;
background-color: var(--netural-color);
font-size: 17px;
color: var(--contrast-color);
font-weight: 400;
cursor: pointer;
}
I have been trying to make it responsive cause my form doesn't shrink down and there isn't any padding on the left and right when the screen keeps getting
smaller:
#media screen and (max-width: 768px) {
body {
overflow-x: hidden;
}
.contact_form input textarea {
width: 70%;
margin-top: 0;
.home-section {
height: 100vh;
}
section {
height: 100%;
width: 100%;
}
}
Based on your code I suspect the .center and it's fixed width is setting the width of your form. Just remove that width or make it responsive.
.center {
text-align: center;
width: 602px;
font-size: 20px;
}
<form>
<div class="center">
<input type="submit" value="Send Message" />
</div>
</form>

Header and footer being pushed left at 375 & 320 mobile view, but fine in all else?

So my footer and header (header is ONLY an image btw) are being pushed left once i go to 375 or 320 mobile view. But on all else it's totally fine, 425px view included. I have tried removing the form margin/padding and fieldset margin/padding, that made no difference. I have tried using background cover/fill for the header image, that did nothing either.
Everything else on the page is right where it should be, only the header and footer are being pushed left at those views.
This is my code
body {
font-family: 'Nunito', sans-serif;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
overflow-x: hidden;
}
* {
box-sizing: border-box;
}
/* Header css */
header {
display: flex;
text-align: center;
}
#header_img {
width: 100%;
max-height: 150px;
}
/* Form css */
form {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
padding: 0;
}
/* Fieldset and Legend*/
fieldset {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
padding: 1em;
margin: 1em 0;
border: solid 1px #ccc;
border-radius: 6px;
min-width: 200px;
}
legend {
padding: 0 .25em;
font-size: 1.25em;
color: rgb(34, 34, 34);
}
/* Labels */
label {
display: block;
margin-top: .5em;
}
label:first-of-type {
margin-top: 0;
}
/* Inputs and textarea */
input {
width: 15em;
padding: .5em;
border: solid 1px #999;
font-size: 12px;
}
#first_name, #last_name {
font-weight: bold;
font-size: 14px;
}
#email {
font-style: italic;
}
textarea {
min-height: 8em;
min-width: 100%;
padding: .5em;
border: solid 1px #999;
}
input[type="checkbox"] {
width: 1em;
}
input[type="submit"] {
padding: .5em 1em;
margin-top: 1em;
border-radius: 6px;
background-color: #333;
color: #fff;
font-size: .8em;
width: 7em;
}
input, textarea {
border-radius: 2px;
}
#terms_and_conditions_div {
display: flex;
flex-direction: row;
align-items: baseline;
}
/* Footer css*/
footer {
display: flex;
justify-content: center;
position: absolute;
width: 100%;
text-align: center;
font: 18px bold;
}
/* Large screen rules */
#media screen and (min-width: 430px) {
legend {
font-size: 1.75em;
}
fieldset {
padding: 1em 2em;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght#400&display=swap" rel="stylesheet">
<link rel="stylesheet" href="./main.css">
<title>Event</title>
</head>
<body>
<header>
<img id="header_img" src="./Code_challenge_img.png" alt="Event location image">
</header>
<form id="contact_form" name="contact_form" method="POST">
<fieldset>
<legend>Contact Details</legend>
<label for="first_name">First name:</label>
<input type="text" id="first_name" name="first_name" placeholder="First name" maxlength="30">
<label for="last_name">Last name:</label>
<input type="text" id="last_name" name="last_name" placeholder="Last name" maxlength="30">
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" placeholder="Phone number" maxlength="30">
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="Email" maxlength="50">
<label for="promo_code">Promo Code:</label>
<input type="text" name="promo_code" id="promo_code" placeholder="Promo code" maxlength="7">
<label for="how_did_you_hear_menu">How did you hear:</label>
<select name="how_did_you_hear_menu" id="how_did_you_hear_menu">
<option value="Social Media">Social Media</option>
<option value="From a friend">From a friend</option>
<option value="Email">Email</option>
<option value="Other">Other</option>
</select>
<label for="how_did_you_hear_other">Please specify:</label>
<textarea name="how_did_you_hear_other" id="how_did_you_hear_other" rows="4" cols="50" placeholder="Please specify" maxlength="255"></textarea>
<div id="terms_and_conditons_div">
<input type="checkbox" name="terms_and_conditions" id="terms_and_conditions">
I agree to the terms and conditions of this event.
</div>
<input type="submit" id="form_submit_btn" value="Submit">
</fieldset>
</form>
<footer>
<p id="footer_text">For assistance please call 555-5555 or email test#test.com.</p>
</footer>
</body>
</html>
Thanks for any help everyone!
It is not the header or footer. The problem here is the textarea, it is more then 100% width. Change in the html and css the textarea code as shown below to have a correct header/form/footer.
body {
font-family: 'Nunito', sans-serif;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
overflow-x: hidden;
}
* {
box-sizing: border-box;
}
/* Header css */
header {
display: flex;
text-align: center;
}
#header_img {
width: 100%;
max-height: 150px;
}
/* Form css */
form {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
padding: 0;
}
/* Fieldset and Legend*/
fieldset {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
padding: 1em;
margin: 1em 0;
border: solid 1px #ccc;
border-radius: 6px;
min-width: 200px;
}
legend {
padding: 0 .25em;
font-size: 1.25em;
color: rgb(34, 34, 34);
}
/* Labels */
label {
display: block;
margin-top: .5em;
}
label:first-of-type {
margin-top: 0;
}
/* Inputs and textarea */
input {
width: 15em;
padding: .5em;
border: solid 1px #999;
font-size: 12px;
}
#first_name, #last_name {
font-weight: bold;
font-size: 14px;
}
#email {
font-style: italic;
}
textarea {
min-height: 8em;
width: 100%;
padding: .5em;
border: solid 1px #999;
}
input[type="checkbox"] {
width: 1em;
}
input[type="submit"] {
padding: .5em 1em;
margin-top: 1em;
border-radius: 6px;
background-color: #333;
color: #fff;
font-size: .8em;
width: 7em;
}
input, textarea {
border-radius: 2px;
}
#terms_and_conditions_div {
display: flex;
flex-direction: row;
align-items: baseline;
}
/* Footer css*/
footer {
display: flex;
justify-content: center;
position: absolute;
width: 100%;
text-align: center;
font: 18px bold;
}
/* Large screen rules */
#media screen and (min-width: 430px) {
legend {
font-size: 1.75em;
}
fieldset {
padding: 1em 2em;
}
}
<header>
<img id="header_img" src="https://placeimg.com/1000/150/animals" alt="Event location image">
</header>
<form id="contact_form" name="contact_form" method="POST">
<fieldset>
<legend>Contact Details</legend>
<label for="first_name">First name:</label>
<input type="text" id="first_name" name="first_name" placeholder="First name" maxlength="30">
<label for="last_name">Last name:</label>
<input type="text" id="last_name" name="last_name" placeholder="Last name" maxlength="30">
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" placeholder="Phone number" maxlength="30">
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="Email" maxlength="50">
<label for="promo_code">Promo Code:</label>
<input type="text" name="promo_code" id="promo_code" placeholder="Promo code" maxlength="7">
<label for="how_did_you_hear_menu">How did you hear:</label>
<select name="how_did_you_hear_menu" id="how_did_you_hear_menu">
<option value="Social Media">Social Media</option>
<option value="From a friend">From a friend</option>
<option value="Email">Email</option>
<option value="Other">Other</option>
</select>
<label for="how_did_you_hear_other">Please specify:</label>
<textarea name="how_did_you_hear_other" id="how_did_you_hear_other" rows="4" placeholder="Please specify" maxlength="255"></textarea>
<div id="terms_and_conditons_div">
<input type="checkbox" name="terms_and_conditions" id="terms_and_conditions">
I agree to the terms and conditions of this event.
</div>
<input type="submit" id="form_submit_btn" value="Submit">
</fieldset>
</form>
<footer>
<p id="footer_text">For assistance please call 555-5555 or email test#test.com.</p>
</footer>
Just wrap the elements you want do be in the center in a <div>, and style them to be position: relative; left: 50%;, so this will allow them to be 50% which is in the center, depending on the device.
EDIT: I have done some research and 50% positioning does not center the element, instead use position: relative; left: 50%; just like last, but them use margin-left: -30%;. And this will definetely center the element.

How to make some elements visible in place of others on click of a button in a website?

I am trying to make a login and signup form which just takes username and password as input by default and when the user presses sign in button new set of inputs like first name, last name, date of birth etc. have to be taken..I tried to write a code with the intention of achieving the same but it doesn't work..
#form{
width: 300px;
margin: 40px auto;
display: flex;
flex-direction: column;
justify-content: center;
border: 3px solid rgb(6, 61, 30);
padding: 20px;
border-radius: 30px;
background-color: #e4e1ff;
}
.butts{
display: flex;
justify-content: center;
position: relative;
}
#form .butt{
width: 150px;
height: 50px;
font-size: 25px;
display: inline-block;
background-color: #e4e1ff;
border: none;
}
hr{
height: 4px;
width: 150px;
margin: 0;
background: tomato;
border: none;
transition: 0.2s ease-in-out;
position: absolute;
left: 0px;
bottom: 0px;
}
#butt1:focus ~ hr{
left: 0;
}
#butt2:focus ~ hr{
left: 50%;
}
#login_form{
margin-top: 20px;
}
#login_form input{
width: 300px;
height: 50px;
font-size: 20px;
margin-bottom: 10px;
border-radius: 20px;
}
#signup_form{
display: none;
}
#butt2:focus ~ #signup_form{
display: block;
}
#butt2:focus ~ #login_form{
display: none;
}
<section id="form">
<div class="butts">
<button class="butt" id="butt1">Login</button>
<button class="butt" id="butt2">Sign Up</button>
<hr>
</div>
<div id="login_form">
<input type="text" placeholder="username">
<input type="password" placeholder="password">
</div>
<div id="signup_form">
<input type="text" placeholder="First Name">
<input type="text" placeholder="Last Name">
<input type='date' placeholder="Birthdate">
<input type='text' placeholder="username">
<input type="password" placeholder="password">
<input type="password" placeholder="confirm password">
<button>Connect Github</button>
<button>Create Account</button>
</div>
</section>
function formSwitch(){
var lf=document.getElementById('login_form');
var sf=document.getElementById('signup_form');
if(event.target.id == "butt1"){
lf.style.display='block';
sf.style.display='none';
}else{
lf.style.display='none';
sf.style.display='block';
}
}
#form{
width: 300px;
margin: 40px auto;
display: flex;
flex-direction: column;
justify-content: center;
border: 3px solid rgb(6, 61, 30);
padding: 20px;
border-radius: 30px;
background-color: #e4e1ff;
}
.butts{
display: flex;
justify-content: center;
position: relative;
}
#form .butt{
width: 150px;
height: 50px;
font-size: 25px;
display: inline-block;
background-color: #e4e1ff;
border: none;
}
hr{
height: 4px;
width: 150px;
margin: 0;
background: tomato;
border: none;
transition: 0.2s ease-in-out;
position: absolute;
left: 0px;
bottom: 0px;
}
#butt1:focus ~ hr{
left: 0;
}
#butt2:focus ~ hr{
left: 50%;
}
#login_form{
margin-top: 20px;
}
#login_form input{
width: 300px;
height: 50px;
font-size: 20px;
margin-bottom: 10px;
border-radius: 20px;
}
#signup_form{
display: none;
}
#butt2:focus ~ #signup_form{
display: block;
}
#butt2:focus ~ #login_form{
display: none;
}
<section id="form">
<div class="butts">
<button class="butt" id="butt1" onClick="formSwitch()">Login</button>
<button class="butt" id="butt2" onClick="formSwitch()">Sign Up</button>
<hr/>
</div>
<div id="login_form">
<input type="text" placeholder="username">
<input type="password" placeholder="password">
</div>
<div id="signup_form">
<input type="text" placeholder="First Name">
<input type="text" placeholder="Last Name">
<input type='date' placeholder="Birthdate">
<input type='text' placeholder="username">
<input type="password" placeholder="password">
<input type="password" placeholder="confirm password">
<button>Connect Github</button>
<button>Create Account</button>
</div>
</section>

How can I get the label text to the left of the input field?

I am not able to understand how I can get the label from top of the input field to the left. The label text is on top of the input field. How can I get it to the left of the input field for the contact information part? Any help would be appreciated. Thanks a lot.
This is my code:
#p1 {
text-align: center;
background-color: black;
color: white;
padding: 20px;
}
#h31 {
text-align: center;
}
#p2 {
text-align: center;
}
input[type="text"] {
border: 2px solid grey;
border-radius: 4px;
padding: 6px;
width: 90%;
background-color: #d3d3d3;
display: block;
margin: 8px 0;
}
input[type="text"]:focus {
border: 2px solid blue;
}
input[type="email"]:focus {
border: 2px solid blue;
}
::placeholder {
text-align: right;
}
input[type="submit"] {
background-color: #3cbc8d;
color: white;
border-radius: 4px;
padding: 16px 32px;
width: 100%;
}
input[type="email"] {
border: 2px solid grey;
padding: 6px;
width: 90%;
background-color: #d3d3d3;
display: block;
margin: 8px 0;
}
#p3 {
text-align: center;
}
#submitdiv {
text-align: center;
}
#textareadiv {
text-align: center;
}
textarea {
width: 100%;
}
hr {
width: 100%;
}
select {
background-color: #d3d3d3;
padding: 6px;
width: 90%;
display: block;
margin: 8px 0;
}
input[id="zipcode"] {
width: 40%;
}
body {
font-family: 'Merriweather', serif;
}
fieldset {
border: none;
}
#media screen and (min-width: 768px) {
.formcenter {
text-align: center;
display: block;
}
form {
text-align: left;
margin-left: auto;
margin-right: auto;
display: inline-block;
}
select {
width: 90%;
padding: 6px;
border-radius: 4px;
}
#p1 {
width: 100%;
}
hr {
width: 100%;
}
}
<link href="https://fonts.googleapis.com/css?family=Merriweather" rel="stylesheet">
<p id="p1">THE CODE REVIEW</p><br><br>
<div class="formcenter">
<form method="post" action="project3.html">
<h3 id="h31">Sign up for our newsletter</h3>
<p id="p2">Get the latest news on how your code is doing right in your inbox</p>
<hr>
<hr>
<fieldset>
<legend>
<h3 id="h32">Contact Information</h3>
</legend>
<label for="inputfield">Full Name</label>
<input type="text" name="fullname" placeholder="Required" id="inputfield">
<label for="inputfield1">Email Address</label>
<input type="email" name="emailaddress" placeholder="Required" id="inputfield1">
<label for="inputfield2">Phone Number</label>
<input type="text" name="phonenumber" id="inputfield2">
<label for="inputfield3">Street Address</label>
<input type="text" name="streetaddress" id="inputfield3">
<label for="inputfield4">City</label>
<input type="text" name="city" id="inputfield4">
<label for="stateselect">State</label>
<select name="state" id="stateselect">
<option>Choose State</option>
<option value="mah">Maharashtra</option>
<option value="guj">Gujarat</option>
<option value="pun">Punjab</option>
</select>
<label for="zipcode">Zip Code</label>
<input type="text" name="zipcode" id="zipcode">
</fieldset>
<hr>
<fieldset>
<legend>
<h3>Newsletter</h3>
</legend><br>
<label>Select the newsletters you would like to receive</label><br><br>
<input type="checkbox" name="htmlnews"><label>HTML News</label><br><br>
<input type="checkbox" name="css"><label>CSS News</label><br><br>
<input type="checkbox" name="javascript"><label>Javascript News</label><br><br>
<label>Newsletter format</label><br><br>
<input type="radio" name="newsletter" value="html"><label>HTML</label><br><br>
<input type="radio" name="newsletter" value="plaintext"><label>Plain Text</label><br><br>
<label>Other topics you'd like to hear about</label><br><br>
<div id="textareadiv">
<textarea rows="5" cols="30"></textarea><br><br>
</div>
<div id="submitdiv">
<input type="submit" value="Sign Up"><br><br>
</div>
</fieldset>
<p id="p3"><i>Copyright The Code Review</i></p>
</form>
</div>
You can reset width and display on inputs and/or use float.
It can be a reset at any time or within the mediaquerie.
You can also filter within which fieldset you need this reset to be effective.(example below)
.formcenter fieldset:first-of-type label,
.formcenter fieldset:first-of-type input{
float: left;
line-height: 1.2em;
padding: 6px;
margin: 8px 0;
width: 50%;
}
.formcenter fieldset:first-of-type label {
clear: left;
width: 35%;
}
input[type="checkbox"],
input[type="radio"]{
margin-right:1em;
}
fieldset ~ fieldset br + label {
margin:1em;
color:gray
}
#p1 {
text-align: center;
background-color: black;
color: white;
padding: 20px;
}
#h31 {
text-align: center;
}
#p2 {
text-align: center;
}
input[type="text"] {
border: 2px solid grey;
border-radius: 4px;
padding: 6px;
width: 90%;
background-color: #d3d3d3;
display: block;
margin: 8px 0;
}
input[type="text"]:focus {
border: 2px solid blue;
}
input[type="email"]:focus {
border: 2px solid blue;
}
::placeholder {
text-align: right;
}
input[type="submit"] {
background-color: #3cbc8d;
color: white;
border-radius: 4px;
padding: 16px 32px;
width: 100%;
}
input[type="email"] {
border: 2px solid grey;
padding: 6px;
width: 90%;
background-color: #d3d3d3;
display: block;
margin: 8px 0;
}
#p3 {
text-align: center;
}
#submitdiv {
text-align: center;
}
#textareadiv {
text-align: center;
}
textarea {
width: 100%;
}
hr {
width: 100%;
}
select {
background-color: #d3d3d3;
padding: 6px;
width: 90%;
display: block;
margin: 8px 0;
}
input[id="zipcode"] {
width: 40%;
}
body {
font-family: 'Merriweather', serif;
}
fieldset {
border: none;
}
#media screen and (min-width: 768px) {
.formcenter {
text-align: center;
display: block;
}
form {
text-align: left;
margin-left: auto;
margin-right: auto;
display: inline-block;
}
select {
width: 90%;
padding: 6px;
border-radius: 4px;
}
#p1 {
width: 100%;
}
hr {
width: 100%;
}
.formcenter fieldset:first-of-type label,
.formcenter fieldset:first-of-type input{
float: left;
line-height: 1.2em;
padding: 6px;
margin: 8px 0;
width: 50%;
}
.formcenter fieldset:first-of-type label {
clear: left;
width: 35%;
}
input[type="checkbox"],
input[type="radio"]{
margin-right:1em;
}
fieldset ~ fieldset br + label {
margin:1em;
color:gray
}
}
<link href="https://fonts.googleapis.com/css?family=Merriweather" rel="stylesheet">
<p id="p1">THE CODE REVIEW</p><br><br>
<div class="formcenter">
<form method="post" action="project3.html">
<h3 id="h31">Sign up for our newsletter</h3>
<p id="p2">Get the latest news on how your code is doing right in your inbox</p>
<hr>
<hr>
<fieldset>
<legend>
<h3 id="h32">Contact Information</h3>
</legend>
<label for="inputfield">Full Name</label>
<input type="text" name="fullname" placeholder="Required" id="inputfield">
<label for="inputfield1">Email Address</label>
<input type="email" name="emailaddress" placeholder="Required" id="inputfield1">
<label for="inputfield2">Phone Number</label>
<input type="text" name="phonenumber" id="inputfield2">
<label for="inputfield3">Street Address</label>
<input type="text" name="streetaddress" id="inputfield3">
<label for="inputfield4">City</label>
<input type="text" name="city" id="inputfield4">
<label for="stateselect">State</label>
<select name="state" id="stateselect">
<option>Choose State</option>
<option value="mah">Maharashtra</option>
<option value="guj">Gujarat</option>
<option value="pun">Punjab</option>
</select>
<label for="zipcode">Zip Code</label>
<input type="text" name="zipcode" id="zipcode">
</fieldset>
<hr>
<fieldset>
<legend>
<h3>Newsletter</h3>
</legend><br>
<label>Select the newsletters you would like to receive</label><br><br>
<input type="checkbox" name="htmlnews"><label>HTML News</label><br><br>
<input type="checkbox" name="css"><label>CSS News</label><br><br>
<input type="checkbox" name="javascript"><label>Javascript News</label><br><br>
<label>Newsletter format</label><br><br>
<input type="radio" name="newsletter" value="html"><label>HTML</label><br><br>
<input type="radio" name="newsletter" value="plaintext"><label>Plain Text</label><br><br>
<label>Other topics you'd like to hear about</label><br><br>
<div id="textareadiv">
<textarea rows="5" cols="30"></textarea><br><br>
</div>
<div id="submitdiv">
<input type="submit" value="Sign Up"><br><br>
</div>
</fieldset>
<p id="p3"><i>Copyright The Code Review</i></p>
</form>
</div>
You can use display:flex with a container for each lines
.line
{
display:flex;
align-items:center;
}
.line label
{
min-width:200px;
}
input
{
flex:1;
margin:10px;
}
#p1 {
text-align: center;
background-color: black;
color: white;
padding: 20px;
}
#h31 {
text-align: center;
}
#p2 {
text-align: center;
}
input[type="text"] {
border: 2px solid grey;
border-radius: 4px;
padding: 6px;
width: 90%;
background-color: #d3d3d3;
display: block;
margin: 8px 0;
}
input[type="text"]:focus {
border: 2px solid blue;
}
input[type="email"]:focus {
border: 2px solid blue;
}
::placeholder {
text-align: right;
}
input[type="submit"] {
background-color: #3cbc8d;
color: white;
border-radius: 4px;
padding: 16px 32px;
width: 100%;
}
input[type="email"] {
border: 2px solid grey;
padding: 6px;
width: 90%;
background-color: #d3d3d3;
display: block;
margin: 8px 0;
}
#p3 {
text-align: center;
}
#submitdiv {
text-align: center;
}
#textareadiv {
text-align: center;
}
textarea {
width: 100%;
}
hr {
width: 100%;
}
select {
background-color: #d3d3d3;
padding: 6px;
width: 90%;
display: block;
margin: 8px 0;
}
input[id="zipcode"] {
width: 40%;
}
body {
font-family: 'Merriweather', serif;
}
fieldset {
border: none;
}
#media screen and (min-width: 768px) {
.formcenter {
text-align: center;
display: block;
}
form {
text-align: left;
margin-left: auto;
margin-right: auto;
display: inline-block;
}
select {
width: 90%;
padding: 6px;
border-radius: 4px;
}
#p1 {
width: 100%;
}
hr {
width: 100%;
}
}
<link href="https://fonts.googleapis.com/css?family=Merriweather" rel="stylesheet">
<p id="p1">THE CODE REVIEW</p><br><br>
<div class="formcenter">
<form method="post" action="project3.html">
<h3 id="h31">Sign up for our newsletter</h3>
<p id="p2">Get the latest news on how your code is doing right in your inbox</p>
<hr>
<hr>
<fieldset>
<legend>
<h3 id="h32">Contact Information</h3>
</legend>
<div class="line">
<label for="inputfield">Full Name :</label>
<input type="text" name="fullname" placeholder="Required" id="inputfield">
</div>
<div class="line">
<label for="inputfield1">Email Address :</label>
<input type="email" name="emailaddress" placeholder="Required" id="inputfield1">
</div>
<div class="line">
<label for="inputfield2">Phone Number :</label>
<input type="text" name="phonenumber" id="inputfield2">
</div>
<div class="line">
<label for="inputfield3">Street Address :</label>
<input type="text" name="streetaddress" id="inputfield3">
</div>
<div class="line">
<label for="inputfield4">City :</label>
<input type="text" name="city" id="inputfield4">
</div>
<div class="line">
<label for="stateselect">State :</label>
<select name="state" id="stateselect">
<option>Choose State</option>
<option value="mah">Maharashtra</option>
<option value="guj">Gujarat</option>
<option value="pun">Punjab</option>
</select>
</div>
<div class="line">
<label for="zipcode">Zip Code :</label>
<input type="text" name="zipcode" id="zipcode">
</div>
</fieldset>
<hr>
<fieldset>
<legend>
<h3>Newsletter</h3>
</legend><br>
<label>Select the newsletters you would like to receive</label><br><br>
<input type="checkbox" name="htmlnews"><label>HTML News</label><br><br>
<input type="checkbox" name="css"><label>CSS News</label><br><br>
<input type="checkbox" name="javascript"><label>Javascript News</label><br><br>
<label>Newsletter format</label><br><br>
<input type="radio" name="newsletter" value="html"><label>HTML</label><br><br>
<input type="radio" name="newsletter" value="plaintext"><label>Plain Text</label><br><br>
<label>Other topics you'd like to hear about</label><br><br>
<div id="textareadiv">
<textarea rows="5" cols="30"></textarea><br><br>
</div>
<div id="submitdiv">
<input type="submit" value="Sign Up"><br><br>
</div>
</fieldset>
<p id="p3"><i>Copyright The Code Review</i></p>
</form>
</div>
Swap your labels and inputs to get desired result:
#p1 {
text-align: center;
background-color: black;
color: white;
padding: 20px;
}
#h31 {
text-align: center;
}
#p2 {
text-align: center;
}
input[type="text"] {
border: 2px solid grey;
border-radius: 4px;
padding: 6px;
width: 90%;
background-color: #d3d3d3;
display: block;
margin: 8px 0;
}
input[type="text"]:focus {
border: 2px solid blue;
}
input[type="email"]:focus {
border: 2px solid blue;
}
::placeholder {
text-align: right;
}
input[type="submit"] {
background-color: #3cbc8d;
color: white;
border-radius: 4px;
padding: 16px 32px;
width: 100%;
}
input[type="email"] {
border: 2px solid grey;
padding: 6px;
width: 90%;
background-color: #d3d3d3;
display: block;
margin: 8px 0;
}
#p3 {
text-align: center;
}
#submitdiv {
text-align: center;
}
#textareadiv {
text-align: center;
}
textarea {
width: 100%;
}
hr {
width: 100%;
}
select {
background-color: #d3d3d3;
padding: 6px;
width: 90%;
display: block;
margin: 8px 0;
}
input[id="zipcode"] {
width: 40%;
}
body {
font-family: 'Merriweather', serif;
}
fieldset {
border: none;
}
#media screen and (min-width: 768px) {
.formcenter {
text-align: center;
display: block;
}
form {
text-align: left;
margin-left: auto;
margin-right: auto;
display: inline-block;
}
select {
width: 90%;
padding: 6px;
border-radius: 4px;
}
#p1 {
width: 100%;
}
hr {
width: 100%;
}
}
<link href="https://fonts.googleapis.com/css?family=Merriweather" rel="stylesheet">
<p id="p1">THE CODE REVIEW</p><br><br>
<div class="formcenter">
<form method="post" action="project3.html">
<h3 id="h31">Sign up for our newsletter</h3>
<p id="p2">Get the latest news on how your code is doing right in your inbox</p>
<hr>
<hr>
<fieldset>
<legend>
<h3 id="h32">Contact Information</h3>
</legend>
<label for="inputfield">Full Name</label>
<input type="text" name="fullname" placeholder="Required" id="inputfield">
<label for="inputfield1">Email Address</label>
<input type="email" name="emailaddress" placeholder="Required" id="inputfield1">
<label for="inputfield2">Phone Number</label>
<input type="text" name="phonenumber" id="inputfield2">
<label for="inputfield3">Street Address</label>
<input type="text" name="streetaddress" id="inputfield3">
<label for="inputfield4">City</label>
<input type="text" name="city" id="inputfield4">
<label for="stateselect">State</label>
<select name="state" id="stateselect">
<option>Choose State</option>
<option value="mah">Maharashtra</option>
<option value="guj">Gujarat</option>
<option value="pun">Punjab</option>
</select>
<label for="zipcode">Zip Code</label>
<input type="text" name="zipcode" id="zipcode">
</fieldset>
<hr>
<fieldset>
<legend>
<h3>Newsletter</h3>
</legend><br>
<label>Select the newsletters you would like to receive</label><br><br>
<label>HTML News</label><input type="checkbox" name="htmlnews"><br><br>
<label>CSS News</label><input type="checkbox" name="css"><br><br>
<label>Javascript News</label><input type="checkbox" name="javascript"><br><br>
<label>Newsletter format</label><br><br>
<label>HTML</label><input type="radio" name="newsletter" value="html"><br><br>
<label>Plain Text</label><input type="radio" name="newsletter" value="plaintext"><br><br>
<label>Other topics you'd like to hear about</label><br><br>
<div id="textareadiv">
<textarea rows="5" cols="30"></textarea><br><br>
</div>
<div id="submitdiv">
<input type="submit" value="Sign Up"><br><br>
</div>
</fieldset>
<p id="p3"><i>Copyright The Code Review</i></p>
</form>
</div>
You can do this fairly easily:
Full Name: <input type="text" name="fullname" placeholder="Required" id="inputfield">
This will just make the text appear to the left side of the input itself.
I used this method for a newsletter input I was doing:
Name: <input id="nameInput" type="text" name="name" required><br>

Align 2 items side-by-side in a flex column

I am very new to flex. But I'm loving it so far. The question I have is, how do I align two items side-by-side when the parent has flex-direction: column;?
I have pasted my HTML and Sass code here along with screenshots.
I have a form like so:
// manage.html
<div class="form2" hidden>
<div class="message" hidden></div>
<form id="updateUserForm" action="https://someaction.com" method="POST">
<label class="userInfo"></label>
<input type="text" name="userFirstName" class="userFirstName" placeholder="First Name">
<input type="text" name="userLastName" class="userLastName" placeholder="Last Name">
<input type="text" name="subTag" class="subTag" placeholder="Title, benefits, companies, expertise">
<input type="text" name="subLocation" class="subLocation" placeholder="City, state, zipe code or country">
Cancel
<button type="submit " class="updateButton"></button>
</form>
</div>
That is styled like so:
// style.scss
.form2 {
#updateUserForm {
display: flex;
flex-direction: column;
}
label {
text-align: center;
}
input {
border-style: solid;
border-color: $black;
color: $black;
font-size: 16px;
padding: 10px 14px;
text-align: left;
margin: 4px 2px;
}
input:focus::-webkit-input-placeholder {
color: transparent;
}
input:focus:-moz-placeholder {
color: transparent;
}
input:focus::-moz-placeholder {
color: transparent;
}
input:focus:-ms-input-placeholder {
color: transparent;
}
a,
button {
border: none;
color: $white;
background-color: $blue;
padding: 12px 16px;
text-align: center;
text-decoration: none;
font-size: 16px;
margin: 4px 2px;
display: inline-block;
align-self: flex-start;
cursor: pointer;
}
}
Here is the output:
I would like to align 'Cancel' link and 'Reactivate' button side by side. I tried the following:
// style.scss
a {
align-self: flex-start;
}
button {
align-self: flex-end;
}
This only slides the elements to left and right respectively without being on the same line. Here is the output I wish to get:
Any suggestions on how to reach my expected output?
You can just use flex-wrap: wrap with row direction and set flex: 0 0 100% on inputs.
form {
display: flex;
flex-wrap: wrap;
}
input {
flex: 0 0 100%;
}
<div class="form2">
<div class="message"></div>
<form id="updateUserForm">
<label class="userInfo"></label>
<input type="text" name="userFirstName" class="userFirstName" placeholder="First Name">
<input type="text" name="userLastName" class="userLastName" placeholder="Last Name">
<input type="text" name="subTag" class="subTag" placeholder="Title, benefits, companies, expertise">
<input type="text" name="subLocation" class="subLocation" placeholder="City, state, zipe code or country">
Cancel
<button type="submit " class="updateButton">Lorem</button>
</form>
</div>
Just place them in a container and change direction.
#updateUserForm {
display: flex;
flex-direction: column;
}
label {
text-align: center;
}
input {
border-style: solid;
border-color: black;
color: black;
font-size: 16px;
padding: 10px 14px;
text-align: left;
margin: 4px 2px;
}
input:focus::-webkit-input-placeholder {
color: transparent;
}
input:focus:-moz-placeholder {
color: transparent;
}
input:focus::-moz-placeholder {
color: transparent;
}
input:focus:-ms-input-placeholder {
color: transparent;
}
/*Just place them in a container and change direction*/
#button_container{
display:flex;
}
a,
button {
border: none;
color: white;
background-color: blue;
padding: 12px 16px;
text-align: center;
text-decoration: none;
font-size: 16px;
margin: 4px 2px;
display: inline-block;
align-self: flex-start;
cursor: pointer;
text-transform: uppercase;
}
<div class="form2">
<div class="message" hidden></div>
<form id="updateUserForm" action="https://someaction.com" method="POST">
<label class="userInfo"></label>
<input type="text" name="userFirstName" class="userFirstName" placeholder="First Name">
<input type="text" name="userLastName" class="userLastName" placeholder="Last Name">
<input type="text" name="subTag" class="subTag" placeholder="Title, benefits, companies, expertise">
<input type="text" name="subLocation" class="subLocation" placeholder="City, state, zipe code or country">
<!--Just place them in a container and change direction -->
<div id="button_container">
Cancel
<button type="submit " class="updateButton">Reactivate</button>
<div>
</form>
</div>
With the existing markup, using flex-direction: column, you can't make those two "buttons" align side-by-side.
One way is to change to row wrap and make all the input + label 100% width, which can be done with either width: 100% (used below) or flex-basis: 100%.
With that the items being 100% wide will stack vertical and the rest horizontal (unless their total width doesn't exceed 100%)
Stack snippet
form {
display: flex;
flex-wrap: wrap;
}
label {
text-align: center;
width: 100%;
}
input {
border-style: solid;
border-color: black;
color: black;
font-size: 16px;
padding: 10px 14px;
text-align: left;
margin: 4px 2px;
width: 100%;
}
a,
button {
border: none;
color: white;
background-color: blue;
padding: 12px 16px;
text-align: center;
text-decoration: none;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
<div class="form2">
<div class="message"></div>
<form id="updateUserForm" action="https://api.ghjobssubscribe.com/manage/update" method="POST">
<label class="userInfo"></label>
<input type="text" name="userFirstName" class="userFirstName" placeholder="First Name">
<input type="text" name="userLastName" class="userLastName" placeholder="Last Name">
<input type="text" name="subTag" class="subTag" placeholder="Title, benefits, companies, expertise">
<input type="text" name="subLocation" class="subLocation" placeholder="City, state, zipe code or country">
Cancel
<button type="submit " class="updateButton">Submit</button>
</form>
</div>